1 package org.onap.vid.api;
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;
19 import java.lang.reflect.Method;
20 import java.util.List;
21 import java.util.Optional;
23 import static org.hamcrest.CoreMatchers.*;
24 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET;
26 public class ProbeApiTest extends BaseApiTest {
34 public static Object[][] probePresetAndResponse(Method test) {
35 return new Object[][]{
37 new PresetAAIGetSubscribersGet(),
38 new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
40 new HttpRequestMetadata(HttpMethod.GET,
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",
49 new PresetAAIGetSubscribersGetInvalidResponse(200),
50 new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
52 new HttpRequestMetadata(HttpMethod.GET,
54 "business/customers?subscriber-type=INFRA&depth=0",
55 "this payload is an invalid json",
56 "org.codehaus.jackson.JsonParseException"
61 new PresetAAIGetSubscribersGetInvalidResponse(500),
62 new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
64 new HttpRequestMetadata(HttpMethod.GET,
66 "business/customers?subscriber-type=INFRA&depth=0",
67 "this payload is an invalid json",
68 "No subscriber received"
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(
81 org.springframework.http.HttpMethod.GET,
83 new ParameterizedTypeReference<List<ExternalComponentStatus>>() {});
84 List<ExternalComponentStatus> probeResults = response.getBody();
85 Assert.assertEquals(probeResults.size(),1);
86 assertAaiGetAllSubscribersAsExpected(probeResults,expectedStatus);
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());
96 Assert.assertThat(requestMetadataReflected(aaiGetAllSubscribersStatus.getMetadata()),is(requestMetadataReflected(expectedStatus.getMetadata())));
97 Assert.assertThat(aaiGetAllSubscribersStatus.getMetadata().getUrl(), both(endsWith(expectedStatus.getMetadata().getUrl())).and(startsWith("http")));
99 Assert.assertThat(aaiGetAllSubscribersStatus.getMetadata().getDescription(),
100 anyOf(equalTo(expectedStatus.getMetadata().getDescription()), startsWith(expectedStatus.getMetadata().getDescription())));
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")