Allow ingestion of edge schema at deploy time
[aai/gizmo.git] / src / test / java / org / onap / schema / RelationshipSchemaTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.schema;
22
23 import org.apache.commons.io.IOUtils;
24 import org.junit.Test;
25 import org.onap.crud.exception.CrudException;
26
27 import com.att.aft.dme2.internal.apache.commons.lang.ArrayUtils;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.Arrays;
34 import java.util.ArrayList;
35 import java.util.Comparator;
36 import java.util.Comparator;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.List;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43 import java.util.stream.Collectors;
44
45 import static org.junit.Assert.*;
46
47 public class RelationshipSchemaTest {
48
49     final static Pattern rulesFilePattern = Pattern.compile("DbEdgeRules(.*).json");
50     final static Pattern propsFilePattern = Pattern.compile("edge_properties_(.*).json");
51     final static Pattern versionPattern = Pattern.compile(".*(v\\d+).json");
52
53
54     @Test
55     public void shouldLoadAllTheVersionsInDirectory() throws Exception {
56         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
57         loadRelations(versionContextMap);
58         assertTrue(versionContextMap.keySet().size() >= 0);
59     }
60
61     @Test
62     public void shouldContainValidTypes() throws Exception {
63         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
64         loadRelations(versionContextMap);
65         assertTrue(versionContextMap.get("v10").isValidType("groupsResourcesIn"));
66         assertTrue(versionContextMap.get("v10").isValidType("uses"));
67         assertFalse(versionContextMap.get("v10").isValidType("notValidType"));
68     }
69
70     @Test
71     public void shouldLookUpByRelation() throws Exception {
72         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
73         loadRelations(versionContextMap);
74         assertNotNull(versionContextMap.get("v10").lookupRelation("availability-zone:complex:groupsResourcesIn"));
75         assertTrue(versionContextMap.get("v10")
76                 .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("prevent-delete"));
77     }
78
79     @Test
80     public void shouldLookUpByRelationType() throws Exception {
81         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
82         loadRelations(versionContextMap);
83         assertNotNull(versionContextMap.get("v10").lookupRelationType("groupsResourcesIn"));
84         assertTrue(versionContextMap.get("v10")
85                 .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("prevent-delete"));
86     }
87
88     private void loadRelations(Map<String, RelationshipSchema> map){
89         ClassLoader classLoader = getClass().getClassLoader();
90         File dir = new File(classLoader.getResource("model").getFile());
91         File[] allFiles = dir.listFiles((d, name) ->
92                 (propsFilePattern.matcher(name).matches() || rulesFilePattern.matcher(name).matches()));
93         
94         // Special handling for the v12 file, as it is used for a special test
95         for (File f : allFiles) {
96           if (f.getName().equals("edge_properties_v11.json")) {
97             allFiles = (File[]) ArrayUtils.removeElement(allFiles, f);
98           }
99         }
100         
101         Arrays.stream(allFiles).sorted(Comparator.comparing(File::getName))
102                 .collect(Collectors.groupingBy(f -> myMatcher(versionPattern, f.getName())))
103                 .forEach((e, f) -> map.put(e, jsonFilesLoader(f)));
104
105     }
106
107
108     private RelationshipSchema jsonFilesLoader (List<File> files) {
109         List<String> fileContents = new ArrayList<>();
110         RelationshipSchema rsSchema = null;
111         for (File f : files) {
112             fileContents.add(jsonToString(f));
113         }
114
115         try {
116             if (fileContents.size() == 2) {
117                 rsSchema = new RelationshipSchema(fileContents);
118             }
119         } catch (CrudException e) {
120             e.printStackTrace();
121         } catch (IOException e) {
122             e.printStackTrace();
123         }
124
125         return rsSchema;
126     }
127
128     private String jsonToString (File file) {
129         InputStream inputStream = null;
130         String content = null;
131         HashMap<String,Object> result = null;
132
133         try {
134             inputStream = new FileInputStream(file);
135             content =  IOUtils.toString(inputStream, "UTF-8");
136         } catch (IOException e) {
137             e.printStackTrace();
138         }
139
140         return content;
141     }
142
143     private String myMatcher (Pattern p, String s) {
144         Matcher m = p.matcher(s);
145         return m.matches() ? m.group(1) : "";
146     }
147 }