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