summaryrefslogtreecommitdiffstats
path: root/aai-schema-ingest/src/test/java/org/onap/aai/edges/EdgeIngestorLocalTest.java
blob: e3826576a2a7057e951adf3df2550c7b235cf059 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-18 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 com.google.common.collect.Multimap;

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.config.EdgesConfiguration;
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.restclient.MockProvider;
import org.onap.aai.setup.SchemaLocationsBean;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.testutils.TestUtilConfigTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {EdgesConfiguration.class, TestUtilConfigTranslator.class})
@TestPropertySource(
        properties = {
                "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local.properties"})

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
// @TestPropertySource(locations = "/schema-service-rest.properties" )
@SpringBootTest
public class EdgeIngestorLocalTest {
    @Autowired
    EdgeIngestor edgeIngestor;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void getRulesTest1() throws EdgeRuleNotFoundException {
        EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
        Multimap<String, EdgeRule> results = edgeIngestor.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("dog", "puppy").build();
        Multimap<String, EdgeRule> results = edgeIngestor.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 getRulesFlippedTypesTest() throws EdgeRuleNotFoundException {
        EdgeRuleQuery q =
                new EdgeRuleQuery.Builder("l-interface", "logical-link").version(new SchemaVersion("v11")).build();
        Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
        assertTrue(results.size() == 3);
        for (EdgeRule r : results.get("l-interface|logical-link")) {
            if ("org.onap.relationships.inventory.Source".equals(r.getLabel())
                    || "org.onap.relationships.inventory.Destination".equals(r.getLabel())) {
                // these are defined with from=logical-link, to=l-interface, so they must be flipped
                assertTrue(Direction.IN.equals(r.getDirection()));
            } else if ("tosca.relationships.network.LinksTo".equals(r.getLabel())) {
                // this is defined with from=l-interface, to=logical-link, so it shouldn't be flipped
                assertTrue(Direction.OUT.equals(r.getDirection()));
            } else {
                fail("how did you get here");
            }
        }
    }

    @Test
    public void fromToSameFlipTests() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
        // getRules, setting from and to
        EdgeRuleQuery q = new EdgeRuleQuery.Builder("bloop", "bloop").version(new SchemaVersion("v11")).build();
        Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
        assertTrue(results.size() == 1);
        for (EdgeRule r : results.get("bloop|bloop")) {
            assertTrue(Direction.IN.equals(r.getDirection()));
        }

        // getRule, setting just from
        EdgeRuleQuery q2 = new EdgeRuleQuery.Builder("bloop").version(new SchemaVersion("v11")).build();
        assertTrue(Direction.IN.equals(edgeIngestor.getRule(q2).getDirection()));

        // getChildRules
        Multimap<String, EdgeRule> child = edgeIngestor.getChildRules("bloop", new SchemaVersion("v11"));
        assertTrue(child.size() == 1);
        for (EdgeRule r : child.get("bloop|bloop")) {
            assertTrue(Direction.IN.equals(r.getDirection()));
        }
    }

    @Test
    public void getRulesTest3() throws EdgeRuleNotFoundException {
        EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v11")).build();
        Multimap<String, EdgeRule> results = edgeIngestor.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();
        edgeIngestor.getRules(q);
    }

    @Test
    public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
        EdgeRuleQuery q = new EdgeRuleQuery.Builder("parent", "notation").build();
        EdgeRule result = edgeIngestor.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 getRuleFlippedTypesTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
        EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
        EdgeRule result = edgeIngestor.getRule(q);
        assertTrue("parent".equals(result.getFrom()));
        assertTrue("notation".equals(result.getTo()));
        assertTrue("has".equals(result.getLabel()));
        // direction flipped to match input order per old EdgeRules.java API
        assertTrue(Direction.IN.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(new
    // SchemaVersion("v11")).build();
    // EdgeRule res = edgeIngestor.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(new
    // SchemaVersion("v11")).build();
    // EdgeRule res = edgeIngestor.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();
        edgeIngestor.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();
    // edgeIngestor.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(new SchemaVersion("v11")).build();
        edgeIngestor.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(new SchemaVersion("v11")).build();
        edgeIngestor.getRule(q);
    }

    // @Test
    // public void hasRuleTest() {
    // assertTrue(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").version(new
    // SchemaVersion("v11")).build()));
    // assertFalse(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
    // }
    //
    // @Test
    // public void getCousinRulesTest() {
    // Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("dog");
    // assertTrue(results.size() == 2);
    // assertTrue(results.containsKey("dog|puppy"));
    // assertTrue(results.containsKey("dog|foo"));
    // }

    @Test
    public void getCousinRulesWithVersionTest() {
        Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("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 = edgeIngestor.getCousinRules("foo", new SchemaVersion("v11"));
        assertTrue(results.isEmpty());
    }

    // @Test
    // public void hasCousinTest() {
    // assertTrue(edgeIngestor.hasCousinRule("foo"));
    // assertTrue(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v10")));
    // assertFalse(edgeIngestor.hasCousinRule("parent"));
    // assertFalse(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v11")));
    // }

    @Test
    public void getChildRulesTest() {
        Multimap<String, EdgeRule> results = edgeIngestor.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 = edgeIngestor.getChildRules("foo", new SchemaVersion("v10"));
        assertTrue(results.size() == 2);
        assertTrue(results.containsKey("baz|foo"));
        assertTrue(results.containsKey("foo|quux"));
    }

    @Test
    public void getChildRulesNoneInVersionTest() {
        Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v11"));
        assertTrue(results.isEmpty());
    }

    // @Test
    // public void hasChildTest() {
    // assertTrue(edgeIngestor.hasChildRule("foo"));
    // assertTrue(edgeIngestor.hasChildRule("foo", new SchemaVersion("v10")));
    // assertFalse(edgeIngestor.hasChildRule("puppy"));
    // assertFalse(edgeIngestor.hasChildRule("foo", new SchemaVersion("v11")));
    // }

    @Test
    public void getParentRulesTest() {
        Multimap<String, EdgeRule> results = edgeIngestor.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 = edgeIngestor.getParentRules("baz", new SchemaVersion("v10"));
        assertTrue(results.size() == 1);
        assertTrue(results.containsKey("baz|foo"));
    }

    @Test
    public void getParentRulesNoneInVersionTest() {
        Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v11"));
        assertTrue(results.isEmpty());
    }

    @Test
    public void hasParentTest() {
        assertTrue(edgeIngestor.hasParentRule("parent"));
        assertTrue(edgeIngestor.hasParentRule("quux", new SchemaVersion("v10")));
        assertFalse(edgeIngestor.hasParentRule("puppy"));
        assertFalse(edgeIngestor.hasParentRule("foo", new SchemaVersion("v11")));
    }

    // @Test
    // public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
    // Multimap<String, EdgeRule> res = edgeIngestor.getAllCurrentRules();
    // assertTrue(res.size() == 18);
    // }

    @Test
    public void getAllRulesTest() throws EdgeRuleNotFoundException {
        Multimap<String, EdgeRule> res = edgeIngestor.getAllRules(new SchemaVersion("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.");
        edgeIngestor.getAllRules(new SchemaVersion("v9"));
    }
}