8acfe6cfea8b30348d7a593fd8e919d014237c5a
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / CsarUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.be.tosca;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.model.ArtifactDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
38 import org.openecomp.sdc.common.api.ConfigurationSource;
39 import org.openecomp.sdc.common.impl.ExternalConfiguration;
40 import org.openecomp.sdc.common.impl.FSConfigurationSource;
41 import org.openecomp.sdc.exception.ResponseFormat;
42
43 import fj.data.Either;
44
45 public class CsarUtilsTest {
46         @Before
47         public void setup() {
48                 ExternalConfiguration.setAppName("catalog-be");
49
50                 // Init Configuration
51                 String appConfigDir = "src/test/resources/config/catalog-be";
52                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
53                                 appConfigDir);
54                 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
55         }
56
57         @Test
58         public void testValidateNonMetaArtifactHappyScenario() {
59                 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
60                 byte[] payloadData = "some payload data".getBytes();
61                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
62                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
63                                 payloadData, collectedWarningMessages);
64                 assertTrue(eitherNonMetaArtifact.isLeft());
65                 assertTrue(collectedWarningMessages.isEmpty());
66
67                 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
68                 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
69                 assertTrue(eitherNonMetaArtifact.isLeft());
70                 assertTrue(collectedWarningMessages.isEmpty());
71         }
72
73         @Test
74         public void testValidateNonMetaArtifactScenarioWithWarnnings() {
75                 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
76                 byte[] payloadData = "some payload data".getBytes();
77                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
78                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
79                                 payloadData, collectedWarningMessages);
80                 assertTrue(eitherNonMetaArtifact.isLeft());
81
82                 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
83                 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
84                 assertTrue(eitherNonMetaArtifact.isLeft());
85
86                 assertTrue(collectedWarningMessages.size() == 1);
87                 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
88         }
89
90         @Test
91         public void testValidateNonMetaArtifactUnhappyScenario() {
92                 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
93                 byte[] payloadData = "some payload data".getBytes();
94                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
95                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
96                                 payloadData, collectedWarningMessages);
97                 assertTrue(eitherNonMetaArtifact.isRight());
98                 assertTrue(!collectedWarningMessages.isEmpty());
99         }
100
101         private CsarUtils createTestSubject() {
102                 return new CsarUtils();
103         }
104
105         
106         @Test
107         public void testExtractVfcsArtifactsFromCsar() throws Exception {
108                 Map<String, byte[]> csar = null;
109                 Map<String, List<ArtifactDefinition>> result;
110
111                 // test 1
112                 csar = null;
113                 result = CsarUtils.extractVfcsArtifactsFromCsar(csar);
114                 Assert.assertEquals(new HashMap<String, List<ArtifactDefinition>>() , result);
115         }
116
117         
118         @Test
119         public void testHandleWarningMessages() throws Exception {
120                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
121
122                 // default test
123                 CsarUtils.handleWarningMessages(collectedWarningMessages);
124         }
125
126         
127         @Test
128         public void testValidateNonMetaArtifact() throws Exception {
129                 String artifactPath = "";
130                 byte[] payloadData = new byte[] { ' ' };
131                 Map<String, Set<List<String>>> collectedWarningMessages = null;
132                 Either<NonMetaArtifactInfo, Boolean> result;
133
134                 // default test
135                 result = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
136         }
137
138 }