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