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