Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / services / impl / etsi / ETSIServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.sdc.vendorsoftwareproduct.services.impl.etsi;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter;
27 import org.openecomp.core.utilities.file.FileContentHandler;
28 import org.openecomp.sdc.tosca.csar.Manifest;
29
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42 public class ETSIServiceImplTest {
43     private ETSIService etsiService;
44     private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" +
45             "CSAR-Version: 1.0\n" +
46             "Created-By: Kuku\n" +
47             "Entry-Definitions: MainServiceTemplate.yaml\n" +
48             "ETSI-Entry-Manifest: MainServiceTemplate.mf\n" +
49             "ETSI-Entry-Change-Log: MainServiceTemplate.log";
50     private String metaFile = "TOSCA-Meta-Version: 1.0\n" +
51             "CSAR-Version: 1.0\n" +
52             "Created-By: Kuku\n" +
53             "Entry-Definitions: MainServiceTemplate.yaml";
54
55     private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml";
56     private String finalOtherNonManoLocation = "Informational/OTHER/test.xml";
57
58     @Before
59     public void setUp() throws IOException {
60         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
61         Configuration configuration = yamlToObjectConverter.convert("src/test/resources",
62                 Configuration.class, "nonManoConfig.yaml");
63         etsiService = new ETSIServiceImpl(configuration);
64     }
65
66     @After
67     public void tearDown() {
68         etsiService = null;
69     }
70
71     @Test
72     public void testIsSol004TrueOrigin() throws IOException {
73         FileContentHandler fileContentHandler = new FileContentHandler();
74         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8));
75         assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
76     }
77
78     @Test
79     public void testIsSol004True() throws IOException  {
80         FileContentHandler fileContentHandler = new FileContentHandler();
81         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", sol004MetaFile.getBytes(StandardCharsets.UTF_8));
82         assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
83     }
84
85     @Test
86     public void testIsSol004False() throws IOException  {
87         FileContentHandler fileContentHandler = new FileContentHandler();
88         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", metaFile.getBytes(StandardCharsets.UTF_8));
89         assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
90     }
91
92     @Test
93     public void testIsSol004FalseWithNull() throws IOException  {
94         FileContentHandler fileContentHandler = new FileContentHandler();
95         assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
96     }
97
98     @Test
99     public void testMoveNonManoFileToArtifactFolder() throws IOException {
100         Map<String, List<String>> nonManoSources = new HashMap<>();
101         List<String> sources = new ArrayList<>();
102         sources.add("Some/test.xml");
103         nonManoSources.put("Some", sources);
104         FileContentHandler fileContentHandler = new FileContentHandler();
105         fileContentHandler.addFile("Some/test.xml", new byte[1]);
106         Manifest manifest = mock(Manifest.class);
107         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
108         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest);
109         assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
110     }
111
112     @Test
113     public void testMoveNonManoFileToArtifactFolderFileNotInFolder() throws IOException {
114         Map<String, List<String>> nonManoSources = new HashMap<>();
115         List<String> sources = new ArrayList<>();
116         sources.add("test.xml");
117         nonManoSources.put("foo", sources);
118         FileContentHandler fileContentHandler = new FileContentHandler();
119         fileContentHandler.addFile("test.xml", new byte[1]);
120         Manifest manifest = mock(Manifest.class);
121         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
122         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest);
123         assertFalse(fileContentHandler.containsFile(finalOtherNonManoLocation));
124     }
125
126     @Test
127     public void testMoveNonManoFileToArtifactFolderNoMove() throws IOException {
128         Map<String, List<String>> nonManoSources = new HashMap<>();
129         List<String> sources = new ArrayList<>();
130         sources.add(finalNonManoLocation);
131         nonManoSources.put("Some", sources);
132         FileContentHandler fileContentHandler = new FileContentHandler();
133         fileContentHandler.addFile(finalNonManoLocation, new byte[1]);
134         Manifest manifest = mock(Manifest.class);
135         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
136         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest);
137         assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
138     }
139
140     @Test
141     public void testMoveNonManoFileToArtifactFolderMoveToKeyFolder() throws IOException {
142         Map<String, List<String>> nonManoSources = new HashMap<>();
143         List<String> sources = new ArrayList<>();
144         sources.add("Artifacts/Deployment/test.xml");
145         nonManoSources.put("Some", sources);
146         FileContentHandler fileContentHandler = new FileContentHandler();
147         fileContentHandler.addFile("Artifacts/Deployment/test.xml", new byte[1]);
148         Manifest manifest = mock(Manifest.class);
149         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
150         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest);
151         assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
152     }
153 }