Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / parser / ToscaParserInflatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 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.onap.vid.asdc.parser;
22
23 import static java.util.Collections.emptyMap;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.onap.vid.asdc.parser.ToscaParserInflatorTest.INFLATION_MODE.ByCustomizationId;
27 import static org.onap.vid.asdc.parser.ToscaParserInflatorTest.INFLATION_MODE.ByVersionId;
28
29 import com.google.common.collect.ImmutableMap;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.file.Path;
33 import java.util.Map;
34 import java.util.UUID;
35 import org.apache.commons.io.IOUtils;
36 import org.apache.log4j.LogManager;
37 import org.apache.log4j.Logger;
38 import org.jetbrains.annotations.NotNull;
39 import org.json.JSONObject;
40 import org.json.JSONTokener;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
45 import org.onap.vid.asdc.AsdcCatalogException;
46 import org.onap.vid.asdc.AsdcClient;
47 import org.onap.vid.asdc.beans.Service;
48 import org.onap.vid.asdc.local.LocalAsdcClient;
49 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
50 import org.onap.vid.model.ServiceModel;
51 import org.testng.annotations.BeforeClass;
52 import org.testng.annotations.BeforeMethod;
53 import org.testng.annotations.DataProvider;
54 import org.testng.annotations.Test;
55
56 public class ToscaParserInflatorTest {
57
58     private static final Logger log = LogManager.getLogger(ToscaParserInflatorTest.class);
59
60     @InjectMocks
61     private ToscaParserImpl2 toscaParserImpl2;
62
63     @Mock
64     private VidNotionsBuilder vidNotionsBuilder;
65
66     private AsdcClient asdcClient;
67
68     enum INFLATION_MODE {
69         ByVersionId, ByCustomizationId
70     }
71
72     @DataProvider
73     public static Object[][] inflationModes() {
74         return new Object[][] {
75             {ByVersionId}, {ByCustomizationId}
76         };
77     }
78
79     private String select(INFLATION_MODE mode, String byVersionId, String byCustomizationId) {
80         switch (mode) {
81             case ByVersionId:
82                 return byVersionId;
83             case ByCustomizationId:
84                 return byCustomizationId;
85             default:
86                 throw new IllegalStateException();
87         }
88     }
89
90     @BeforeClass
91     void init() throws IOException {
92
93         final InputStream asdcServicesFile = this.getClass().getClassLoader().getResourceAsStream("sdcservices.json");
94
95         final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
96         final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
97
98         asdcClient = new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
99
100     }
101
102     @BeforeMethod
103     public void initMocks() {
104         MockitoAnnotations.initMocks(this);
105     }
106
107
108     @Test(dataProvider = "inflationModes")
109     public void inflateFabricConfigurationModel_noIdsAreGiven(INFLATION_MODE inflationMode) throws Exception {
110         final String fabricConfigurationUuid = "12344bb4-a416-4b4e-997e-0059973630b9";
111         final Map<String, Names> inflated = inflateModelByUuid(fabricConfigurationUuid, inflationMode);
112
113         // see vf-with-annotation-csar.json
114         assertThat(inflated, is(ImmutableMap.of(
115             select(inflationMode, "ea81d6f7-0861-44a7-b7d5-d173b562c350", "41516cc6-5098-4b40-a619-f8d5f55fc4d8"),
116             doubleName("2017-488_PASQUALE-vPE 0"),
117
118             select(inflationMode, "a5d8df05-11cb-4351-96e0-b6d4168ea4df", "f3d97417-0c8d-424e-8ff7-b2eb4fbcecc3"),
119             new Names("2017488PasqualeVpe..PASQUALE_vRE_BV..module-1", "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vRE_BV..module-1"),
120
121             select(inflationMode, "b3e8b26e-cff0-49fc-a4e6-f3e16c8440fe", "6e410843-257c-46d9-ba8a-8d94e1362452"),
122             new Names("2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2", "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2"),
123
124             select(inflationMode, "040e591e-5d30-4e0d-850f-7266e5a8e013", "5c5f91f9-5e31-4120-b892-5536587ec258"),
125             new Names("2017488PasqualeVpe..PASQUALE_base_vPE_BV..module-0", "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_base_vPE_BV..module-0")
126         )));
127     }
128
129
130     @Test(dataProvider = "inflationModes")
131     public void inflateVlModel_allIdsAreGiven(INFLATION_MODE inflationMode) throws Exception {
132         final String fabricConfigurationUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
133         final Map<String, Names> inflated = inflateModelByUuid(fabricConfigurationUuid, inflationMode);
134
135         // see vl-csar.json
136         assertThat(inflated, is(ImmutableMap.of(
137             select(inflationMode, "af584529-d7f0-420e-a6f3-c38b689c030f", "664f8aa7-3989-46ac-81c0-dd72a8a63f26"),
138             doubleName("ExtVL 0")
139         )));
140     }
141
142     @NotNull
143     private Names doubleName(String modelCustomizationName) {
144         return new Names(modelCustomizationName, modelCustomizationName);
145     }
146
147     @Test(dataProvider = "inflationModes")
148     public void inflateConfigurationByPolicyFalseUuid_allIdsAreGiven(INFLATION_MODE inflationMode) throws Exception {
149         final String configurationByPolicyFalseUuid = "ee6d61be-4841-4f98-8f23-5de9da845544";
150         final Map<String, Names> inflated = inflateModelByUuid(configurationByPolicyFalseUuid, inflationMode);
151
152         // see policy-configuration-by-policy-false.json
153         // no relevant model here
154         assertThat(inflated, is(emptyMap()));
155     }
156
157     private Map<String, Names> inflateModelByUuid(String fabricConfigurationUuid, INFLATION_MODE inflationMode) throws SdcToscaParserException, AsdcCatalogException {
158         ServiceModel actualServiceModel = serviceModelByUuid(fabricConfigurationUuid);
159
160         ServiceModelInflator serviceModelInflator = new ServiceModelInflator();
161         return inflationMode == ByVersionId
162             ? serviceModelInflator.toNamesByVersionId(actualServiceModel)
163             : serviceModelInflator.toNamesByCustomizationId(actualServiceModel);
164     }
165
166     private ServiceModel serviceModelByUuid(String uuid) throws SdcToscaParserException, AsdcCatalogException {
167         final Path modelPath = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
168         final Service modelMetadata = asdcClient.getService(UUID.fromString(uuid));
169
170         return toscaParserImpl2.makeServiceModel(modelPath, modelMetadata);
171     }
172
173
174 }