Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AaiResponseTranslatorTest.java
1 package org.onap.vid.services;
2
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import org.onap.vid.aai.AaiResponseTranslator;
6 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
7 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
8 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
9 import org.testng.annotations.Test;
10
11 import java.io.IOException;
12
13 import static org.hamcrest.CoreMatchers.*;
14 import static org.hamcrest.MatcherAssert.assertThat;
15
16 public class AaiResponseTranslatorTest {
17
18     private static final ObjectMapper objectMapper = new ObjectMapper();
19
20     @Test
21     public void extractPortMirroringConfigData_givenValidAaiResponse_yieldCloudRegionId() throws IOException {
22
23         final JsonNode aaiPayload = objectMapper.readTree("" +
24                 "{" +
25                 "  \"results\": [{" +
26                 "      \"id\": \"2979590232\"," +
27                 "      \"node-type\": \"cloud-region\"," +
28                 "      \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/SDNO-S-BcloudReg-E1802\"," +
29                 "      \"properties\": {" +
30                 "        \"cloud-owner\": \"att-aic\"," +
31                 "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\"," +
32                 "        \"sriov-automation\": false," +
33                 "        \"resource-version\": \"1513631040564\"" +
34                 "      }" +
35                 "    }," +
36                 "    {" +
37                 "      \"id\": \"2979598424\"," +
38                 "      \"node-type\": \"generic-vnf\"," +
39                 "      \"url\": \"/aai/v12/network/generic-vnfs/generic-vnf/SOURCE-gVnf-E1802\"," +
40                 "      \"properties\": {" +
41                 "        \"vnf-id\": \"SOURCE-gVnf-E1802\"," +
42                 "        \"vnf-name\": \"SOURCE-vnf-SDNO\"," +
43                 "        \"vnf-type\": \"S-1-SDNO\"," +
44                 "        \"service-id\": \"a9a77d5a-123e-4-SDNO\"," +
45                 "        \"orchestration-status\": \"active\"," +
46                 "        \"in-maint\": true," +
47                 "        \"is-closed-loop-disabled\": false," +
48                 "        \"resource-version\": \"1513631043149\"" +
49                 "      }" +
50                 "    }" +
51                 "  ]" +
52                 "}");
53
54         PortMirroringConfigData portMirroringConfigData =
55                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
56
57         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
58         assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
59
60     }
61
62     @Test
63     public void extractPortMirroringConfigData_givenKindOfValidAaiResponse_yieldCloudRegionId() throws IOException {
64         // some completley different response, but with
65         // the results[cloud-region]->properties->cloud-region-id
66
67         final JsonNode aaiPayload = objectMapper.readTree("" +
68                 "{  " +
69                 "  \"results\": [{  " +
70                 "      \"node-type\": \"generic-vnf\",  " +
71                 "      \"url\": \"configuration entries) so that git\"  " +
72                 "    },  " +
73                 "    {},  " +
74                 "    {  " +
75                 "      \"node-type\": \"cloud-region\",  " +
76                 "      \"but it will not switch\": \"tip commits are reachable\",  " +
77                 "      \"named\": [{  " +
78                 "        \"resource-version\": \"1513631040564\"  " +
79                 "      }],  " +
80                 "      \"properties\": {  " +
81                 "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\",  " +
82                 "        \"oldbranch> will be renamed\": false  " +
83                 "      }  " +
84                 "    },  " +
85                 "    {  " +
86                 "      \"node-type\": [\"generic-vnf\", \"can be overridden by using\"]  " +
87                 "    }  " +
88                 "  ]  " +
89                 "}");
90
91         PortMirroringConfigData portMirroringConfigData =
92                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
93
94         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
95         assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
96
97     }
98
99     public void extractPortMirroringConfigData_givenAaiResponseWithoutRegionIdName_yieldException() throws IOException {
100
101         final JsonNode aaiPayload = objectMapper.readTree("" +
102                 "{" +
103                 "  \"results\": [{" +
104                 "      \"node-type\": \"cloud-region\"," +
105                 "      \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/SDNO-S-BcloudReg-E1802\"," +
106                 "      \"properties\": {" +
107                 "        \"resource-version\": \"1513631040564\"" +
108                 "      }" +
109                 "    }" +
110                 "  ]" +
111                 "}");
112
113         PortMirroringConfigData portMirroringConfigData =
114                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
115
116         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataError.class)));
117         assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getErrorDescription(),
118                 containsString("The node-type 'cloud-region' does not contain the property 'cloud-region-id'"));
119         assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getRawAaiResponse(),
120                 containsString(aaiPayload.toString())
121         );
122
123     }
124
125     /*
126     More tests:
127     [x]  cloud-region-id field is missing -- descriptive exception is thrown, including the problematic payload itself
128     [ ]  cloud-region-id field is empty -- descriptive exception etc.
129     [ ]  node-type=="cloud-region" entry is empty -- descriptive exception etc.
130      */
131 }