2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.schema;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.Test;
25 import org.onap.crud.exception.CrudException;
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;
38 import java.util.List;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import java.util.stream.Collectors;
43 import static org.junit.Assert.*;
45 public class RelationshipSchemaTest {
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");
53 public void shouldLoadAllTheVersionsInDirectory() throws Exception {
54 Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
55 loadRelations(versionContextMap);
56 assertTrue(versionContextMap.keySet().size() >= 0);
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"));
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("prevent-delete"));
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("prevent-delete"));
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()));
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)));
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));
107 rsSchema = new RelationshipSchema(fileContents);
108 } catch (CrudException e) {
110 } catch (IOException e) {
117 private String jsonToString (File file) {
118 InputStream inputStream = null;
119 String content = null;
120 HashMap<String,Object> result = null;
123 inputStream = new FileInputStream(file);
124 content = IOUtils.toString(inputStream, "UTF-8");
125 } catch (IOException e) {
132 private String myMatcher (Pattern p, String s) {
133 Matcher m = p.matcher(s);
134 return m.matches() ? m.group(1) : "";