2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.doNothing;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
34 import javax.servlet.http.HttpServletRequest;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.ActionPolicyDict;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.springframework.mock.web.MockHttpServletResponse;
47 public class ActionPolicyDictionaryControllerTest {
49 private static Logger logger = FlexLogger.getLogger(ActionPolicyDictionaryControllerTest.class);
50 private static CommonClassDao commonClassDao;
51 private String jsonString = null;
52 private HttpServletRequest request = null;
53 private ActionPolicyDictionaryController controller = null;
54 private MockHttpServletResponse response = null;
57 public void setUp() throws Exception {
58 logger.info("setUp: Entering");
59 commonClassDao = Mockito.mock(CommonClassDao.class);
60 List<String> data = new ArrayList<>();
61 List<Object> objectData = new ArrayList<>();
64 UserInfo userInfo = new UserInfo();
65 userInfo.setUserLoginId("Test");
66 userInfo.setUserName("Test");
68 ActionPolicyDict actionData = new ActionPolicyDict();
69 actionData.setAttributeName("Test");
70 assertTrue("Test".equals(actionData.getAttributeName()));
71 actionData.setBody("Test");
72 assertTrue("Test".equals(actionData.getBody()));
73 actionData.setCreatedDate(new Date());
74 assertTrue(actionData.getCreatedDate()!= null);
75 actionData.setModifiedDate(new Date());
76 assertTrue(actionData.getModifiedDate()!= null);
77 actionData.setHeader("Test");
78 assertTrue("Test".equals(actionData.getHeader()));
79 actionData.setMethod("POST");
80 assertTrue("POST".equals(actionData.getMethod()));
81 actionData.setType("Test");
82 assertTrue("Test".equals(actionData.getType()));
83 actionData.setUrl("http://test.com");
84 assertTrue("http://test.com".equals(actionData.getUrl()));
85 actionData.setUserCreatedBy(userInfo);
86 assertTrue(actionData.getUserCreatedBy()!= null);
87 actionData.setUserModifiedBy(userInfo);
88 assertTrue(actionData.getUserModifiedBy()!= null);
90 objectData.add(actionData);
91 when(commonClassDao.getDataByColumn(ActionPolicyDict.class, "attributeName")).thenReturn(data);
92 when(commonClassDao.getData(ActionPolicyDict.class)).thenReturn(objectData);
93 doNothing().when(commonClassDao).delete(new ActionPolicyDict());
94 doNothing().when(commonClassDao).save(new ActionPolicyDict());
95 controller = new ActionPolicyDictionaryController();
96 controller.setCommonClassDao(commonClassDao);
97 request = Mockito.mock(HttpServletRequest.class);
98 response = new MockHttpServletResponse();
99 new DictionaryUtils(commonClassDao);
100 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
101 mock(DictionaryUtils.class);
102 logger.info("setUp: exit");
106 public void testGetActionEntitybyName(){
107 controller.getActionEntitybyName(response);
109 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
110 } catch (Exception e) {
112 logger.error(e.getMessage(),e);
117 public void testGetActionPolicyDictionaryEntityData(){
118 controller.getActionPolicyDictionaryEntityData(response);
120 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
121 } catch (Exception e) {
123 logger.error(e.getMessage(),e);
128 public void testSaveActionPolicyDictionary(){
129 jsonString = "{\"actionPolicyDictionaryData\":{\"attributeName\":\"Test\",\"body\":\"{}\",\"description\":\"test\",\"headers\":[{\"$$hashKey\":\"object:548\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"method\":\"GET\",\"type\":\"REST\",\"url\":\"http://test.com\"},\"userid\":\"demo\"}";
130 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
131 when(request.getReader()).thenReturn(br);
132 controller.saveActionPolicyDictionary(request, response);
133 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryData"));
135 logger.error("Exception"+ e);
140 public void testUpdateActionPolicyDictionary(){
141 jsonString = "{\"actionPolicyDictionaryData\":{\"id\":1,\"attributeName\":\"Test\",\"body\":\"{}\",\"description\":\"test\",\"headers\":[{\"$$hashKey\":\"object:548\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"method\":\"GET\",\"type\":\"REST\",\"url\":\"http://test.com\"},\"userid\":\"demo\"}";
142 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
143 when(request.getReader()).thenReturn(br);
144 controller.saveActionPolicyDictionary(request, response);
145 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryData"));
147 logger.error("Exception"+ e);
152 public void testRemoveActionPolicyDictionary(){
153 jsonString = "{\"data\":{\"$$hashKey\":\"uiGrid-003S\",\"attributeName\":\"Test\",\"body\":\"{}\",\"createdDate\":1518195117000,\"description\":\"test\",\"header\":\"test=12\",\"id\":1,\"method\":\"GET\",\"modifiedDate\":1518195489000,\"type\":\"REST\",\"url\":\"http://test.com\",\"userCreatedBy\":{\"userLoginId\":\"demo\",\"userName\":\"Demo\"},\"userModifiedBy\":{\"userLoginId\":\"demo\",\"userName\":\"Demo\"}}}";
154 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
155 when(request.getReader()).thenReturn(br);
156 controller.removeActionPolicyDictionary(request, response);
157 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
159 logger.error("Exception"+ e);