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