2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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.ccsdk.sli.adaptors.aai;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.net.ProtocolException;
31 import java.net.URISyntaxException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.HashMap;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
41 import javax.net.ssl.HttpsURLConnection;
42 import javax.ws.rs.HttpMethod;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.Spy;
48 import org.mockito.runners.MockitoJUnitRunner;
50 import com.fasterxml.jackson.core.JsonParseException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
54 import org.onap.aai.inventory.v16.*;
56 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
58 import org.onap.ccsdk.sli.adaptors.aai.AAIService.TransactionIdTracker;
59 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
61 @RunWith(MockitoJUnitRunner.class)
62 public class AAIServiceTest {
63 private static AAIService aaiService = new AAIService(
64 AAIService.class.getResource(AAIService.AAICLIENT_PROPERTIES));
66 @Spy private AAIService aaiServiceSpy = new AAIService(
67 AAIService.class.getResource(AAIService.AAICLIENT_PROPERTIES));
69 @Mock private HttpsURLConnection connMock;
72 public void existsInvalidResource_shouldReturnFailure() throws MalformedURLException, Exception {
73 QueryStatus queryStatus = aaiServiceSpy.exists("InvalidResource", null, null, null);
74 assertEquals(QueryStatus.FAILURE, queryStatus);
78 public void existsGetPserverByCallBackUrl_shouldReturnSuccess() throws MalformedURLException, Exception {
79 String key = "https://aai.api.simpledemo.onap.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
80 String fileLocation = "json/pserverJson.txt";
81 SvcLogicContext ctx = new SvcLogicContext();
84 when(aaiServiceSpy.getConfiguredConnection(new URL(key), HttpMethod.GET)).thenReturn(connMock);
85 when(connMock.getResponseCode()).thenReturn(200);
86 when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
88 QueryStatus queryStatus = aaiServiceSpy.exists("pserver", key, "prefix.", ctx);
90 assertEquals(QueryStatus.SUCCESS, queryStatus);
94 public void existsGetPserverByCallBackUrl_throwsExceptionAndReturnsFailure()
95 throws MalformedURLException, Exception {
96 String key = "https://aai.api.simpledemo.onap.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
97 String fileLocation = "json/pserverJson.txt";
98 SvcLogicContext ctx = new SvcLogicContext();
101 when(aaiServiceSpy.getConfiguredConnection(new URL(key), HttpMethod.GET)).thenReturn(connMock);
102 when(connMock.getResponseCode()).thenReturn(200);
103 when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
105 when(aaiServiceSpy.dataChangeRequestAaiData(key, Pserver.class)).thenThrow(
106 new AAIServiceException("testException"));
108 QueryStatus queryStatus = aaiServiceSpy.exists("pserver", key, "prefix.", ctx);
110 assertEquals(QueryStatus.FAILURE, queryStatus);
114 public void pserverDataChangeRequestData_shouldSucceed() throws Exception {
115 String fileLocation = "json/pserverJson.txt";
116 String url = "https://aai.api.simpledemo.onap.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
119 when(aaiServiceSpy.getConfiguredConnection(new URL(url), HttpMethod.GET)).thenReturn(connMock);
120 when(connMock.getResponseCode()).thenReturn(200);
121 when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
123 Pserver pserver = aaiServiceSpy.dataChangeRequestAaiData(url, Pserver.class);
125 assertEquals("chcil129snd", pserver.getHostname());
129 public void pserverDataChangeRequestData_shouldReturnNullFor404() throws Exception {
130 String fileLocation = "json/pserverJson.txt";
131 String url = "https://aai.api.simpledemo.onap.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
134 when(aaiServiceSpy.getConfiguredConnection(new URL(url), HttpMethod.GET)).thenReturn(connMock);
135 when(connMock.getResponseCode()).thenReturn(404);
136 when(connMock.getErrorStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
138 Pserver pserver = aaiServiceSpy.dataChangeRequestAaiData(url, Pserver.class);
140 assertEquals(null, pserver);
143 @Test(expected = AAIServiceException.class)
144 public void dataChangeRequestData_throwsAAIServiceException() throws Exception {
145 String fileLocation = "json/pserverJson.txt";
146 String url = "https://aai.api.simpledemo.onap.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
149 when(aaiServiceSpy.getConfiguredConnection(new URL(url), HttpMethod.GET)).thenReturn(connMock);
150 when(connMock.getResponseCode()).thenReturn(500);
151 when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
153 aaiServiceSpy.dataChangeRequestAaiData(url, Class.class);
156 public String readFileToString(String fileName) throws IOException, URISyntaxException {
157 URL url = AAIServiceTest.class.getResource(fileName);
158 Path resPath = Paths.get(url.toURI());
160 return new String(Files.readAllBytes(resPath), "UTF8");
163 public <T> T getObjectFromJson(String text, Class<T> type)
164 throws JsonParseException, JsonMappingException, IOException {
165 ObjectMapper mapper = AAIService.getObjectMapper();
167 return type.cast(mapper.readValue(text, type));
170 private void setConnMock() throws ProtocolException {
171 // Set up the connection properties
172 connMock.setRequestProperty("Connection", "close");
173 connMock.setDoInput(true);
174 connMock.setDoOutput(true);
175 connMock.setUseCaches(false);
176 connMock.setConnectTimeout(1000);
177 connMock.setReadTimeout(1000);
178 connMock.setRequestMethod(HttpMethod.GET);
179 connMock.setRequestProperty("Accept", "application/json");
180 connMock.setRequestProperty("Content-Type", "application/json");
181 connMock.setRequestProperty("X-FromAppId", "testId");
182 connMock.setRequestProperty("X-TransactionId", TransactionIdTracker.getNextTransactionId());
186 public void testSetStatusMessage_shouldSucceed() throws SvcLogicException, MalformedURLException {
187 SvcLogicContext ctx = new SvcLogicContext();
188 Map<String, String> parameters = new HashMap<String, String>();
190 parameters.put("key1", "ActivateSubnet failure, need to manually activate in EIPAM.");
191 aaiService.setStatusMethod(parameters, ctx);
193 Pattern r8601 = Pattern.compile(
194 "(\\d{4})-(\\d{2})-(\\d{2})T((\\d{2}):(\\d{2}):(\\d{2}))Z");
195 Matcher isoDate = r8601.matcher(ctx.getAttribute("aai-summary-status-message"));
197 assertTrue(isoDate.lookingAt());
199 assertTrue(ctx.getAttribute("aai-summary-status-message")
200 .contains("ActivateSubnet failure, need to manually activate in EIPAM."));
203 @Test(expected = SvcLogicException.class)
204 public void testSetStatusMessage_nullContext() throws SvcLogicException, MalformedURLException {
205 SvcLogicContext ctx = null;
206 Map<String, String> parameters = new HashMap<String, String>();
208 parameters.put("key1", "ActivateSubnet failure, need to manually activate in EIPAM.");
209 aaiService.setStatusMethod(parameters, ctx);