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