1 package org.onap.portalapp.portal.controller;
3 * ============LICENSE_START==========================================
5 * ===================================================================
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * ===================================================================
9 * Unless otherwise specified, all software contained herein is licensed
10 * under the Apache License, Version 2.0 (the "License");
11 * you may not use this software except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
22 * Unless otherwise specified, all documentation contained herein is licensed
23 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
24 * you may not use this documentation except in compliance with the License.
25 * You may obtain a copy of the License at
27 * https://creativecommons.org/licenses/by/4.0/
29 * Unless required by applicable law or agreed to in writing, documentation
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
35 * ============LICENSE_END============================================
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNotNull;
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.UUID;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56 import org.drools.core.command.assertion.AssertEquals;
57 import org.json.JSONObject;
58 import org.junit.Assert;
59 import org.junit.Before;
60 import org.junit.Test;
61 import org.junit.runner.RunWith;
62 import org.mockito.InjectMocks;
63 import org.mockito.Matchers;
64 import org.mockito.Mock;
65 import org.mockito.Mockito;
66 import org.mockito.MockitoAnnotations;
67 import org.onap.portalapp.portal.controller.SharedContextRestClient;
68 import org.onap.portalapp.portal.controller.SharedContextTestProperties;
69 import org.onap.portalapp.portal.core.MockEPUser;
70 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
71 import org.onap.portalapp.portal.domain.SharedContext;
72 import org.onap.portalapp.portal.framework.MockitoTestSuite;
73 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
74 import org.onap.portalapp.portal.service.SharedContextService;
75 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
76 import org.onap.portalsdk.core.util.SystemProperties;
77 import org.onap.portalsdk.core.web.support.UserUtils;
78 import org.powermock.api.mockito.PowerMockito;
79 import org.powermock.core.classloader.annotations.PrepareForTest;
80 import org.powermock.modules.junit4.PowerMockRunner;
81 import org.springframework.beans.factory.annotation.Autowired;
83 import com.fasterxml.jackson.databind.DeserializationFeature;
84 import com.fasterxml.jackson.databind.ObjectMapper;
87 * Tests the endpoints exposed by the Shared Context controller in Portal.
89 @RunWith(PowerMockRunner.class)
90 @PrepareForTest({SharedContext.class,EPCommonSystemProperties.class})
92 public class SharedContextRestControllerTest {
95 SharedContextService contextService;
98 SharedContextRestController sharedContextRestController=new SharedContextRestController();
101 public void setup() {
102 MockitoAnnotations.initMocks(this);
106 MockEPUser mockUser = new MockEPUser();
107 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
109 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
110 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
111 NullPointerException nullPointerException = new NullPointerException();
113 private final Log logger = LogFactory.getLog(getClass());
115 //private final SharedContextTestProperties properties;
117 private final String ckey = "ckey";
118 private final String cvalue = "cvalue";
120 // Supposed to be a Portal session ID
121 private final String cxid = UUID.randomUUID().toString();
123 private final String key = "key123";
124 private final String value1 = "first value";
125 private final String value2 = "second value";
127 /*public SharedContextRestControllerTest() throws IOException {
128 properties = new SharedContextTestProperties();
131 @SuppressWarnings("unchecked")
133 /*public void test() throws Exception {
134 String response = null, val = null;
135 ObjectMapper mapper = new ObjectMapper();
136 Map<String, Object> responseMap, jsonMap;
138 logger.info("Get on empty context");
139 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
140 // Should not exist - just generated the UUID
141 Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);
142 response = (String) responseMap1.get("response");
143 Assert.assertNull(response);
145 logger.info("Set a new context");
146 response = setContext(cxid, key, value1);
147 Assert.assertNotNull(response);
148 responseMap = mapper.readValue(response, Map.class);
149 String responseValue = (String) responseMap.get("response");
150 Assert.assertNotNull(responseValue);
151 Assert.assertEquals("added", responseValue);
153 logger.info("Get existing context");
154 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
155 responseMap = mapper.readValue(response, Map.class);
156 jsonMap = (Map<String,Object>) responseMap.get("response");
157 Assert.assertNotNull(jsonMap);
158 val = (String) jsonMap.get(cvalue);
159 Assert.assertEquals(val, value1);
161 logger.info("Overwrite exiting context");
162 response = setContext(cxid, key, value2);
163 Assert.assertNotNull(response);
164 responseMap = mapper.readValue(response, Map.class);
165 response = (String) responseMap.get("response");
166 Assert.assertNotNull(responseValue);
167 // Assert.assertEquals("replaced", responseValue);
169 logger.info("Get existing context to verify overwrite");
170 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
171 responseMap = mapper.readValue(response, Map.class);
172 jsonMap = (Map<String,Object>) responseMap.get("response");
173 Assert.assertNotNull(jsonMap);
174 val = (String) jsonMap.get(cvalue);
175 Assert.assertEquals(val, value2);
177 logger.info("Delete one context");
178 response = SharedContextRestClient.getJson(properties, "remove", cxid, key);
179 responseMap = mapper.readValue(response, Map.class);
180 response = (String) responseMap.get("response");
181 Assert.assertEquals(response, "removed");
183 logger.info("Clear the context");
184 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
185 Assert.assertEquals("", response);
188 private String setContext(String context, String id, String value) throws Exception {
189 ObjectMapper mapper = new ObjectMapper();
190 HashMap<String,String> stringMap = new HashMap<String,String>();
191 stringMap.put("context_id", cxid);
192 stringMap.put(ckey, key);
193 stringMap.put(cvalue, value2);
194 String json = mapper.writeValueAsString(stringMap);
195 String response = SharedContextRestClient.postJson(properties, "set", json);
200 public void getContextTest() throws Exception{
201 SharedContext sharedContext=new SharedContext();
202 sharedContext.setContext_id("test_contextid");
203 sharedContext.setCkey("test_ckey");
204 Mockito.when(contextService.getSharedContext(Matchers.any(), Matchers.any())).thenReturn(sharedContext);
205 String result = sharedContextRestController.getContext(mockedRequest, "12","test");
206 assertNotNull(result);
210 public void getContextTestWithContextNull() throws Exception{
211 SharedContext sharedContext=new SharedContext();
212 sharedContext.setContext_id("test_contextid");
213 sharedContext.setCkey("test_ckey");
214 Mockito.when(contextService.getSharedContext(Matchers.any(), Matchers.any())).thenReturn(null);
215 String result = sharedContextRestController.getContext(mockedRequest, "12","test");
216 assertNotNull(result);
219 @Test(expected=Exception.class)
220 public void getContextTestWithException() throws Exception{
221 sharedContextRestController.getContext(mockedRequest, null,null);
224 @Test(expected=Exception.class)
225 public void getUserContextTest() throws Exception{
226 sharedContextRestController.getUserContext(mockedRequest, null);
230 public void getUserContextTestWithContext() throws Exception{
231 PowerMockito.mock(SharedContext.class);
232 SharedContext sharedContext=new SharedContext();
233 sharedContext.setContext_id("test_contextid");
234 sharedContext.setCkey("test_ckey");
235 List<SharedContext> listSharedContext = new ArrayList<SharedContext>();
236 listSharedContext.add(sharedContext);
237 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(sharedContext);
238 String response=sharedContextRestController.getUserContext(mockedRequest, "12");
239 assertNotNull(response);
243 public void checkContextTest() throws Exception{
244 SharedContext sharedContext=new SharedContext();
245 sharedContext.setContext_id("test_contextid");
246 sharedContext.setCkey("test_ckey");
247 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(sharedContext);
248 String response=sharedContextRestController.checkContext(mockedRequest, "12","test");
249 assertNotNull(response);
252 @Test(expected=Exception.class)
253 public void checkContextTestWithContextIdNull() throws Exception{
254 SharedContext sharedContext=new SharedContext();
255 sharedContext.setContext_id("test_contextid");
256 sharedContext.setCkey("test_ckey");
257 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(sharedContext);
258 sharedContextRestController.checkContext(mockedRequest, null,null);
262 public void removeContextTest() throws Exception{
263 SharedContext sharedContext=new SharedContext();
264 sharedContext.setContext_id("test_contextid");
265 sharedContext.setCkey("test_ckey");
266 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(sharedContext);
268 //Mockito.when(contextService.deleteSharedContext(sharedContext));
269 String actual=sharedContextRestController.removeContext(mockedRequest, "12","test");
270 assertNotNull(actual);
274 @Test(expected=Exception.class)
275 public void removeContextTestWithContextNull() throws Exception{
276 SharedContext sharedContext=new SharedContext();
277 sharedContext.setContext_id("test_contextid");
278 sharedContext.setCkey("test_ckey");
279 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(sharedContext);
281 //Mockito.when(contextService.deleteSharedContext(sharedContext));
282 String actual=sharedContextRestController.removeContext(mockedRequest, null,null);
283 assertNotNull(actual);
287 @Test(expected=Exception.class)
288 public void clearContextTestwithContextIdNull() throws Exception{
290 Mockito.when(contextService.deleteSharedContexts(Matchers.any())).thenReturn(12);
292 String actual=sharedContextRestController.clearContext(mockedRequest,null);
293 assertNotNull(actual);
298 public void clearContextTest() throws Exception{
300 Mockito.when(contextService.deleteSharedContexts(Matchers.any())).thenReturn(12);
302 String actual=sharedContextRestController.clearContext(mockedRequest,"12");
303 assertNotNull(actual);
308 public void setContextTest() throws Exception{
309 ObjectMapper mapper = new ObjectMapper();
310 Map<String, Object> userData = new HashMap<String, Object>();
311 userData.put("context_id", "test_contextId");
312 userData.put("ckey", "test_ckey");
313 userData.put("cvalue", "test_cvalue");
314 //String testUserJson=Matchers.anyString();
315 JSONObject testUserJson = new JSONObject();
316 testUserJson.put("context_id", "test1ContextId");
317 testUserJson.put("ckey", "testCkey");
318 testUserJson.put("cvalue", "testCValue");
319 Map<String, Object> userData1 = mapper.readValue(testUserJson.toString(), Map.class);
320 SharedContext sharedContext=new SharedContext();
321 sharedContext.setContext_id("test_contextid");
322 sharedContext.setCkey("test_ckey");
323 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(sharedContext);
324 // Mockito.when(mapper.readValue("true", Map.class)).thenReturn(userData);
325 String actual=sharedContextRestController.setContext(mockedRequest,testUserJson.toString());
329 @Test(expected=Exception.class)
330 public void setContextTestWithContextNull() throws Exception{
331 ObjectMapper mapper = new ObjectMapper();
332 Map<String, Object> userData = new HashMap<String, Object>();
333 userData.put("context_id", "test_contextId");
334 userData.put("ckey", "test_ckey");
335 userData.put("cvalue", "test_cvalue");
336 //String testUserJson=Matchers.anyString();
337 JSONObject testUserJson = new JSONObject();
338 testUserJson.put("context_id", "test1ContextId");
339 testUserJson.put("ckey", "testCkey");
340 testUserJson.put("cvalue", "testCValue");
341 Map<String, Object> userData1 = mapper.readValue(testUserJson.toString(), Map.class);
342 SharedContext sharedContext=new SharedContext();
343 sharedContext.setContext_id("test_contextid");
344 sharedContext.setCkey("test_ckey");
345 Mockito.when(contextService.getSharedContext(Matchers.any(),Matchers.any())).thenReturn(null);
346 Mockito.when(userData1.get(ckey)).thenReturn(null);
347 Mockito.when(userData1.get(cxid)).thenReturn(null);
349 // Mockito.when(mapper.readValue("true", Map.class)).thenReturn(userData);
350 String actual=sharedContextRestController.setContext(mockedRequest,testUserJson.toString());