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 static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.CoreMatchers.instanceOf;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.MatcherAssert.assertThat;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import java.io.IOException;
32 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
33 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
34 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
35 import org.testng.annotations.Test;
37 public class AaiResponseTranslatorTest {
39 private static final ObjectMapper objectMapper = new ObjectMapper();
42 public void extractPortMirroringConfigData_givenValidAaiResponse_yieldCloudRegionId() throws IOException {
44 final JsonNode aaiPayload = objectMapper.readTree("" +
47 " \"id\": \"2979590232\"," +
48 " \"node-type\": \"cloud-region\"," +
49 " \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/SDNO-S-BcloudReg-E1802\"," +
50 " \"properties\": {" +
51 " \"cloud-owner\": \"irma-aic\"," +
52 " \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\"," +
53 " \"sriov-automation\": false," +
54 " \"resource-version\": \"1513631040564\"" +
58 " \"id\": \"2979598424\"," +
59 " \"node-type\": \"generic-vnf\"," +
60 " \"url\": \"/aai/v12/network/generic-vnfs/generic-vnf/SOURCE-gVnf-E1802\"," +
61 " \"properties\": {" +
62 " \"vnf-id\": \"SOURCE-gVnf-E1802\"," +
63 " \"vnf-name\": \"SOURCE-vnf-SDNO\"," +
64 " \"vnf-type\": \"S-1-SDNO\"," +
65 " \"service-id\": \"a9a77d5a-123e-4-SDNO\"," +
66 " \"orchestration-status\": \"active\"," +
67 " \"in-maint\": true," +
68 " \"is-closed-loop-disabled\": false," +
69 " \"resource-version\": \"1513631043149\"" +
75 PortMirroringConfigData portMirroringConfigData =
76 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
78 assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
79 assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
84 public void extractPortMirroringConfigData_givenKindOfValidAaiResponse_yieldCloudRegionId() throws IOException {
85 // some completley different response, but with
86 // the results[cloud-region]->properties->cloud-region-id
88 final JsonNode aaiPayload = objectMapper.readTree("" +
91 " \"node-type\": \"generic-vnf\", " +
92 " \"url\": \"configuration entries) so that git\" " +
96 " \"node-type\": \"cloud-region\", " +
97 " \"but it will not switch\": \"tip commits are reachable\", " +
99 " \"resource-version\": \"1513631040564\" " +
101 " \"properties\": { " +
102 " \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\", " +
103 " \"oldbranch> will be renamed\": false " +
107 " \"node-type\": [\"generic-vnf\", \"can be overridden by using\"] " +
112 PortMirroringConfigData portMirroringConfigData =
113 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
115 assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
116 assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
121 public void extractPortMirroringConfigData_givenAaiResponseWithoutRegionIdName_yieldException() throws IOException {
123 final JsonNode aaiPayload = objectMapper.readTree("" +
126 " \"node-type\": \"cloud-region\"," +
127 " \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/SDNO-S-BcloudReg-E1802\"," +
128 " \"properties\": {" +
129 " \"resource-version\": \"1513631040564\"" +
135 PortMirroringConfigData portMirroringConfigData =
136 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
138 assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataError.class)));
139 assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getErrorDescription(),
140 containsString("The node-type 'cloud-region' does not contain the property 'cloud-region-id'"));
141 assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getRawAaiResponse(),
142 containsString(aaiPayload.toString())
149 [x] cloud-region-id field is missing -- descriptive exception is thrown, including the problematic payload itself
150 [ ] cloud-region-id field is empty -- descriptive exception etc.
151 [ ] node-type=="cloud-region" entry is empty -- descriptive exception etc.