EdgeRules throws descriptive error on invalid rule
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / dbmodel / DbEdgeRulesConverterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.dbmodel;
22
23 import com.google.common.collect.ImmutableSetMultimap;
24 import com.google.common.collect.Multimap;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Test;
27
28 import java.io.*;
29 import java.util.Map.Entry;
30
31 import static org.junit.Assert.*;
32
33 public class DbEdgeRulesConverterTest {
34         
35         @Test
36         public void testExtractData() {
37                 Multimap<String, String> EdgeRules = new ImmutableSetMultimap.Builder<String, String>()
38                                 .putAll("availability-zone|complex",
39                                                 "groupsResourcesIn,OUT,Many2Many,false,true,false,true").build();
40                 
41                 DbEdgeRulesConverter dberConverter = new DbEdgeRulesConverter();
42                 
43                 for (Entry<String, String> r : EdgeRules.entries()) {
44                         EdgeRuleBean bean = dberConverter.extractData(r);
45                         assertEquals("from availability-zone", "availability-zone", bean.getFrom());
46                         assertEquals("to complex", "complex", bean.getTo());
47                         assertEquals("label", "groupsResourcesIn", bean.getLabel());
48                         assertEquals("direction", "OUT", bean.getDirection());
49                         assertEquals("multiplicity", "Many2Many", bean.getMultiplicity());
50                         assertEquals("isParent", "false", bean.getIsParent());
51                         assertEquals("usesResource", "true", bean.getUsesResource());
52                         assertEquals("hasDelTarget", "false", bean.getHasDelTarget());
53                         assertEquals("SVC-INFRA", "true", bean.getSvcInfra());
54                 }
55         }
56
57         @Test
58         public void testConvert(){
59                 DbEdgeRulesConverter dberCon = new DbEdgeRulesConverter();
60                 String dest = "src/test/resources/dbEdgeRulesConversion";
61                 String outFile = dest + "/testOutput.json";
62                 
63                 Multimap<String, String> EdgeRules = new ImmutableSetMultimap.Builder<String, String>()
64                                 .putAll("foo|bar",
65                                                 "has,OUT,Many2Many,false,false,false,false")
66                                 .putAll("baz|quux",
67                                                 "treatsVeryKindly,IN,One2One,true,true,true,true")
68                                 .build();
69
70                 try {
71                         dberCon.setup(dest);
72                         File result = new File(outFile);
73                         //Add delete hook to delete the temporary result file on exit/
74                         result.deleteOnExit();
75                         FileOutputStream writeStream = new FileOutputStream(result);
76                         Writer writer = new OutputStreamWriter(writeStream);
77                         dberCon.convert(EdgeRules, writer);
78                         File compare = new File("src/test/resources/dbEdgeRulesConversion/conversionTestCompare.json");
79                         assertTrue(FileUtils.contentEquals(result, compare));
80                         writer.close();
81                 } catch (IOException e) {
82                         e.printStackTrace();
83                         fail("IOException on setup");
84                 }
85         }
86 }