Merge 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 org.apache.commons.lang3.builder.ReflectionToStringBuilder;
4 import org.apache.commons.lang3.builder.ToStringStyle;
5 import org.hamcrest.MatcherAssert;
6 import org.junit.Assert;
7 import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
8 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
9 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGetInvalidResponse;
10 import org.onap.vid.model.probe.ExternalComponentStatus;
11 import org.onap.vid.model.probe.HttpRequestMetadata;
12 import org.springframework.core.ParameterizedTypeReference;
13 import org.springframework.http.HttpMethod;
14 import org.springframework.http.ResponseEntity;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.DataProvider;
17 import org.testng.annotations.Test;
18 import vid.automation.test.services.SimulatorApi;
19
20 import java.lang.reflect.Method;
21 import java.util.List;
22 import java.util.Optional;
23
24 import static org.hamcrest.CoreMatchers.*;
25 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET;
26
27 public class ProbeApiTest extends BaseApiTest {
28
29     @BeforeClass
30     public void login() {
31         super.login();
32     }
33
34     @DataProvider
35     public static Object[][] probePresetAndResponse(Method test) {
36         return new Object[][]{
37                 {
38                     new PresetAAIGetSubscribersGet(),
39                     new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
40                         true,
41                         new HttpRequestMetadata(HttpMethod.GET,
42                                 200,
43                                 "business/customers?subscriber-type=INFRA&depth=0",
44                                 "{\"customer\":[{\"global-customer-id\":\"MSO_1610_ST\",\"subscriber-name\":\"MSO_1610_ST\",\"subscriber-type\":\"INFRA\",\"resource-version\":\"1494001902987\"},{\"global-customer-id\":\"21014aa2-526b-11e6-beb8-9e71128cae77\",\"subscriber-name\":\"PACKET CORE\",\"subscriber-type\":\"INFRA\",\"resource-version\":\"1494001776295\"},{\"global-customer-id\":\"DHV1707-TestSubscriber-2\",\"subscriber-name\":\"ICORE CORE\",\"subscriber-type\":\"INFRA\",\"resource-version\":\"1498751754450\"},{\"global-customer-id\":\"DHV1707-TestSubscriber-1\",\"subscriber",
45                                 "OK"
46                                 )
47                     )
48                 },
49                 {
50                         new PresetAAIGetSubscribersGetInvalidResponse(200),
51                         new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
52                                 false,
53                                 new HttpRequestMetadata(HttpMethod.GET,
54                                         200,
55                                         "business/customers?subscriber-type=INFRA&depth=0",
56                                         "this payload is an invalid json",
57                                         "org.codehaus.jackson.JsonParseException"
58                                 )
59                         )
60                 },
61                 {
62                         new PresetAAIGetSubscribersGetInvalidResponse(500),
63                         new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
64                                 false,
65                                 new HttpRequestMetadata(HttpMethod.GET,
66                                         500,
67                                         "business/customers?subscriber-type=INFRA&depth=0",
68                                         "this payload is an invalid json",
69                                         "No subscriber received"
70                                 )
71                         )
72                 }
73
74         };
75     }
76
77     @Test(dataProvider = "probePresetAndResponse")
78     public void probeRequest_returnsResponseAsExpected(BasePreset preset, ExternalComponentStatus expectedStatus ){
79         SimulatorApi.registerExpectationFromPreset(preset, CLEAR_THEN_SET);
80         ResponseEntity<List<ExternalComponentStatus>> response = restTemplate.exchange(
81                 uri + "/probe",
82                 org.springframework.http.HttpMethod.GET,
83                 null,
84                 new ParameterizedTypeReference<List<ExternalComponentStatus>>() {});
85         List<ExternalComponentStatus> probeResults = response.getBody();
86         Assert.assertEquals(probeResults.size(),1);
87         assertAaiGetAllSubscribersAsExpected(probeResults,expectedStatus);
88
89     }
90
91     private void assertAaiGetAllSubscribersAsExpected(List<ExternalComponentStatus> probeResults,ExternalComponentStatus expectedStatus){
92         Optional<ExternalComponentStatus> aaiGetAllSubscribersResult = probeResults.stream().filter(x -> x.getComponent()== ExternalComponentStatus.Component.AAI).findFirst();
93         Assert.assertTrue(aaiGetAllSubscribersResult.isPresent());
94         ExternalComponentStatus aaiGetAllSubscribersStatus = aaiGetAllSubscribersResult.get();
95         Assert.assertEquals(aaiGetAllSubscribersStatus.isAvailable(),expectedStatus.isAvailable());
96
97         MatcherAssert.assertThat(requestMetadataReflected(aaiGetAllSubscribersStatus.getMetadata()),is(requestMetadataReflected(expectedStatus.getMetadata())));
98         MatcherAssert.assertThat(aaiGetAllSubscribersStatus.getMetadata().getUrl(), both(endsWith(expectedStatus.getMetadata().getUrl())).and(startsWith("http")));
99
100         MatcherAssert.assertThat(aaiGetAllSubscribersStatus.getMetadata().getDescription(),
101                 anyOf(equalTo(expectedStatus.getMetadata().getDescription()), startsWith(expectedStatus.getMetadata().getDescription())));
102     }
103
104     //serialize fields except of fields we cannot know ahead of time
105     private static String requestMetadataReflected(HttpRequestMetadata metadata) {
106         return new ReflectionToStringBuilder(metadata, ToStringStyle.SHORT_PREFIX_STYLE)
107                 .setExcludeFieldNames("duration", "url", "description")
108                 .toString();
109     }
110 }