3a1d9cc1af8d29f2927e4bc82ad891a90661209b
[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 static org.junit.Assert.*;
24
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 import java.util.Map.Entry;
31
32 import org.apache.commons.io.FileUtils;
33 import org.junit.Test;
34
35 import com.google.common.collect.ImmutableSetMultimap;
36 import com.google.common.collect.Multimap;
37
38 import freemarker.template.TemplateException;
39
40 public class DbEdgeRulesConverterTest {
41         
42         @Test
43         public void testExtractData() {
44                 Multimap<String, String> EdgeRules = new ImmutableSetMultimap.Builder<String, String>()
45                                 .putAll("availability-zone|complex",
46                                                 "groupsResourcesIn,OUT,Many2Many,false,true,false,true").build();
47                 
48                 DbEdgeRulesConverter dberConverter = new DbEdgeRulesConverter();
49                 
50                 for (Entry<String, String> r : EdgeRules.entries()) {
51                         EdgeRuleBean bean = dberConverter.extractData(r);
52                         assertEquals("from availability-zone", "availability-zone", bean.getFrom());
53                         assertEquals("to complex", "complex", bean.getTo());
54                         assertEquals("label", "groupsResourcesIn", bean.getLabel());
55                         assertEquals("direction", "OUT", bean.getDirection());
56                         assertEquals("multiplicity", "Many2Many", bean.getMultiplicity());
57                         assertEquals("isParent", "false", bean.getIsParent());
58                         assertEquals("usesResource", "true", bean.getUsesResource());
59                         assertEquals("hasDelTarget", "false", bean.getHasDelTarget());
60                         assertEquals("SVC-INFRA", "true", bean.getSvcInfra());
61                 }
62         }
63
64         @Test
65         public void testConvert(){
66                 DbEdgeRulesConverter dberCon = new DbEdgeRulesConverter();
67                 String dest = "src/test/resources/dbEdgeRulesConversion";
68                 String outFile = dest + "/testOutput.json";
69                 
70                 Multimap<String, String> EdgeRules = new ImmutableSetMultimap.Builder<String, String>()
71                                 .putAll("foo|bar",
72                                                 "has,OUT,Many2Many,false,false,false,false")
73                                 .putAll("baz|quux",
74                                                 "treatsVeryKindly,IN,One2One,true,true,true,true")
75                                 .build();
76
77                 try {
78                         dberCon.setup(dest);
79                         File result = new File(outFile);
80                         //Add delete hook to delete the temporary result file on exit/
81                         result.deleteOnExit();
82                         FileOutputStream writeStream = new FileOutputStream(result);
83                         Writer writer = new OutputStreamWriter(writeStream);
84                         dberCon.convert(EdgeRules, writer);
85                         File compare = new File("src/test/resources/dbEdgeRulesConversion/conversionTestCompare.json");
86                         assertTrue(FileUtils.contentEquals(result, compare));
87                         writer.close();
88                 } catch (IOException e) {
89                         e.printStackTrace();
90                         fail("IOException on setup");
91                 }
92         }
93 }