summaryrefslogtreecommitdiffstats
path: root/aai-schema-ingest/src/test/java/org/onap/aai/edges/EdgeIngestorTest.java
blob: fe43de09aaffbff7c88dc07ca49a6fbf6be432b1 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.aai.edges;

import static org.junit.Assert.*;

import java.util.Collection;

import org.apache.tinkerpop.gremlin.structure.Direction;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.onap.aai.edges.enums.AAIDirection;
import org.onap.aai.edges.enums.MultiplicityRule;
import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
import org.onap.aai.setup.SchemaLocationsBean;
import org.onap.aai.setup.Version;
import org.onap.aai.testutils.TestUtilConfigTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.collect.Multimap;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SchemaLocationsBean.class, TestUtilConfigTranslator.class, EdgeIngestor.class})
@SpringBootTest
public class EdgeIngestorTest {
	@Autowired
	EdgeIngestor ei;
	
	@Rule
	public ExpectedException thrown = ExpectedException.none();
	
	@Test
	public void getRulesTest1() throws EdgeRuleNotFoundException {
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
		Multimap<String, EdgeRule> results = ei.getRules(q);
		assertTrue(results.size() == 5);
		assertTrue(results.containsKey("bar|foo"));
		
		assertTrue(2 == results.get("bar|foo").size());
		boolean seenLabel1 = false;
		boolean seenLabel2 = false;
		for(EdgeRule r : results.get("bar|foo")) {
			if ("eats".equals(r.getLabel())) {
				seenLabel1 = true;
			}
			if ("eatz".equals(r.getLabel())) {
				seenLabel2 = true;
			}
		}
		assertTrue(seenLabel1 && seenLabel2);
		
		assertTrue(results.containsKey("baz|foo"));
		assertTrue(results.containsKey("foo|quux"));
		assertTrue(results.containsKey("dog|foo"));
	}
	
	@Test
	public void getRulesTest2() throws EdgeRuleNotFoundException {
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("puppy", "dog").build();
		Multimap<String, EdgeRule> results = ei.getRules(q);
		assertTrue(results.size() == 1);
		assertTrue(results.containsKey("dog|puppy"));
		Collection<EdgeRule> cr = results.get("dog|puppy");
		for (EdgeRule r : cr) {
			assertTrue("dog".equals(r.getFrom()));
			assertTrue("puppy".equals(r.getTo()));
			assertTrue("caresFor".equals(r.getLabel()));
			assertTrue(Direction.OUT.equals(r.getDirection()));
			assertTrue("One2Many".equalsIgnoreCase(r.getMultiplicityRule().toString()));
			assertTrue("NONE".equals(r.getContains()));
			assertTrue("OUT".equals(r.getDeleteOtherV()));
			assertTrue("NONE".equals(r.getPreventDelete()));
			assertTrue(r.isDefault());
		}
	}
	
	@Test
	public void getRulesTest3() throws EdgeRuleNotFoundException {
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").version(Version.V11).build();
		Multimap<String, EdgeRule> results = ei.getRules(q);
		assertTrue(results.size() == 4);
		assertTrue(results.containsKey("lag-interface|l-interface"));
		assertTrue(results.containsKey("l-interface|logical-link"));
		assertTrue(results.get("l-interface|logical-link").size() == 3);
	}
	
	@Test
	public void getRulesNoneFound() throws EdgeRuleNotFoundException {
		thrown.expect(EdgeRuleNotFoundException.class);
		thrown.expectMessage("No rules found for");
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").build();
		ei.getRules(q);
	}
	
	@Test
	public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
		EdgeRule result = ei.getRule(q);
		assertTrue("parent".equals(result.getFrom()));
		assertTrue("notation".equals(result.getTo()));
		assertTrue("has".equals(result.getLabel()));
		assertTrue(Direction.OUT.equals(result.getDirection()));
		assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
		assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
		assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
		assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
		assertTrue("parent contains notation".equals(result.getDescription()));
	}
	
	@Test
	public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","logical-link").version(Version.V11).build();	
		EdgeRule res = ei.getRule(q);
		assertTrue(res.isDefault());
		assertTrue("tosca.relationships.network.LinksTo".equals(res.getLabel()));
	}
	
	@Test
	public void getRuleWithNonDefault() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","logical-link").label("org.onap.relationships.inventory.Source").version(Version.V11).build();
		EdgeRule res = ei.getRule(q);
		assertFalse(res.isDefault());
		assertTrue("org.onap.relationships.inventory.Source".equals(res.getLabel()));
	}
	
	@Test
	public void getRuleNoneFoundTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		thrown.expect(EdgeRuleNotFoundException.class);
		thrown.expectMessage("No rule found for");
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","nonexistent").build();
		ei.getRule(q);
	}
	
	@Test
	public void getRuleTooManyPairsTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		thrown.expect(AmbiguousRuleChoiceException.class);
		thrown.expectMessage("No way to select single rule from these pairs:");
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
		ei.getRule(q);
	}
	
	@Test
	public void getRuleAmbiguousDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		thrown.expect(AmbiguousRuleChoiceException.class);
		thrown.expectMessage("Multiple defaults found.");
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("seed","plant").version(Version.V11).build();
		ei.getRule(q);
	}
	
	@Test
	public void getRuleNoDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
		thrown.expect(AmbiguousRuleChoiceException.class);
		thrown.expectMessage("No default found.");
		EdgeRuleQuery q = new EdgeRuleQuery.Builder("apple", "orange").version(Version.V11).build();
		ei.getRule(q);
	}
	
	@Test
	public void hasRuleTest() {
		assertTrue(ei.hasRule(new EdgeRuleQuery.Builder("l-interface").version(Version.V11).build()));
		assertFalse(ei.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
	}
	
	@Test
	public void getCousinRulesTest() {
		Multimap<String, EdgeRule> results = ei.getCousinRules("dog");
		assertTrue(results.size() == 2);
		assertTrue(results.containsKey("dog|puppy"));
		assertTrue(results.containsKey("dog|foo"));
	}
	
	@Test
	public void getCousinRulesWithVersionTest() {
		Multimap<String, EdgeRule> results = ei.getCousinRules("foo", Version.V10);
		assertTrue(results.size() == 2);
		assertTrue(results.containsKey("bar|foo"));
		assertTrue(results.get("bar|foo").size() == 2);
	}
	
	@Test
	public void getCousinsNoneInVersionTest() {
		Multimap<String, EdgeRule> results = ei.getCousinRules("foo", Version.V11);
		assertTrue(results.isEmpty());
	}
	
	@Test
	public void hasCousinTest() {
		assertTrue(ei.hasCousinRule("foo"));
		assertTrue(ei.hasCousinRule("foo", Version.V10));
		assertFalse(ei.hasCousinRule("parent"));
		assertFalse(ei.hasCousinRule("foo", Version.V11));
	}

	@Test
	public void getChildRulesTest() {
		Multimap<String, EdgeRule> results = ei.getChildRules("parent");
		assertTrue(results.size() == 6);
		assertTrue(results.containsKey("notation|parent"));
		assertTrue(results.containsKey("not-notation|parent"));
		assertTrue(results.containsKey("out-out|parent"));
		assertTrue(results.containsKey("in-in|parent"));
		assertTrue(results.containsKey("in-out|parent"));
		assertTrue(results.containsKey("out-in|parent"));
	}
	
	@Test
	public void getChildRulesWithVersionTest() {
		Multimap<String, EdgeRule> results = ei.getChildRules("foo", Version.V10);
		assertTrue(results.size() == 2);
		assertTrue(results.containsKey("baz|foo"));
		assertTrue(results.containsKey("foo|quux"));
	}
	
	@Test
	public void getChildRulesNoneInVersionTest() {
		Multimap<String, EdgeRule> results = ei.getChildRules("foo", Version.V11);
		assertTrue(results.isEmpty());
	}
	
	@Test
	public void hasChildTest() {
		assertTrue(ei.hasChildRule("foo"));
		assertTrue(ei.hasChildRule("foo", Version.V10));
		assertFalse(ei.hasChildRule("puppy"));
		assertFalse(ei.hasChildRule("foo", Version.V11));
	}
	
	@Test
	public void getParentRulesTest() {
		Multimap<String, EdgeRule> results = ei.getParentRules("parent");
		assertTrue(results.size() == 6);
		assertTrue(results.containsKey("grandparent1|parent"));
		assertTrue(results.containsKey("grandparent2|parent"));
		assertTrue(results.containsKey("grandparent3|parent"));
		assertTrue(results.containsKey("grandparent4|parent"));
		assertTrue(results.containsKey("grandparent5|parent"));
		assertTrue(results.containsKey("grandparent6|parent"));
	}
	
	@Test
	public void getParentRulesWithVersionTest() {
		Multimap<String, EdgeRule> results = ei.getParentRules("baz", Version.V10);
		assertTrue(results.size() == 1);
		assertTrue(results.containsKey("baz|foo"));
	}
	
	@Test
	public void getParentRulesNoneInVersionTest() {
		Multimap<String, EdgeRule> results = ei.getParentRules("baz", Version.V11);
		assertTrue(results.isEmpty());
	}
	
	@Test
	public void hasParentTest() {
		assertTrue(ei.hasParentRule("parent"));
		assertTrue(ei.hasParentRule("quux", Version.V10));
		assertFalse(ei.hasParentRule("puppy"));
		assertFalse(ei.hasParentRule("foo", Version.V11));
	}
	
	@Test
	public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
		Multimap<String, EdgeRule> res = ei.getAllCurrentRules();
		assertTrue(res.size() == 18);
	}
	
	@Test
	public void getAllRulesTest() throws EdgeRuleNotFoundException {
		Multimap<String, EdgeRule> res = ei.getAllRules(Version.V10);
		assertTrue(res.size() == 4);
		assertTrue(res.containsKey("bar|foo"));
		assertTrue(res.get("bar|foo").size() == 2);
		assertTrue(res.containsKey("baz|foo"));
		assertTrue(res.containsKey("foo|quux"));
		
		thrown.expect(EdgeRuleNotFoundException.class);
		thrown.expectMessage("No rules found for version V9.");
		ei.getAllRules(Version.V9);
	}
}