summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/TopoSearchActionTest.java
blob: d052d26e2585c564d4a4d5e0b2944ea20b9e329f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * ============LICENSE_START===============================================
 * ONAP SDC
 * ========================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=================================================
 */

package org.onap.sdc.dcae.composition.restmodels.ruleeditor;

import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanConstructor;
import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEquals;
import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanHashCodeFor;
import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;
import java.util.Map;

import org.junit.Test;

public class TopoSearchActionTest {

	private static Gson gson = new GsonBuilder()
			.registerTypeAdapter(BaseAction.class, new ActionDeserializer())
			.registerTypeAdapter(BaseCondition.class, new ConditionDeserializer()).create();

	private static final String ACTION_TYPE = "Test Topology Search";
	private static final String ID = "123";
	private static final String SEARCH_FIELD = "sourceToSearch";
	private static final String SEARCH_STRING = "searchString";
	private static final String SEARCH_VALUE = "${"+ SEARCH_STRING +"}";
	private static final String RADIO = "1234";
	private static final String SEARCH_FILTER_LEFT = "${event.commonEventHeader.eventId}";
	private static final String SEARCH_FILTER_RIGHT_ITEM = "testItem";
	private static final String SEARCH_FILTER_RIGHT_ARRAY = "["+ SEARCH_FILTER_RIGHT_ITEM +"]";
	private static final String SEARCH_FILTER_OPERATOR = "TestOperator";
	private static final String ENRICH_FIELD1 = "enrichFiled1";
	private static final String ENRICH_FIELD2 = "enrichField2";
	private static final String ENRICH_FIELDS_ARRAY = "[{value:"+ ENRICH_FIELD1 +"},{value:"+ ENRICH_FIELD2 +"}]";
	private static final String ENRICH_PREFIX = "enrichPrefix";

	private static final String UI_HASH_MAP_KEY1 = "testKey1";
	private static final String UI_HASH_MAP_KEY2 = "testKey2";
	private static final String UI_HASH_MAP_VALUE1 = "test1";
	private static final String UI_HASH_MAP_VALUE2 = "test2";

	private static final String BAD_STRIPPED_TARGET = "badTarget";

	@Test
	public void shouldHaveValidGettersAndSetters() {
		assertThat(TopoSearchAction.class, hasValidGettersAndSetters());
	}

	@Test
	public void checkEquals() {
		assertThat(TopoSearchAction.class, hasValidBeanEquals());
	}

	@Test
	public void testHasValidConstructor() {
		assertThat(TopoSearchAction.class, hasValidBeanConstructor());
	}

	@Test
	public void checkHashCodeFor() {
		assertThat(TopoSearchAction.class, hasValidBeanHashCodeFor());
	}

	@Test
	public void topSearchActionBaseFieldTest() {
		TopoSearchAction topoSearchAction = buildBaseConditionalTopoSearchAction();

		assertEquals(SEARCH_FIELD, topoSearchAction.searchField());
		assertEquals(SEARCH_VALUE, topoSearchAction.searchValue());
		assertNotNull(topoSearchAction.searchFilter());

		Condition filter = topoSearchAction.searchFilter();
		assertEquals(SEARCH_FILTER_LEFT, filter.left);
		assertEquals(SEARCH_FILTER_RIGHT_ITEM, filter.right.get(0));
		assertEquals(SEARCH_FILTER_OPERATOR, filter.operator);

		assertEquals(ENRICH_PREFIX, topoSearchAction.enrichPrefix());
		assertNotNull(topoSearchAction.enrichFields());

		List<String> enrichFields = topoSearchAction.enrichFields();
		assertEquals(ENRICH_FIELD1, enrichFields.get(0));
		assertEquals(ENRICH_FIELD2, enrichFields.get(1));

		assertTrue(topoSearchAction.doEnrich());
	}

	@Test
	public void topSearchActionUpdatesTest() {
		TopoSearchAction topoSearchAction = buildConditionalTopoSearchActionWithUpdates();
		assertNotNull(topoSearchAction.updates());

		List<BaseAction.UIHashMap> uiHashMapList = topoSearchAction.updates();
		assertEquals(UI_HASH_MAP_VALUE1, uiHashMapList.get(0).getValue());
		assertEquals(UI_HASH_MAP_VALUE2, uiHashMapList.get(1).getValue());
		assertEquals(UI_HASH_MAP_KEY1, uiHashMapList.get(0).getKey());
		assertEquals(UI_HASH_MAP_KEY2, uiHashMapList.get(1).getKey());

		Map<String, String> updatesMap = topoSearchAction.updatesMap();
		assertEquals(UI_HASH_MAP_VALUE1, updatesMap.get(UI_HASH_MAP_KEY1));
		assertEquals(UI_HASH_MAP_VALUE2, updatesMap.get(UI_HASH_MAP_KEY2));
	}

	@Test
	public void checkReferencesTargetTest() {
		TopoSearchAction topoSearchAction = buildBaseConditionalTopoSearchAction();

		BaseAction baseAction = new TestBaseAction(SEARCH_STRING);
		BaseAction baseActionWithBadTarget = new TestBaseAction(BAD_STRIPPED_TARGET);

		assertTrue(topoSearchAction.referencesTarget(baseAction));
		assertFalse(topoSearchAction.referencesTarget(baseActionWithBadTarget));
	}

	private TopoSearchAction buildBaseConditionalTopoSearchAction() {
		String topoSearch = "{actionType:\"" + ACTION_TYPE + "\"," +
				"id:" + ID + "," +
				"search:{searchField:" + SEARCH_FIELD + "," +
				"searchValue:\"" + SEARCH_VALUE + "\"," +
				"radio:" + RADIO + "," +
				"searchFilter:{left:\"" + SEARCH_FILTER_LEFT + "\"," +
				"right:" + SEARCH_FILTER_RIGHT_ARRAY + "," +
				"operator:" + SEARCH_FILTER_OPERATOR + "}," +
				"enrich:{fields:" + ENRICH_FIELDS_ARRAY + "," +
				"prefix:" + ENRICH_PREFIX + "}}}";
		return buildTopoSearchAction(topoSearch);
	}

	private TopoSearchAction buildConditionalTopoSearchActionWithUpdates() {
		String topoSearch = "{search:{updates: [{key:" + UI_HASH_MAP_KEY1 + ", value:" + UI_HASH_MAP_VALUE1 + "}," +
				"{key:" + UI_HASH_MAP_KEY2 + ", value:" + UI_HASH_MAP_VALUE2 + "}]}}";

		return buildTopoSearchAction(topoSearch);
	}

	private TopoSearchAction buildTopoSearchAction(String topoSearch) {
		return gson.fromJson(topoSearch, TopoSearchAction.class);
	}

	private class TestBaseAction extends BaseAction {

		private String strippedTarget;

		TestBaseAction(String strippedTarget) {
			this.strippedTarget = strippedTarget;
		}

		@Override
		public boolean referencesTarget(BaseAction other) {
			return false;
		}

		@Override
		public String strippedTarget() {
			return strippedTarget;
		}
	}
}