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