aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java
blob: aad11fc0457b46c2c26b96190f2721d98b8bb6e7 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * 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.schema;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Set;

import org.junit.Test;
import org.onap.crud.exception.CrudException;
import org.onap.crud.parser.EdgePayload;
import org.onap.crud.util.CrudServiceUtil;

public class EdgeRulesLoaderTest {

    @Test
    public void loadModels() throws Exception {
        EdgeRulesLoader.loadModels();
        assertTrue(EdgeRulesLoader.getSchemaForVersion ( "v11" ).isValidType ( "org.onap.relationships.inventory.groupsResourcesIn" ));
    }

    @Test
    public void loadModelsWithAVersion() throws Exception {
        EdgeRulesLoader.resetSchemaVersionContext ();
        EdgeRulesLoader.loadModels("V11");
        assertEquals(1, EdgeRulesLoader.getSchemas ().size ());
        assertEquals("v11", EdgeRulesLoader.getLatestSchemaVersion ());
    }

    @Test
    public void getSchemaForVersion() throws Exception {
        EdgeRulesLoader.resetSchemaVersionContext ();
        EdgeRulesLoader.loadModels("v11");
        String version = EdgeRulesLoader.getLatestSchemaVersion();
        RelationshipSchema g = EdgeRulesLoader.getSchemaForVersion(version);
        assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.groupsResourcesIn"));
        assertNotNull(g.lookupRelation("U:V:org.onap.relationships.inventory.groupsResourcesIn"));
        assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn"));
    }

    @Test
    public void getRelationshipTypeForNodePair() throws Exception {
        EdgeRulesLoader.resetSchemaVersionContext();
        EdgeRulesLoader.loadModels("v11");
        RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11");
        
        EdgePayload payload1 = new EdgePayload();
        payload1.setSource("services/inventory/v11/availability-zone/xxx");
        payload1.setTarget("services/inventory/v11/cloud-region/xxx");

        EdgePayload payload2 = new EdgePayload();
        payload2.setSource("services/inventory/v11/image/xxx");
        payload2.setTarget("services/inventory/v11/pserver/xxx");
        
        EdgePayload payload3 = new EdgePayload();
        payload3.setSource("services/inventory/v11/allotted-resource/xxx");
        payload3.setTarget("services/inventory/v11/instance-group/xxx");
        
        // Get edge types for node pair with a single possible edge between them
        Set<String> typeList = schema.getValidRelationTypes("availability-zone", "cloud-region");
        assertEquals(1, typeList.size());
        assertTrue(typeList.contains("org.onap.relationships.inventory.BelongsTo"));
        assertEquals(CrudServiceUtil.determineEdgeType(payload1, "v11"), "org.onap.relationships.inventory.BelongsTo");
        
        // Get edge types for node pair with no possible edge between them
        typeList = schema.getValidRelationTypes("image", "pserver");
        assertEquals(0, typeList.size());
        typeList = schema.getValidRelationTypes("cloud-region", "availability-zone");
        assertEquals(0, typeList.size());
        
        try {
          // Should throw an exception here
          CrudServiceUtil.determineEdgeType(payload2, "v11");
          assertTrue(false);
        }
        catch (CrudException ex) {
          System.out.println(ex.getMessage());
        }
        
        typeList = schema.getValidRelationTypes("allotted-resource", "instance-group");
        assertEquals(2, typeList.size());
        assertTrue(typeList.contains("org.onap.relationships.inventory.TestEdge"));
        assertTrue(typeList.contains("org.onap.relationships.inventory.MemberOf"));
        
        for (String type : typeList) {
          System.out.println(type);
        }
        
        try {
          // Should throw an exception here
          CrudServiceUtil.determineEdgeType(payload3, "v11");
          assertTrue(false);
        }
        catch (CrudException ex) {
          System.out.println(ex.getMessage());
        }
    }

    @Test
    public void getSchemaForVersionFail() throws Exception {
        EdgeRulesLoader.loadModels();
        try {
            EdgeRulesLoader.getSchemaForVersion("v1");
        } catch (CrudException e) {
            assertEquals(404, e.getHttpStatus().getStatusCode());
        }
    }
}