db10fdc578b61e524d98d3852ddb83ea115c40c7
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / pProbeAaiApiTest.java
1 package org.onap.vid.api;
2
3 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy;
4 import static vid.automation.test.services.SimulatorApi.registerExpectation;
5
6 import com.google.common.collect.ImmutableMap;
7 import java.io.IOException;
8 import java.lang.reflect.Method;
9 import java.net.URISyntaxException;
10 import java.util.ArrayList;
11 import org.apache.commons.text.StringEscapeUtils;
12 import org.springframework.http.HttpMethod;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.ResponseEntity;
15 import org.testng.Assert;
16 import org.testng.annotations.DataProvider;
17 import org.testng.annotations.Test;
18
19
20 public class pProbeAaiApiTest extends BaseApiAaiTest {
21
22     private static final String GET_SERVICE_INSTANCE_NOT_FOUND_JSON = "get_service_instance_not_found.json";
23     private static final String GET_SERVICE_INSTANCE_JSON = "get_service_instance.json";
24     private static final String GET_LOGICAL_LINK_JSON = "get_logical_link.json";
25     private static final String GET_LOGICAL_LINK_NOT_FOUND_JSON = "get_logical_link_not_found.json";
26     private static final String [] GET_SPECIFIC_PNF_FINE_RESPONSE = {"aai_get_specific_pnf.json"};
27     private static final String [] GET_SPECIFIC_PNF_ERROR_RESPONSE = {"aai_get_specific_pnf_error.json"};
28     private static final String [] GET_PNF_BY_REGION_RESPONSE = {"aai_get_pnf_by_region.json"};
29     private static final String [] GET_PNF_BY_REGION_RESPONSE_EMPTY = {"aai_get_pnf_by_region_error.json"};
30
31     //Request
32     private static final String GET_PNF_BY_REGION_AAI_EXPECTED_RESPONSE = "registration_to_simulator/body_jsons/aai_response_get_pnf_by_region.json";
33
34
35     //URIs
36     private static final String GET_SERVICE_INSTANCE_PNFS = "aai_get_service_instance_pnfs/31739f3e-526b-11e6-beb8-9e71128cae77/AIM Transport/f36f5734-e9df-4fbf-9f35-61be13f028a1";
37     private static final String GET_SPECIFIC_PNF_URI = "aai_get_pnfs/pnf/DEAAI78";
38     private static final String GET_PNF_BY_REGION = "aai_get_pnf_instances/e433710f-9217-458d-a79d-1c7aff376d89/TYLER SILVIA/8a84e59b-45fe-4851-8ff1-34225a0b32c3/83b458fd-5dd3-419b-a9e3-7335814a0911/JANET25/Cisco/Nexus 3048-TP";
39
40
41
42     //Expected strings
43     private static final String GET_SPECIFIC_PNF_EXPECTED = "{\"resourceVersion\":\"1494001797554\",\"relationshipList\":{\"relationship\":[{\"relatedTo\":\"complex\",\"relatedLink\":\"/aai/v11/cloud-infrastructure/complexes/complex/NAMEAAI2\",\"relationshipLabel\": \"onap.pnf\",\"relationshipData\":[{\"relationshipKey\":\"complex.physical-location-id\",\"relationshipValue\":\"NAMEAAI2\"}],\"relatedToProperty\":null,\"relationDataList\":[{\"relationshipKey\":\"complex.physical-location-id\",\"relationshipValue\":\"NAMEAAI2\"}],\"relatedToPropertyList\":null}]},\"pnfName\":\"DEAAI78\",\"pnfName2\":\"DEAAI78-name-2\",\"pnfName2Source\":\"DEAAI78-name-2-source\",\"pnfId\":\"DEAAI78-id\",\"equipType\":\"Switch\",\"equipVendor\":\"Cisco\",\"equipModel\":\"ASR1002-X\"}";
44
45     @DataProvider
46     public static Object[][] getAssociatedPnfs(Method test) {
47         return new Object[][]{
48                 {GET_SERVICE_INSTANCE_JSON, GET_LOGICAL_LINK_JSON},
49
50                 //no need to call to getLogicalLink if service has direct relation to the pnfs
51                 {"get_service_instance_direct_relation_pnf.json", null}
52         };
53     }
54
55     @Test(dataProvider = "getAssociatedPnfs")
56     public void testGetAssociatedPnfs(String getServiceInstanceJson, String getLogicalLinkJson) {
57         registerExpectation(getServiceInstanceJson, RegistrationStrategy.APPEND);
58         if (getLogicalLinkJson!=null) {
59             registerExpectation(getLogicalLinkJson, RegistrationStrategy.APPEND);
60         }
61         ResponseEntity<ArrayList> response = restTemplate.getForEntity(buildUri(GET_SERVICE_INSTANCE_PNFS), ArrayList.class);
62         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
63         ArrayList pnfs = response.getBody();
64         Assert.assertNotNull(pnfs);
65         Assert.assertEquals(pnfs.size(), 2);
66         Assert.assertEquals(pnfs.get(0), "tesaaisdgraclz1a1");
67         Assert.assertEquals(pnfs.get(1), "tesai371ve2");
68     }
69
70     @Test
71     public void testGetAssociatedPnfsByRegisteredServiceResponse() throws Exception {
72         registerExpectation(GET_SERVICE_INSTANCE_NOT_FOUND_JSON, RegistrationStrategy.CLEAR_THEN_SET);
73         ResponseEntity<ArrayList> response = restTemplate.getForEntity(buildUri(GET_SERVICE_INSTANCE_PNFS), ArrayList.class);
74         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
75         ArrayList pnfs = response.getBody();
76         Assert.assertNotNull(pnfs);
77         Assert.assertEquals(pnfs.size(), 0);
78     }
79
80     @Test
81     public void testGetAssociatedPnfsByRegisteredLogicalLinkResponse() throws Exception {
82         registerExpectation(GET_SERVICE_INSTANCE_JSON, RegistrationStrategy.CLEAR_THEN_SET);
83         registerExpectation(GET_LOGICAL_LINK_NOT_FOUND_JSON, RegistrationStrategy.APPEND);
84         ResponseEntity<ArrayList> response = restTemplate.getForEntity(buildUri(GET_SERVICE_INSTANCE_PNFS), ArrayList.class);
85         Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
86         ArrayList pnfs = response.getBody();
87         Assert.assertNotNull(pnfs);
88         Assert.assertEquals(pnfs.size(), 0);
89     }
90
91     @Test
92     public void testGetSpecificPnf() throws Exception {
93         callAaiWithSimulatedErrorResponse(GET_SPECIFIC_PNF_FINE_RESPONSE,
94                 ImmutableMap.of(),
95                 buildUri(GET_SPECIFIC_PNF_URI), "",200,GET_SPECIFIC_PNF_EXPECTED, HttpMethod.GET);
96
97     }
98
99     @Test(dataProvider = "errorCodes")
100     public void testGetSpecificPnfError(int errorCode) throws IOException, URISyntaxException {
101         final String expectedResult ="{ \"requestError\": { \"serviceException\": { \"messageId\": \"SVC3001\", \"text\": \"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\", \"variables\": [ \"GET\", \"network/pnfs/pnf/DEAAI78ff\", \"Node Not Found:No Node of type pnf found at: network/pnfs/pnf/DEAAI78ff\", \"ERR.5.4.6114\" ] } } }";
102
103         callAaiWithSimulatedErrorResponse(GET_SPECIFIC_PNF_ERROR_RESPONSE,
104                 ImmutableMap.of("500", Integer.toString(errorCode),"\"ERROR_PAYLOAD\"", StringEscapeUtils.escapeJson(expectedResult)),
105                 buildUri(GET_SPECIFIC_PNF_URI), "",errorCode,expectedResult,HttpMethod.GET);
106
107     }
108
109     @Test
110     public void testGetPnfDataByRegion() throws Exception {
111         String expected = "{\"results\":[{\"id\":\"901128280\",\"url\":\"/aai/v12/network/pnfs/pnf/AS-pnf2-10219--as988q\",\"properties\":{\"pnfName\":\"AS-pnf2-10219--as988q\",\"equipType\":\"Switch\",\"equipVendor\":\"Cisco\",\"equipModel\":\"Nexus3048-TP\",\"inMaint\":false,\"resourceVersion\":\"1508776538192\"},\"nodeType\":\"pnf\",\"relatedTo\":[{\"id\":\"532488360\",\"url\":\"/aai/v12/business/customers/customer/customer-10219-as988q/service-subscriptions/service-subscription/serviceSub2-test-10219-as988q/service-instances/service-instance/serviceIns2-test-10219-as988q\",\"node-type\":\"service-instance\",\"relationship-label\":\"uses\"},{\"id\":\"860164248\",\"url\":\"/aai/v12/cloud-infrastructure/complexes/complex/complex-10219--as988q\",\"node-type\":\"complex\",\"relationship-label\":\"locatedIn\"}]}]}";
112         callAaiWithSimulatedErrorResponse(GET_PNF_BY_REGION_RESPONSE,
113                 ImmutableMap.of(),
114                 buildUri(GET_PNF_BY_REGION), "",200,expected, HttpMethod.GET);
115
116     }
117
118     @Test(dataProvider = "errorCodes")
119     public void testGetPnfDataByRegionError(int errorCode) throws IOException, URISyntaxException {
120         final String expectedResult ="{\"results\":[]}";
121
122         callAaiWithSimulatedErrorResponse(GET_PNF_BY_REGION_RESPONSE_EMPTY,
123                 ImmutableMap.of("500", Integer.toString(errorCode),"\"ERROR_PAYLOAD\"", StringEscapeUtils.escapeJson(expectedResult)),
124                 buildUri(GET_PNF_BY_REGION), "",errorCode,expectedResult,HttpMethod.GET);
125
126     }
127
128     @DataProvider
129     public static Object[][] errorCodes(Method test) {
130         return new Object[][]{
131                 {500},{505}, {400}, {401}, {405}
132         };
133     }
134
135     @Test
136     public void testGetPnfDataByRegionNoResults() throws IOException, URISyntaxException {
137         final String registratedResult = "{\"results\":[]}";
138         final String expectedResult ="{\"results\":[]}";
139         final int expectedResponseCode = 200;
140         callAaiWithSimulatedErrorResponse(GET_PNF_BY_REGION_RESPONSE_EMPTY,
141                 ImmutableMap.of("500", Integer.toString(expectedResponseCode),"\"ERROR_PAYLOAD\"", StringEscapeUtils.escapeJson(registratedResult)),
142                 buildUri(GET_PNF_BY_REGION), "",expectedResponseCode,expectedResult,HttpMethod.GET);
143
144     }
145 }