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;
27 import com.att.aft.dme2.internal.apache.commons.lang.ArrayUtils;
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;
40 import java.util.List;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43 import java.util.stream.Collectors;
45 import static org.junit.Assert.*;
47 public class RelationshipSchemaTest {
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");
55 public void shouldLoadAllTheVersionsInDirectory() throws Exception {
56 Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
57 loadRelations(versionContextMap);
58 assertTrue(versionContextMap.keySet().size() >= 0);
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"));
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"));
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"));
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()));
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);
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)));
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));
116 if (fileContents.size() == 2) {
117 rsSchema = new RelationshipSchema(fileContents);
119 } catch (CrudException e) {
121 } catch (IOException e) {
128 private String jsonToString (File file) {
129 InputStream inputStream = null;
130 String content = null;
131 HashMap<String,Object> result = null;
134 inputStream = new FileInputStream(file);
135 content = IOUtils.toString(inputStream, "UTF-8");
136 } catch (IOException e) {
143 private String myMatcher (Pattern p, String s) {
144 Matcher m = p.matcher(s);
145 return m.matches() ? m.group(1) : "";