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