Updating aai adapter to v16 model
[ccsdk/sli/adaptors.git] / aai-service / provider / src / test / java / org / onap / ccsdk / sli / adaptors / aai / AAIServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              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.ccsdk.sli.adaptors.aai;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.net.ProtocolException;
31 import java.net.URISyntaxException;
32 import java.net.URL;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40
41 import javax.net.ssl.HttpsURLConnection;
42 import javax.ws.rs.HttpMethod;
43
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;
49
50 import com.fasterxml.jackson.core.JsonParseException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 import org.onap.aai.inventory.v16.*;
55
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;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class AAIServiceTest {
63     private static AAIService aaiService = new AAIService(
64             AAIService.class.getResource(AAIService.AAICLIENT_PROPERTIES));
65
66     @Spy private AAIService aaiServiceSpy = new AAIService(
67             AAIService.class.getResource(AAIService.AAICLIENT_PROPERTIES));
68
69     @Mock private HttpsURLConnection connMock;
70
71     // @Test
72     public void existsInvalidResource_shouldReturnFailure() throws MalformedURLException, Exception {
73         QueryStatus queryStatus = aaiServiceSpy.exists("InvalidResource", null, null, null);
74         assertEquals(QueryStatus.FAILURE, queryStatus);
75     }
76
77 //    @Test
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();
82         setConnMock();
83
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));
87
88         QueryStatus queryStatus = aaiServiceSpy.exists("pserver", key, "prefix.", ctx);
89
90         assertEquals(QueryStatus.SUCCESS, queryStatus);
91     }
92
93 //    @Test
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();
99         setConnMock();
100
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));
104
105         when(aaiServiceSpy.dataChangeRequestAaiData(key, Pserver.class)).thenThrow(
106                 new AAIServiceException("testException"));
107
108         QueryStatus queryStatus = aaiServiceSpy.exists("pserver", key, "prefix.", ctx);
109
110         assertEquals(QueryStatus.FAILURE, queryStatus);
111     }
112
113 //    @Test
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";
117         setConnMock();
118
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));
122
123         Pserver pserver = aaiServiceSpy.dataChangeRequestAaiData(url, Pserver.class);
124
125         assertEquals("chcil129snd", pserver.getHostname());
126     }
127
128 //    @Test
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";
132         setConnMock();
133
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));
137
138         Pserver pserver = aaiServiceSpy.dataChangeRequestAaiData(url, Pserver.class);
139
140         assertEquals(null, pserver);
141     }
142
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";
147         setConnMock();
148
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));
152
153         aaiServiceSpy.dataChangeRequestAaiData(url, Class.class);
154     }
155
156     public String readFileToString(String fileName) throws IOException, URISyntaxException {
157         URL url = AAIServiceTest.class.getResource(fileName);
158         Path resPath = Paths.get(url.toURI());
159
160         return new String(Files.readAllBytes(resPath), "UTF8");
161     }
162
163     public <T> T getObjectFromJson(String text, Class<T> type)
164             throws JsonParseException, JsonMappingException, IOException {
165         ObjectMapper mapper = AAIService.getObjectMapper();
166
167         return type.cast(mapper.readValue(text, type));
168     }
169
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());
183     }
184
185     @Test
186     public void testSetStatusMessage_shouldSucceed() throws SvcLogicException, MalformedURLException {
187         SvcLogicContext ctx = new SvcLogicContext();
188         Map<String, String> parameters = new HashMap<String, String>();
189
190         parameters.put("key1", "ActivateSubnet failure, need to manually activate in EIPAM.");
191         aaiService.setStatusMethod(parameters, ctx);
192
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"));
196
197         assertTrue(isoDate.lookingAt());
198
199         assertTrue(ctx.getAttribute("aai-summary-status-message")
200                 .contains("ActivateSubnet failure, need to manually activate in EIPAM."));
201     }
202
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>();
207
208         parameters.put("key1", "ActivateSubnet failure, need to manually activate in EIPAM.");
209         aaiService.setStatusMethod(parameters, ctx);
210     }
211 }