d6c080d555cdfb802d44e3f823071350efba49aa
[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
27 import com.google.common.collect.ImmutableMap;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Path;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.apache.commons.io.IOUtils;
34 import org.apache.log4j.LogManager;
35 import org.apache.log4j.Logger;
36 import org.jetbrains.annotations.NotNull;
37 import org.json.JSONObject;
38 import org.json.JSONTokener;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
43 import org.onap.vid.asdc.AsdcCatalogException;
44 import org.onap.vid.asdc.AsdcClient;
45 import org.onap.vid.asdc.beans.Service;
46 import org.onap.vid.asdc.local.LocalAsdcClient;
47 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
48 import org.onap.vid.model.ServiceModel;
49 import org.testng.annotations.BeforeClass;
50 import org.testng.annotations.BeforeMethod;
51 import org.testng.annotations.Test;
52
53 public class ToscaParserInflatorTest {
54
55     private static final Logger log = LogManager.getLogger(ToscaParserInflatorTest.class);
56
57     @InjectMocks
58     private ToscaParserImpl2 toscaParserImpl2;
59
60     @Mock
61     private VidNotionsBuilder vidNotionsBuilder;
62
63     private AsdcClient asdcClient;
64
65     @BeforeClass
66     void init() throws IOException {
67
68         final InputStream asdcServicesFile = this.getClass().getClassLoader().getResourceAsStream("sdcservices.json");
69
70         final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
71         final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
72
73         asdcClient = new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
74
75     }
76
77     @BeforeMethod
78     public void initMocks() {
79         MockitoAnnotations.initMocks(this);
80     }
81
82
83     @Test
84     public void inflateFabricConfigurationModel_allIdsAreGiven() throws Exception {
85         final String fabricConfigurationUuid = "12344bb4-a416-4b4e-997e-0059973630b9";
86         final Map<String, Names> inflated = inflateModelByUuid(fabricConfigurationUuid);
87
88         // see vf-with-annotation-csar.json
89         assertThat(inflated, is(ImmutableMap.of(
90                 "ea81d6f7-0861-44a7-b7d5-d173b562c350", doubleName("2017-488_PASQUALE-vPE 0"),
91                 "a5d8df05-11cb-4351-96e0-b6d4168ea4df", new Names("2017488PasqualeVpe..PASQUALE_vRE_BV..module-1", "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vRE_BV..module-1"),
92                 "b3e8b26e-cff0-49fc-a4e6-f3e16c8440fe", new Names("2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2","2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2"),
93                 "040e591e-5d30-4e0d-850f-7266e5a8e013", new Names("2017488PasqualeVpe..PASQUALE_base_vPE_BV..module-0","2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_base_vPE_BV..module-0")
94         )));
95     }
96
97
98     @Test
99     public void inflateVlModel_allIdsAreGiven() throws Exception {
100         final String fabricConfigurationUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
101         final Map<String, Names> inflated = inflateModelByUuid(fabricConfigurationUuid);
102
103         // see vl-csar.json
104         assertThat(inflated, is(ImmutableMap.of(
105                 "af584529-d7f0-420e-a6f3-c38b689c030f", doubleName("ExtVL 0")
106         )));
107     }
108
109     @NotNull
110     private Names doubleName(String modelCustomizationName) {
111         return new Names(modelCustomizationName, modelCustomizationName);
112     }
113
114     @Test
115     public void inflateConfigurationByPolicyFalseUuid_allIdsAreGiven() throws Exception {
116         final String configurationByPolicyFalseUuid = "ee6d61be-4841-4f98-8f23-5de9da845544";
117         final Map<String, Names> inflated = inflateModelByUuid(configurationByPolicyFalseUuid);
118
119         // see policy-configuration-by-policy-false.json
120         // no relevant model here
121         assertThat(inflated, is(emptyMap()));
122     }
123
124     private Map<String, Names> inflateModelByUuid(String fabricConfigurationUuid) throws SdcToscaParserException, AsdcCatalogException {
125         ServiceModel actualServiceModel = serviceModelByUuid(fabricConfigurationUuid);
126
127         ServiceModelInflator serviceModelInflator = new ServiceModelInflator();
128         return serviceModelInflator.toNamesByVersionId(actualServiceModel);
129     }
130
131     private ServiceModel serviceModelByUuid(String uuid) throws SdcToscaParserException, AsdcCatalogException {
132         final Path modelPath = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
133         final Service modelMetadata = asdcClient.getService(UUID.fromString(uuid));
134
135         return toscaParserImpl2.makeServiceModel(modelPath, modelMetadata);
136     }
137
138
139 }