Fix for Penetration test _ Session and cookie management
[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 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;
28
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;
36
37 public class AaiResponseTranslatorTest {
38
39     private static final ObjectMapper objectMapper = new ObjectMapper();
40
41     @Test
42     public void extractPortMirroringConfigData_givenValidAaiResponse_yieldCloudRegionId() throws IOException {
43
44         final JsonNode aaiPayload = objectMapper.readTree("" +
45                 "{" +
46                 "  \"results\": [{" +
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\"" +
55                 "      }" +
56                 "    }," +
57                 "    {" +
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\"" +
70                 "      }" +
71                 "    }" +
72                 "  ]" +
73                 "}");
74
75         PortMirroringConfigData portMirroringConfigData =
76                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
77
78         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
79         assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
80
81     }
82
83     @Test
84     public void extractPortMirroringConfigData_givenKindOfValidAaiResponse_yieldCloudRegionId() throws IOException {
85         // some completley different response, but with
86         // the results[cloud-region]->properties->cloud-region-id
87
88         final JsonNode aaiPayload = objectMapper.readTree("" +
89                 "{  " +
90                 "  \"results\": [{  " +
91                 "      \"node-type\": \"generic-vnf\",  " +
92                 "      \"url\": \"configuration entries) so that git\"  " +
93                 "    },  " +
94                 "    {},  " +
95                 "    {  " +
96                 "      \"node-type\": \"cloud-region\",  " +
97                 "      \"but it will not switch\": \"tip commits are reachable\",  " +
98                 "      \"named\": [{  " +
99                 "        \"resource-version\": \"1513631040564\"  " +
100                 "      }],  " +
101                 "      \"properties\": {  " +
102                 "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\",  " +
103                 "        \"oldbranch> will be renamed\": false  " +
104                 "      }  " +
105                 "    },  " +
106                 "    {  " +
107                 "      \"node-type\": [\"generic-vnf\", \"can be overridden by using\"]  " +
108                 "    }  " +
109                 "  ]  " +
110                 "}");
111
112         PortMirroringConfigData portMirroringConfigData =
113                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
114
115         assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
116         assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));
117
118     }
119
120     @Test
121     public void extractPortMirroringConfigData_givenAaiResponseWithoutRegionIdName_yieldException() throws IOException {
122
123         final JsonNode aaiPayload = objectMapper.readTree("" +
124                 "{" +
125                 "  \"results\": [{" +
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\"" +
130                 "      }" +
131                 "    }" +
132                 "  ]" +
133                 "}");
134
135         PortMirroringConfigData portMirroringConfigData =
136                 new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);
137
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())
143         );
144
145     }
146
147     /*
148     More tests:
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.
152      */
153 }