2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.vid.aai;
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;
31 import java.io.IOException;
33 import static org.hamcrest.CoreMatchers.*;
34 import static org.hamcrest.MatcherAssert.assertThat;
36 public class AaiResponseTranslatorTest {
38 private static final ObjectMapper objectMapper = new ObjectMapper();
41 public void extractPortMirroringConfigData_givenValidAaiResponse_yieldCloudRegionId() throws IOException {
43 final JsonNode aaiPayload = objectMapper.readTree("" +
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\"" +
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\"" +
74 PortMirroringConfigData portMirroringConfigData =
75 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
77 assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
78 assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
83 public void extractPortMirroringConfigData_givenKindOfValidAaiResponse_yieldCloudRegionId() throws IOException {
84 // some completley different response, but with
85 // the results[cloud-region]->properties->cloud-region-id
87 final JsonNode aaiPayload = objectMapper.readTree("" +
90 " \"node-type\": \"generic-vnf\", " +
91 " \"url\": \"configuration entries) so that git\" " +
95 " \"node-type\": \"cloud-region\", " +
96 " \"but it will not switch\": \"tip commits are reachable\", " +
98 " \"resource-version\": \"1513631040564\" " +
100 " \"properties\": { " +
101 " \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\", " +
102 " \"oldbranch> will be renamed\": false " +
106 " \"node-type\": [\"generic-vnf\", \"can be overridden by using\"] " +
111 PortMirroringConfigData portMirroringConfigData =
112 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
114 assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
115 assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
119 public void extractPortMirroringConfigData_givenAaiResponseWithoutRegionIdName_yieldException() throws IOException {
121 final JsonNode aaiPayload = objectMapper.readTree("" +
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\"" +
133 PortMirroringConfigData portMirroringConfigData =
134 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
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())
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.