Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / AaiResponseTranslatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.aai;
23
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
27 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
28 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
29 import org.testng.annotations.Test;
30
31 import java.io.IOException;
32
33 import static org.hamcrest.CoreMatchers.*;
34 import static org.hamcrest.MatcherAssert.assertThat;
35
36 public class AaiResponseTranslatorTest {
37
38     private static final ObjectMapper objectMapper = new ObjectMapper();
39
40     @Test
41     public void extractPortMirroringConfigData_givenValidAaiResponse_yieldCloudRegionId() throws IOException {
42
43         final JsonNode aaiPayload = objectMapper.readTree("" +
44                 "{" +
45                 "  \"results\": [{" +
46                 "      \"id\": \"2979590232\"," +
47                 "      \"node-type\": \"cloud-region\"," +
48                 "      \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/SDNO-S-BcloudReg-E1802\"," +
49                 "      \"properties\": {" +
50                 "        \"cloud-owner\": \"irma-aic\"," +
51                 "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\"," +
52                 "        \"sriov-automation\": false," +
53                 "        \"resource-version\": \"1513631040564\"" +
54                 "      }" +
55                 "    }," +
56                 "    {" +
57                 "      \"id\": \"2979598424\"," +
58                 "      \"node-type\": \"generic-vnf\"," +
59                 "      \"url\": \"/aai/v12/network/generic-vnfs/generic-vnf/SOURCE-gVnf-E1802\"," +
60                 "      \"properties\": {" +
61                 "        \"vnf-id\": \"SOURCE-gVnf-E1802\"," +
62                 "        \"vnf-name\": \"SOURCE-vnf-SDNO\"," +
63                 "        \"vnf-type\": \"S-1-SDNO\"," +
64                 "        \"service-id\": \"a9a77d5a-123e-4-SDNO\"," +
65                 "        \"orchestration-status\": \"active\"," +
66                 "        \"in-maint\": true," +
67                 "        \"is-closed-loop-disabled\": false," +
68                 "        \"resource-version\": \"1513631043149\"" +
69                 "      }" +
70                 "    }" +
71                 "  ]" +
72                 "}");
73
74         PortMirroringConfigData portMirroringConfigData =
75                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
76
77         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
78         assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
79
80     }
81
82     @Test
83     public void extractPortMirroringConfigData_givenKindOfValidAaiResponse_yieldCloudRegionId() throws IOException {
84         // some completley different response, but with
85         // the results[cloud-region]->properties->cloud-region-id
86
87         final JsonNode aaiPayload = objectMapper.readTree("" +
88                 "{  " +
89                 "  \"results\": [{  " +
90                 "      \"node-type\": \"generic-vnf\",  " +
91                 "      \"url\": \"configuration entries) so that git\"  " +
92                 "    },  " +
93                 "    {},  " +
94                 "    {  " +
95                 "      \"node-type\": \"cloud-region\",  " +
96                 "      \"but it will not switch\": \"tip commits are reachable\",  " +
97                 "      \"named\": [{  " +
98                 "        \"resource-version\": \"1513631040564\"  " +
99                 "      }],  " +
100                 "      \"properties\": {  " +
101                 "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\",  " +
102                 "        \"oldbranch> will be renamed\": false  " +
103                 "      }  " +
104                 "    },  " +
105                 "    {  " +
106                 "      \"node-type\": [\"generic-vnf\", \"can be overridden by using\"]  " +
107                 "    }  " +
108                 "  ]  " +
109                 "}");
110
111         PortMirroringConfigData portMirroringConfigData =
112                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
113
114         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
115         assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
116
117     }
118
119     public void extractPortMirroringConfigData_givenAaiResponseWithoutRegionIdName_yieldException() throws IOException {
120
121         final JsonNode aaiPayload = objectMapper.readTree("" +
122                 "{" +
123                 "  \"results\": [{" +
124                 "      \"node-type\": \"cloud-region\"," +
125                 "      \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/SDNO-S-BcloudReg-E1802\"," +
126                 "      \"properties\": {" +
127                 "        \"resource-version\": \"1513631040564\"" +
128                 "      }" +
129                 "    }" +
130                 "  ]" +
131                 "}");
132
133         PortMirroringConfigData portMirroringConfigData =
134                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
135
136         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataError.class)));
137         assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getErrorDescription(),
138                 containsString("The node-type 'cloud-region' does not contain the property 'cloud-region-id'"));
139         assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getRawAaiResponse(),
140                 containsString(aaiPayload.toString())
141         );
142
143     }
144
145     /*
146     More tests:
147     [x]  cloud-region-id field is missing -- descriptive exception is thrown, including the problematic payload itself
148     [ ]  cloud-region-id field is empty -- descriptive exception etc.
149     [ ]  node-type=="cloud-region" entry is empty -- descriptive exception etc.
150      */
151 }