4c5cc97877b2fc6bcd4f98c63abbbf742becba08
[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 com.google.common.collect.ImmutableMap;
24 import org.apache.commons.io.IOUtils;
25 import org.apache.log4j.LogManager;
26 import org.apache.log4j.Logger;
27 import org.jetbrains.annotations.NotNull;
28 import org.json.JSONObject;
29 import org.json.JSONTokener;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
34 import org.onap.vid.asdc.AsdcCatalogException;
35 import org.onap.vid.asdc.AsdcClient;
36 import org.onap.vid.asdc.beans.Service;
37 import org.onap.vid.asdc.local.LocalAsdcClient;
38 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
39 import org.onap.vid.model.ServiceModel;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.nio.file.Path;
47 import java.util.Map;
48 import java.util.UUID;
49
50 import static java.util.Collections.emptyMap;
51 import static org.hamcrest.Matchers.is;
52 import static org.junit.Assert.assertThat;
53
54 public class ToscaParserInflatorTest {
55
56     private static final Logger log = LogManager.getLogger(ToscaParserInflatorTest.class);
57
58     @InjectMocks
59     private ToscaParserImpl2 toscaParserImpl2;
60
61     @Mock
62     private VidNotionsBuilder vidNotionsBuilder;
63
64     private AsdcClient asdcClient;
65
66     @BeforeClass
67     void init() throws IOException {
68
69         final InputStream asdcServicesFile = this.getClass().getClassLoader().getResourceAsStream("sdcservices.json");
70
71         final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
72         final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
73
74         asdcClient = new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
75
76     }
77
78     @BeforeMethod
79     public void initMocks() {
80         MockitoAnnotations.initMocks(this);
81     }
82
83
84     @Test
85     public void inflateFabricConfigurationModel_allIdsAreGiven() throws Exception {
86         final String fabricConfigurationUuid = "90fe6842-aa76-4b68-8329-5c86ff564407";
87         final Map<String, Names> inflated = inflateModelByUuid(fabricConfigurationUuid);
88
89         // see vf-with-annotation-csar.json
90         assertThat(inflated, is(ImmutableMap.of(
91                 "8df1892c-377d-460b-8a8d-fc8a116e9d92", doubleName("201712-488_ADIOD-vPE-1 0"),
92                 "8d521692-7661-4296-b77e-a2058bb62e87", new Names("201712488AdiodVpe1..ADIOD_vRE_BV..module-1", "201712488_adiodvpe10..201712488AdiodVpe1..ADIOD_vRE_BV..module-1"),
93                 "79fbee20-7fba-4166-ae4b-b94c869e7d8b", new Names("201712488AdiodVpe1..ADIOD_vPFE_BV..module-2","201712488_adiodvpe10..201712488AdiodVpe1..ADIOD_vPFE_BV..module-2"),
94                 "806505b8-7a7c-47a2-acef-b4d26fe95a92", new Names("201712488AdiodVpe1..ADIOD_base_vPE_BV..module-0","201712488_adiodvpe10..201712488AdiodVpe1..ADIOD_base_vPE_BV..module-0")
95         )));
96     }
97
98
99     @Test
100     public void inflateVlModel_allIdsAreGiven() throws Exception {
101         final String fabricConfigurationUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
102         final Map<String, Names> inflated = inflateModelByUuid(fabricConfigurationUuid);
103
104         // see vl-csar.json
105         assertThat(inflated, is(ImmutableMap.of(
106                 "af584529-d7f0-420e-a6f3-c38b689c030f", doubleName("ExtVL 0")
107         )));
108     }
109
110     @NotNull
111     private Names doubleName(String modelCustomizationName) {
112         return new Names(modelCustomizationName, modelCustomizationName);
113     }
114
115     @Test
116     public void inflateConfigurationByPolicyFalseUuid_allIdsAreGiven() throws Exception {
117         final String configurationByPolicyFalseUuid = "ee6d61be-4841-4f98-8f23-5de9da845544";
118         final Map<String, Names> inflated = inflateModelByUuid(configurationByPolicyFalseUuid);
119
120         // see policy-configuration-by-policy-false.json
121         // no relevant model here
122         assertThat(inflated, is(emptyMap()));
123     }
124
125     private Map<String, Names> inflateModelByUuid(String fabricConfigurationUuid) throws SdcToscaParserException, AsdcCatalogException {
126         ServiceModel actualServiceModel = serviceModelByUuid(fabricConfigurationUuid);
127
128         ServiceModelInflator serviceModelInflator = new ServiceModelInflator();
129         return serviceModelInflator.toNamesByVersionId(actualServiceModel);
130     }
131
132     private ServiceModel serviceModelByUuid(String uuid) throws SdcToscaParserException, AsdcCatalogException {
133         final Path modelPath = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
134         final Service modelMetadata = asdcClient.getService(UUID.fromString(uuid));
135
136         return toscaParserImpl2.makeServiceModel(modelPath, modelMetadata);
137     }
138
139
140 }