c5fbf9d6c029b095942e6f18568a1a735dd4a653
[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 java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Arrays;
32 import java.util.ArrayList;
33 import java.util.Comparator;
34 import java.util.Comparator;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.List;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import java.util.stream.Collectors;
42
43 import static org.junit.Assert.*;
44
45 public class RelationshipSchemaTest {
46
47     final static Pattern rulesFilePattern = Pattern.compile("DbEdgeRules(.*).json");
48     final static Pattern propsFilePattern = Pattern.compile("edge_properties_(.*).json");
49     final static Pattern versionPattern = Pattern.compile(".*(v\\d+).json");
50
51
52     @Test
53     public void shouldLoadAllTheVersionsInDirectory() throws Exception {
54         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
55         loadRelations(versionContextMap);
56         assertTrue(versionContextMap.keySet().size() >= 0);
57     }
58
59     @Test
60     public void shouldContainValidTypes() throws Exception {
61         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
62         loadRelations(versionContextMap);
63         assertTrue(versionContextMap.get("v11").isValidType("groupsResourcesIn"));
64         assertTrue(versionContextMap.get("v11").isValidType("uses"));
65         assertFalse(versionContextMap.get("v11").isValidType("notValidType"));
66     }
67
68     @Test
69     public void shouldLookUpByRelation() throws Exception {
70         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
71         loadRelations(versionContextMap);
72         assertNotNull(versionContextMap.get("v11").lookupRelation("availability-zone:complex:groupsResourcesIn"));
73         assertTrue(versionContextMap.get("v11")
74                 .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("usesResource"));
75     }
76
77     @Test
78     public void shouldLookUpByRelationType() throws Exception {
79         Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
80         loadRelations(versionContextMap);
81         assertNotNull(versionContextMap.get("v11").lookupRelationType("groupsResourcesIn"));
82         assertTrue(versionContextMap.get("v11")
83                 .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("usesResource"));
84     }
85
86     private void loadRelations(Map<String, RelationshipSchema> map){
87         ClassLoader classLoader = getClass().getClassLoader();
88         File dir = new File(classLoader.getResource("model").getFile());
89         File[] allFiles = dir.listFiles((d, name) ->
90                 (propsFilePattern.matcher(name).matches() || rulesFilePattern.matcher(name).matches()));
91
92         Arrays.stream(allFiles).sorted(Comparator.comparing(File::getName))
93                 .collect(Collectors.groupingBy(f -> myMatcher(versionPattern, f.getName())))
94                 .forEach((e, f) -> map.put(e, jsonFilesLoader(f)));
95
96     }
97
98
99     private RelationshipSchema jsonFilesLoader (List<File> files) {
100         List<String> fileContents = new ArrayList<>();
101         RelationshipSchema rsSchema = null;
102         for (File f : files) {
103             fileContents.add(jsonToString(f));
104         }
105
106         try {
107             rsSchema = new RelationshipSchema(fileContents);
108         } catch (CrudException e) {
109             e.printStackTrace();
110         } catch (IOException e) {
111             e.printStackTrace();
112         }
113
114         return rsSchema;
115     }
116
117     private String jsonToString (File file) {
118         InputStream inputStream = null;
119         String content = null;
120         HashMap<String,Object> result = null;
121
122         try {
123             inputStream = new FileInputStream(file);
124             content =  IOUtils.toString(inputStream, "UTF-8");
125         } catch (IOException e) {
126             e.printStackTrace();
127         }
128
129         return content;
130     }
131
132     private String myMatcher (Pattern p, String s) {
133         Matcher m = p.matcher(s);
134         return m.matches() ? m.group(1) : "";
135     }
136 }