04dbbc0c529da278d1270bc1330f806a599ccd03
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / SdcApiTest.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.api;
22
23 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
24 import static net.javacrumbs.jsonunit.JsonMatchers.jsonStringEquals;
25 import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.core.Is.is;
28 import static org.hamcrest.core.IsNot.not;
29 import static org.testng.Assert.assertFalse;
30 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND;
31 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET;
32 import static vid.automation.test.services.SimulatorApi.registerExpectation;
33 import static vid.automation.test.services.SimulatorApi.registerExpectationFromPresets;
34 import static vid.automation.test.utils.ReadFile.loadResourceAsString;
35
36 import com.fasterxml.jackson.databind.JsonNode;
37 import com.google.common.collect.ImmutableList;
38 import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
39 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceMetadataGet;
40 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGet;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.testng.Assert;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47 import vid.automation.test.infra.FeatureTogglingTest;
48 import vid.automation.test.infra.Features;
49
50 public class SdcApiTest extends BaseApiTest {
51
52     private static final String SDC_GET_SERVICE_MODEL = "/rest/models/services/";
53
54
55     private static final String A_LA_CARTE_INSTANTIATION_TYPE_UUID = "4d71990b-d8ad-4510-ac61-496288d9078e";
56     private static final String A_LA_CARTE_INSTANTIATION_TYPE_INVARIANT_UUID = "d27e42cf-087e-4d31-88ac-6c4b7585f800";
57     private static final String A_LA_CARTE_INSTANTIATION_TYPE_FILE_PATH = "csar15782222_instantiationTypeAlacarte_invariantUUIDMacro.zip";
58     private static final String A_LA_CARTE_INSTANTIATION_TYPE_EXPECTED_RESPONSE = "sdcApiTest/aLaCarteInstantiationTypeResponse.json";
59
60
61     private static final String MACRO_INSTANTIATION_TYPE_FILE_PATH = "csar15782222_instantiationTypeMacro_invariantUUIDAlacarte.zip";
62     private static final String MACRO_INSTANTIATION_TYPE_UUID = "4d71990b-d8ad-4510-ac61-496288d9078e";
63     private static final String MACRO_INSTANTIATION_TYPE_INVARIANT_UUID = "a8dcd72d-d44d-44f2-aa85-53aa9ca99cba";
64     private static final String MACRO_INSTANTIATION_TYPE_EXPECTED_RESPONSE = "sdcApiTest/macroInstantiationTypeResponse.json";
65
66
67     private static final String EMPTY_INSTANTIATION_TYPE_FILE_PATH = "csar15782222_instantiationTypeEmpty_invariantUUIDAlacarte.zip";
68     private static final String EMPTY_INSTANTIATION_TYPE_EXPECTED_RESPONSE = "sdcApiTest/emptyInstantiationTypeResponse.json";
69
70     private static final String BOTH_INSTANTIATION_TYPE_FILE_PATH = "csar15782222_instantiationTypeBoth_invariantUUIDAlacarte.zip";
71
72
73     private static final String MIN_MAX_INITIAL_UUID = "43f13072-fe50-496b-b673-7af075d10143";
74     private static final String MIN_MAX_INITIAL_INVARIANT_UUID = "35fb95d8-d1f0-4e46-99ac-e01b423e8e3f";
75     private static final String MIN_MAX_INITIAL_FILE_PATH = "min_max_initial_vfModule_csar_v4.0.zip";
76
77     private static final String MIN_MAX_INITIAL_UUID_OLD_CSAR = "245562de-3984-49ef-a708-6c9d7cfcabd1";
78     private static final String MIN_MAX_INITIAL_INVARIANT_UUID_OLD_CSAR = "24216d6-71d0-41c8-ac81-0c5acfee514a";
79     private static final String MIN_MAX_INITIAL_FILE_PATH_OLD_CSAR = "service-VflorenceRvpmsFeAic3011217Svc-csar.csar.zip";
80
81     private static final String GROUPING_SERVICE_ROLE_FILE_PATH = "csar15782222_instantiationTypeAlacarte_VnfGrouping.zip";
82     private static final String GROUPING_SERVICE_ROLE_UUID = "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc";
83     private static final String GROUPING_SERVICE_ROLE_INVARIANT_UUID = "7ee41ce4-4827-44b0-a48e-2707a59905d2";
84     private static final String GROUPING_SERVICE_ROLE_EXPECTED_RESPONSE = "VnfGroup/groupingServiceRoleResponse.json";
85
86     @BeforeClass
87     public void login() {
88         super.login();
89     }
90
91     @BeforeMethod
92     public void invalidateTheCache(){
93         if(Features.FLAG_SERVICE_MODEL_CACHE.isActive()) {
94             restTemplate.postForObject(uri + "/rest/models/reset", "", Object.class);
95         }
96     }
97
98     @Test
99     public void getServiceModelALaCarteInstantiation() {
100         registerToSimulatorWithPresets(A_LA_CARTE_INSTANTIATION_TYPE_UUID, A_LA_CARTE_INSTANTIATION_TYPE_INVARIANT_UUID, A_LA_CARTE_INSTANTIATION_TYPE_FILE_PATH);
101         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + A_LA_CARTE_INSTANTIATION_TYPE_UUID), String.class);
102         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
103         String aLaCarteInstantiationTypeExpectedResponse = loadResourceAsString(A_LA_CARTE_INSTANTIATION_TYPE_EXPECTED_RESPONSE);
104         assertThat(response.getBody(), jsonEquals(aLaCarteInstantiationTypeExpectedResponse)
105             .when(IGNORING_ARRAY_ORDER)
106             .whenIgnoringPaths("service.vidNotions.instantiationUI"));
107     }
108
109
110     @Test
111     public void getServiceModelMacroInstantiation() {
112         registerToSimulatorWithPresets(MACRO_INSTANTIATION_TYPE_UUID, MACRO_INSTANTIATION_TYPE_INVARIANT_UUID, MACRO_INSTANTIATION_TYPE_FILE_PATH);
113         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + MACRO_INSTANTIATION_TYPE_UUID), String.class);
114         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
115         String macroInstantiationTypeExpectedResponse = loadResourceAsString(MACRO_INSTANTIATION_TYPE_EXPECTED_RESPONSE);
116         assertThat(response.getBody(), jsonEquals(macroInstantiationTypeExpectedResponse)
117             .when(IGNORING_ARRAY_ORDER)
118             .whenIgnoringPaths("service.vidNotions.viewEditUI"));
119     }
120
121
122     @Test
123     public void getServiceModelWithoutInstantiationType(){
124         registerToSimulatorWithPresets(MACRO_INSTANTIATION_TYPE_UUID, MACRO_INSTANTIATION_TYPE_INVARIANT_UUID, EMPTY_INSTANTIATION_TYPE_FILE_PATH);
125         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + MACRO_INSTANTIATION_TYPE_UUID), String.class);
126         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
127         String emptyInstantiationTypeExpectedResponse = loadResourceAsString(EMPTY_INSTANTIATION_TYPE_EXPECTED_RESPONSE);
128         assertThat("The response is in the format of JSON", response.getBody(), is(jsonStringEquals(turnOffInstantiationUI(emptyInstantiationTypeExpectedResponse))));
129     }
130
131     @Test
132     public void getServiceModelBothInstantiationType(){
133         registerToSimulatorWithPresets(MACRO_INSTANTIATION_TYPE_UUID, MACRO_INSTANTIATION_TYPE_INVARIANT_UUID, BOTH_INSTANTIATION_TYPE_FILE_PATH);
134         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + MACRO_INSTANTIATION_TYPE_UUID), String.class);
135         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
136         String macroInstantiationTypeExpectedResponse = loadResourceAsString(MACRO_INSTANTIATION_TYPE_EXPECTED_RESPONSE);
137         assertThat(response.getBody(), jsonEquals(macroInstantiationTypeExpectedResponse)
138             .when(IGNORING_ARRAY_ORDER)
139             .whenIgnoringPaths("service.vidNotions.viewEditUI"));
140     }
141
142     @Test
143     public void getServiceModelWithGroupsAndCheckMinMaxInitialParams(){
144         registerToSimulatorWithPresets(MIN_MAX_INITIAL_UUID, MIN_MAX_INITIAL_INVARIANT_UUID, MIN_MAX_INITIAL_FILE_PATH);
145         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + MIN_MAX_INITIAL_UUID), String.class);
146         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
147         String minMaxInitialExpectedResponse = loadResourceAsString("sdcApiTest/minMaxInitialExpectedResponse.json");
148         assertThat("The response is in the format of JSON", response.getBody(), is(jsonStringEquals(turnOffInstantiationUI(minMaxInitialExpectedResponse))));
149     }
150
151     @Test
152     public void getServiceModelWithGroupsAndCheckMinMaxInitialParamsOldCsar(){
153         registerToSimulatorWithPresets(MIN_MAX_INITIAL_UUID_OLD_CSAR, MIN_MAX_INITIAL_INVARIANT_UUID_OLD_CSAR, MIN_MAX_INITIAL_FILE_PATH_OLD_CSAR);
154         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + MIN_MAX_INITIAL_UUID_OLD_CSAR), String.class);
155         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
156         String minMaxInitialExpectedResponseOldCsar = loadResourceAsString("sdcApiTest/minMaxInitialExpectedResponseOldCsar.json");
157         assertThat("The response is in the format of JSON", response.getBody(), is(jsonStringEquals(minMaxInitialExpectedResponseOldCsar)));
158     }
159
160     @Test
161     @FeatureTogglingTest(Features.FLAG_1902_VNF_GROUPING)
162     public void getServiceModelWithServiceRoleGrouping(){
163         registerToSimulatorWithPresets(GROUPING_SERVICE_ROLE_UUID, GROUPING_SERVICE_ROLE_INVARIANT_UUID, GROUPING_SERVICE_ROLE_FILE_PATH);
164         ResponseEntity<String> response = restTemplate.getForEntity(buildUri(SDC_GET_SERVICE_MODEL + GROUPING_SERVICE_ROLE_UUID), String.class);
165         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
166         String groupingServiceRoleExpectedResponse = loadResourceAsString(GROUPING_SERVICE_ROLE_EXPECTED_RESPONSE);
167         assertThat("The response is in the format of JSON", response.getBody(), is(jsonStringEquals(groupingServiceRoleExpectedResponse)));
168     }
169
170     private void registerToSimulatorWithPresets(String uuid, String invariantUuid, String pathPath){
171         ImmutableList<BasePreset> presets = ImmutableList.of(
172                 new PresetSDCGetServiceToscaModelGet(uuid, pathPath),
173                 new PresetSDCGetServiceMetadataGet(uuid,invariantUuid, pathPath));
174         registerExpectationFromPresets(presets, CLEAR_THEN_SET);
175     }
176
177     private String turnOffInstantiationUI(String expectedJson) {
178         if (!Features.FLAG_5G_IN_NEW_INSTANTIATION_UI.isActive()) {
179             // replaces the instantiationUI field-value with "legacy", whatever it was
180             return expectedJson.replaceFirst("(\"instantiationUI\": *\")[^\"]*(\",)", "$1legacy$2");
181         } else {
182             return expectedJson;
183         }
184     }
185
186     @Test
187     public void withModelFromE2eWithToscaParserButNewFlow_requestModels_expectVnfRelatedVfModulesNotNull() {
188
189         /*
190         We had a problem that this exact model vnfs returned with no vfModules and
191         volumeGroups, because a 'isNewFlow' value in org.onap.vid.asdc.parser.ToscaParserImpl
192         was always false because a coding error.
193          */
194         registerExpectation("get_sdc_catalog_services_VflorenceRvpmsFeAic3011217Svc.json", CLEAR_THEN_SET);
195         registerExpectation("create_new_instance/aai_get_full_subscribers.json", APPEND);
196
197         final JsonNode response = restTemplate.getForObject(uri + "/rest/models/services/" + "245562de-3984-49ef-a708-6c9d7cfcabd1", JsonNode.class);
198
199         // using json-pointers instead of path, because vnf name has
200         // dots and spaces
201         final String myVnf = "vFLORENCEvProbe_FE_AIC3-11.2.1_VF 1";
202         final String base = "/vnfs/" + myVnf;
203
204         assertFalse(response.at(base).isMissingNode(),
205                 "test relies on '" + myVnf + "' to be in model; got: " + response);
206
207         assertThat("vfModules under '" + myVnf + "' must not be empty; got: " + response,
208                 response.at(base + "/vfModules").size(), is(not(0)));
209
210         assertThat("volumeGroups under '" + myVnf + "' must not be empty; got: " + response,
211                 response.at(base + "/volumeGroups").size(), is(not(0)));
212
213     }
214 }