Fix for Penetration test _ Session and cookie management
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / ProbeApiTest.java
1 package org.onap.vid.api;
2
3 import static org.hamcrest.CoreMatchers.anyOf;
4 import static org.hamcrest.CoreMatchers.both;
5 import static org.hamcrest.CoreMatchers.endsWith;
6 import static org.hamcrest.CoreMatchers.equalTo;
7 import static org.hamcrest.CoreMatchers.is;
8 import static org.hamcrest.CoreMatchers.startsWith;
9 import static org.hamcrest.MatcherAssert.assertThat;
10 import static org.testng.Assert.assertEquals;
11 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET;
12
13 import com.google.common.collect.ImmutableList;
14 import java.lang.reflect.Method;
15 import java.util.Collection;
16 import java.util.List;
17 import org.apache.commons.lang3.StringUtils;
18 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
19 import org.apache.commons.lang3.builder.ToStringStyle;
20 import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
21 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
22 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGetInvalidResponse;
23 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestsManyStatusesGet;
24 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestsManyStatusesInvalidResponseGet;
25 import org.onap.simulator.presetGenerator.presets.scheduler.PresetGetSchedulerChangeManagementInvalidResponse;
26 import org.onap.simulator.presetGenerator.presets.scheduler.PresetGetSchedulerChangeManagements;
27 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGet;
28 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGetEmptyResult;
29 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGetInvalidResponse;
30 import org.onap.vid.model.probe.ExternalComponentStatus;
31 import org.onap.vid.model.probe.HttpRequestMetadata;
32 import org.springframework.core.ParameterizedTypeReference;
33 import org.springframework.http.HttpMethod;
34 import org.springframework.http.ResponseEntity;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.DataProvider;
37 import org.testng.annotations.Test;
38 import vid.automation.test.services.SimulatorApi;
39
40 public class ProbeApiTest extends BaseApiTest {
41
42     private static final String MSO_QUERY_PARAMS = "filter=requestExecutionDate:EQUALS:01-01-2100";
43     private static final String AAI_QUERY_PARMAS = "business/customers?subscriber-type=INFRA&depth=0";
44     private static final String SDC_PATH_PARAMS = "46401eec-35bd-4e96-ad0d-0356ff6b8c8d/toscaModel";
45     private static final String SCHEDULER_PATH = "/scheduler/v1/ChangeManagement/schedules/scheduleDetails/";
46
47
48     @BeforeClass
49     public void login() {
50         super.login();
51     }
52
53     @DataProvider
54     public static Object[][] probePresetAndResponse(Method test) {
55         return new Object[][]{
56                 {
57                         "all good",
58                         ImmutableList.of(
59                                 new PresetAAIGetSubscribersGet(),
60                                 new PresetMSOOrchestrationRequestsManyStatusesGet(),
61                                 new PresetSDCGetServiceToscaModelGet("46401eec-35bd-4e96-ad0d-0356ff6b8c8d", "serviceCreationTest.zip"),
62                                 new PresetGetSchedulerChangeManagements()
63                         ),
64                         ImmutableList.of(new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
65                                 true,
66                                 new HttpRequestMetadata(HttpMethod.GET,
67                                         200,
68                                         AAI_QUERY_PARMAS,
69                                         "{\"customer\":[{\"global-customer-id\":\"CAR_2020_ER\",\"subscriber-name\":\"CAR_2020_ER\",\"subscriber-type\":\"INFRA\",\"resource-version\":\"1494001902987\",\"relationship-list\":null},{\"global-customer-id\":\"21014aa2-526b-11e6-beb8-9e71128cae77\",\"subscriber-name\":\"JULIO ERICKSON\",\"subscriber-type\":\"INFRA\",\"resource-version\":\"1494001776295\",\"relationship-list\":null},{\"global-customer-id\":\"DHV1707-TestSubscriber-2\",\"subscriber-name\":\"DALE BRIDGES\",\"subscriber-type\":\"INFRA\",\"resource-version\":\"1498751754450\",\"relat",
70                                         "OK"
71                                 )
72                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.MSO,
73                                 true,
74                                 new HttpRequestMetadata(HttpMethod.GET,
75                                         200,
76                                         MSO_QUERY_PARAMS,
77                                         "{ " +
78                                                 " \"requestList\": [{ " +
79                                                 "   \"request\": { " +
80                                                 "    \"requestId\": \"rq1234d1-5a33-55df-13ab-12abad84e333\", " +
81                                                 "    \"startTime\": \"Thu, 04 Jun 2009 02:51:59 GMT\", " +
82                                                 "    \"instanceReferences\": { " +
83                                                 "     \"serviceInstanceId\": \"bc305d54-75b4-431b-adb2-eb6b9e546014\" " +
84                                                 "    }, " +
85                                                 "    \"requestScope\": \"vnf\", " +
86                                                 "    \"requestType\": \"updateInstance\", " +
87                                                 "    \"requestDetails\": { " +
88                                                 "     \"modelInfo\": { " +
89                                                 "      \"modelType\": \"service\", " +
90                                                 "      \"modelInvariantId\": \"sn5256d1-5a33-55df-13ab-12abad84e764\", " +
91                                                 "      \"modelVersionId\": \"ab6478e4-ea33-3346-ac12-ab1",
92                                         "OK"
93                                 )
94                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.SDC,
95                                 true,
96                                 new HttpRequestMetadata(HttpMethod.GET,
97                                         200,
98                                         SDC_PATH_PARAMS,
99                                         "",
100                                         "OK"
101                                 )
102                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.SCHEDULER,
103                                 true,
104                                 new HttpRequestMetadata(HttpMethod.GET,
105                                         200,
106                                         SCHEDULER_PATH,
107                                         StringUtils.substring(new PresetGetSchedulerChangeManagements().getResponseBody().toString(), 0, 500),
108                                         "OK"
109                                 )
110                         ))
111                 },
112                 {
113                         "invalid json",
114                         ImmutableList.of(
115                                 new PresetAAIGetSubscribersGetInvalidResponse(200),
116                                 new PresetMSOOrchestrationRequestsManyStatusesInvalidResponseGet(200),
117                                 new PresetSDCGetServiceToscaModelGetEmptyResult("46401eec-35bd-4e96-ad0d-0356ff6b8c8d"),
118                                 new PresetGetSchedulerChangeManagementInvalidResponse(200)
119                         ),
120                         ImmutableList.of(new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
121                                 false,
122                                 new HttpRequestMetadata(HttpMethod.GET,
123                                         200,
124                                         AAI_QUERY_PARMAS,
125                                         "this payload is an invalid json",
126                                         "com.fasterxml.jackson.core.JsonParseException"
127                                 )
128                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.MSO,
129                                 false,
130                                 new HttpRequestMetadata(HttpMethod.GET,
131                                         200,
132                                         MSO_QUERY_PARAMS,
133                                         "this payload is an invalid json",
134                                         "com.fasterxml.jackson.core.JsonParseException"
135                                 )
136                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.SDC,
137                                 false,
138                                 new HttpRequestMetadata(HttpMethod.GET,
139                                         200,
140                                         SDC_PATH_PARAMS,
141                                         "",
142                                         "error reading model 46401eec-35bd-4e96-ad0d-0356ff6b8c8d from SDC"
143                                 )
144                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.SCHEDULER,
145                                 false,
146                                 new HttpRequestMetadata(HttpMethod.GET,
147                                         200,
148                                         SCHEDULER_PATH,
149                                         "this payload is an invalid json",
150                                         "com.fasterxml.jackson.core.JsonParseException"
151                                 )
152                         ))
153                 },
154                 {
155                         "bad http code",
156                         ImmutableList.of(
157                                 new PresetAAIGetSubscribersGetInvalidResponse(500),
158                                 new PresetMSOOrchestrationRequestsManyStatusesInvalidResponseGet(406),
159                                 new PresetSDCGetServiceToscaModelGetInvalidResponse("46401eec-35bd-4e96-ad0d-0356ff6b8c8d", 404),
160                                 new PresetGetSchedulerChangeManagementInvalidResponse(400)
161                         ),
162                         ImmutableList.of(new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
163                                 false,
164                                 new HttpRequestMetadata(HttpMethod.GET,
165                                         500,
166                                         AAI_QUERY_PARMAS,
167                                         "this payload is an invalid json",
168                                         "No subscriber received"
169                                 )
170                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.MSO,
171                                 false,
172                                 new HttpRequestMetadata(HttpMethod.GET,
173                                         406,
174                                         MSO_QUERY_PARAMS,
175                                         "this payload is an invalid json",
176                                         "MSO returned no orchestration requests"
177                                 )
178                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.SDC,
179                                 false,
180                                 new HttpRequestMetadata(HttpMethod.GET,
181                                         404,
182                                         SDC_PATH_PARAMS,
183                                         "simulated error description from sdc",
184                                         "model 46401eec-35bd-4e96-ad0d-0356ff6b8c8d not found in SDC" +
185                                                 " (consider updating vid probe configuration 'probe.sdc.model.uuid')"
186                                 )
187                         ), new ExternalComponentStatus(ExternalComponentStatus.Component.SCHEDULER,
188                                 false,
189                                 new HttpRequestMetadata(HttpMethod.GET,
190                                         400,
191                                         SCHEDULER_PATH,
192                                         "this payload is an invalid json",
193                                         "org.apache.http.HttpException"
194                                 )
195                         ))
196                 }
197
198         };
199     }
200
201     @Test(dataProvider = "probePresetAndResponse")
202     public void probeRequest_returnsResponseAsExpected(String desc, Collection<BasePreset> presets, Collection<ExternalComponentStatus> expectedStatuses) {
203         SimulatorApi.registerExpectationFromPresets(presets, CLEAR_THEN_SET);
204         ResponseEntity<List<ExternalComponentStatus>> response = restTemplate.exchange(
205                 uri + "/probe",
206                 org.springframework.http.HttpMethod.GET,
207                 null,
208                 new ParameterizedTypeReference<List<ExternalComponentStatus>>() {
209                 });
210         List<ExternalComponentStatus> probeResults = response.getBody();
211         assertEquals(5, probeResults.size());
212         assertResultAsExpected(ExternalComponentStatus.Component.AAI, probeResults, expectedStatuses);
213         assertResultAsExpected(ExternalComponentStatus.Component.SDC, probeResults, expectedStatuses);
214         assertResultAsExpected(ExternalComponentStatus.Component.MSO, probeResults, expectedStatuses);
215         assertResultAsExpected(ExternalComponentStatus.Component.SCHEDULER, probeResults, expectedStatuses);
216     }
217
218     private void assertResultAsExpected(ExternalComponentStatus.Component component, List<ExternalComponentStatus> probeResults, Collection<ExternalComponentStatus> expectedStatuses) {
219         ExternalComponentStatus expectedStatus = expectedStatuses.stream().filter(x -> x.getComponent() == component)
220                 .findFirst().orElseThrow(() -> new AssertionError("Missing setup for " + component + " expected result"));
221         ExternalComponentStatus componentStatus = probeResults.stream().filter(x -> x.getComponent() == component)
222                 .findFirst().orElseThrow(() -> new AssertionError(component.name()+" result not found in response"));
223
224         assertThat("wrong metadata for " + component, requestMetadataReflected(componentStatus.getMetadata()),
225                 is(requestMetadataReflected(expectedStatus.getMetadata())));
226
227         assertThat("wrong url for " + component, componentStatus.getMetadata().getUrl(),
228                 both(endsWith(expectedStatus.getMetadata().getUrl())).and(startsWith("http")));
229
230         assertThat("wrong description for " + component, componentStatus.getMetadata().getDescription(),
231                 anyOf(equalTo(expectedStatus.getMetadata().getDescription()), startsWith(expectedStatus.getMetadata().getDescription())));
232
233         assertThat("wrong status for " + component, componentStatus.isAvailable(), is(expectedStatus.isAvailable()));
234     }
235
236     //serialize fields except of fields we cannot know ahead of time
237     private static String requestMetadataReflected(HttpRequestMetadata metadata) {
238         return new ReflectionToStringBuilder(metadata, ToStringStyle.SHORT_PREFIX_STYLE)
239                 .setExcludeFieldNames("duration", "url", "description")
240                 .toString();
241     }
242 }