Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / ActionPolicyDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
34
35 import javax.servlet.http.HttpServletRequest;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
43 import org.onap.policy.rest.dao.CommonClassDao;
44 import org.onap.policy.rest.jpa.ActionPolicyDict;
45 import org.onap.policy.rest.jpa.UserInfo;
46 import org.springframework.mock.web.MockHttpServletResponse;
47
48 public class ActionPolicyDictionaryControllerTest {
49
50     private static Logger logger = FlexLogger.getLogger(ActionPolicyDictionaryControllerTest.class);
51     private static CommonClassDao commonClassDao;
52     private String jsonString = null;
53     private HttpServletRequest request = null;
54     private ActionPolicyDictionaryController controller = null;
55     private MockHttpServletResponse response = null;
56
57     @Before
58     public void setUp() throws Exception {
59         logger.info("setUp: Entering");
60         commonClassDao = Mockito.mock(CommonClassDao.class);
61         List<String> data = new ArrayList<>();
62         List<Object> objectData = new ArrayList<>();
63         data.add("Test");
64
65         UserInfo userInfo = new UserInfo();
66         userInfo.setUserLoginId("Test");
67         userInfo.setUserName("Test");
68
69         ActionPolicyDict actionData = new ActionPolicyDict();
70         actionData.setAttributeName("Test");
71         assertTrue("Test".equals(actionData.getAttributeName()));
72         actionData.setBody("Test");
73         assertTrue("Test".equals(actionData.getBody()));
74         actionData.setCreatedDate(new Date());
75         assertTrue(actionData.getCreatedDate() != null);
76         actionData.setModifiedDate(new Date());
77         assertTrue(actionData.getModifiedDate() != null);
78         actionData.setHeader("Test");
79         assertTrue("Test".equals(actionData.getHeader()));
80         actionData.setMethod("POST");
81         assertTrue("POST".equals(actionData.getMethod()));
82         actionData.setType("Test");
83         assertTrue("Test".equals(actionData.getType()));
84         actionData.setUrl("http://test.com");
85         assertTrue("http://test.com".equals(actionData.getUrl()));
86         actionData.setUserCreatedBy(userInfo);
87         assertTrue(actionData.getUserCreatedBy() != null);
88         actionData.setUserModifiedBy(userInfo);
89         assertTrue(actionData.getUserModifiedBy() != null);
90
91         objectData.add(actionData);
92         when(commonClassDao.getDataByColumn(ActionPolicyDict.class, "attributeName")).thenReturn(data);
93         when(commonClassDao.getData(ActionPolicyDict.class)).thenReturn(objectData);
94         doNothing().when(commonClassDao).delete(new ActionPolicyDict());
95         doNothing().when(commonClassDao).save(new ActionPolicyDict());
96         controller = new ActionPolicyDictionaryController();
97         controller.setCommonClassDao(commonClassDao);
98         request = Mockito.mock(HttpServletRequest.class);
99         response = new MockHttpServletResponse();
100         new DictionaryUtils(commonClassDao);
101         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
102         mock(DictionaryUtils.class);
103         logger.info("setUp: exit");
104     }
105
106     @Test
107     public void testGetActionEntitybyName() {
108         controller.getActionEntitybyName(response);
109         try {
110             assertTrue(response.getContentAsString() != null
111                     && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
112         } catch (Exception e) {
113             fail();
114             logger.error(e.getMessage(), e);
115         }
116     }
117
118     @Test
119     public void testGetActionPolicyDictionaryEntityData() {
120         controller.getActionPolicyDictionaryEntityData(response);
121         try {
122             assertTrue(response.getContentAsString() != null
123                     && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
124         } catch (Exception e) {
125             fail();
126             logger.error(e.getMessage(), e);
127         }
128     }
129
130     @Test
131     public void testSaveActionPolicyDictionary() {
132         jsonString =
133                 "{\"actionPolicyDictionaryData\":{\"attributeName\":\"Test\",\"body\":\"{}\",\"description\":"
134                 + "\"test\",\"headers\":[{\"$$hashKey\":\"object:548\",\"id\":\"choice1\",\"number\":\"12\","
135                 + "\"option\":\"test\"}],\"method\":\"GET\",\"type\":\"REST\",\"url\":\"http://test.com\"},"
136                 + "\"userid\":\"demo\"}";
137         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
138             when(request.getReader()).thenReturn(br);
139             controller.saveActionPolicyDictionary(request, response);
140             assertTrue(response.getContentAsString() != null
141                     && response.getContentAsString().contains("actionPolicyDictionaryData"));
142         } catch (Exception e) {
143             logger.error("Exception" + e);
144         }
145     }
146
147     @Test
148     public void testUpdateActionPolicyDictionary() {
149         jsonString =
150                 "{\"actionPolicyDictionaryData\":{\"id\":1,\"attributeName\":\"Test\",\"body\":\"{}\",\"description\":"
151                 + "\"test\",\"headers\":[{\"$$hashKey\":\"object:548\",\"id\":\"choice1\",\"number\":"
152                 + "\"12\",\"option\":\"test\"}],\"method\":\"GET\",\"type\":\"REST\",\"url\":\"http://test.com\"},"
153                 + "\"userid\":\"demo\"}";
154         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
155             when(request.getReader()).thenReturn(br);
156             controller.saveActionPolicyDictionary(request, response);
157             assertTrue(response.getContentAsString() != null
158                     && response.getContentAsString().contains("actionPolicyDictionaryData"));
159         } catch (Exception e) {
160             logger.error("Exception" + e);
161         }
162     }
163
164     @Test
165     public void testRemoveActionPolicyDictionary() {
166         jsonString =
167                 "{\"data\":{\"$$hashKey\":\"uiGrid-003S\",\"attributeName\":\"Test\",\"body\":\"{}\",\"createdDate\":"
168                 + "1518195117000,\"description\":\"test\",\"header\":\"test=12\",\"id\":1,\"method\":\"GET\","
169                 + "\"modifiedDate\":1518195489000,\"type\":\"REST\",\"url\":\"http://test.com\",\"userCreatedBy\":"
170                 + "{\"userLoginId\":\"demo\",\"userName\":\"Demo\"},\"userModifiedBy\":{\"userLoginId\":\"demo\","
171                 + "\"userName\":\"Demo\"}}}";
172         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
173             when(request.getReader()).thenReturn(br);
174             controller.removeActionPolicyDictionary(request, response);
175             assertTrue(response.getContentAsString() != null
176                     && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
177         } catch (Exception e) {
178             logger.error("Exception" + e);
179         }
180     }
181 }