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.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;
20 import java.lang.reflect.Method;
21 import java.util.List;
22 import java.util.Optional;
24 import static org.hamcrest.CoreMatchers.*;
25 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET;
27 public class ProbeApiTest extends BaseApiTest {
35 public static Object[][] probePresetAndResponse(Method test) {
36 return new Object[][]{
38 new PresetAAIGetSubscribersGet(),
39 new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
41 new HttpRequestMetadata(HttpMethod.GET,
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",
50 new PresetAAIGetSubscribersGetInvalidResponse(200),
51 new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
53 new HttpRequestMetadata(HttpMethod.GET,
55 "business/customers?subscriber-type=INFRA&depth=0",
56 "this payload is an invalid json",
57 "org.codehaus.jackson.JsonParseException"
62 new PresetAAIGetSubscribersGetInvalidResponse(500),
63 new ExternalComponentStatus(ExternalComponentStatus.Component.AAI,
65 new HttpRequestMetadata(HttpMethod.GET,
67 "business/customers?subscriber-type=INFRA&depth=0",
68 "this payload is an invalid json",
69 "No subscriber received"
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(
82 org.springframework.http.HttpMethod.GET,
84 new ParameterizedTypeReference<List<ExternalComponentStatus>>() {});
85 List<ExternalComponentStatus> probeResults = response.getBody();
86 Assert.assertEquals(probeResults.size(),1);
87 assertAaiGetAllSubscribersAsExpected(probeResults,expectedStatus);
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());
97 MatcherAssert.assertThat(requestMetadataReflected(aaiGetAllSubscribersStatus.getMetadata()),is(requestMetadataReflected(expectedStatus.getMetadata())));
98 MatcherAssert.assertThat(aaiGetAllSubscribersStatus.getMetadata().getUrl(), both(endsWith(expectedStatus.getMetadata().getUrl())).and(startsWith("http")));
100 MatcherAssert.assertThat(aaiGetAllSubscribersStatus.getMetadata().getDescription(),
101 anyOf(equalTo(expectedStatus.getMetadata().getDescription()), startsWith(expectedStatus.getMetadata().getDescription())));
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")