Modify ONAP PAP REST classes basic checkstyle
[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 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 package org.onap.policy.pap.xacml.rest.controller;
21
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;
27
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;
33
34 import javax.servlet.http.HttpServletRequest;
35
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;
46
47 public class ActionPolicyDictionaryControllerTest {
48
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;
55
56     @Before
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<>();
62         data.add("Test");
63
64         UserInfo userInfo = new UserInfo();
65         userInfo.setUserLoginId("Test");
66         userInfo.setUserName("Test");
67
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);
89
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");
103     }
104
105     @Test
106     public void testGetActionEntitybyName(){
107         controller.getActionEntitybyName(response);
108         try {
109             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
110         } catch (Exception e) {
111             fail();
112             logger.error(e.getMessage(),e);
113         }
114     }
115
116     @Test
117     public void testGetActionPolicyDictionaryEntityData(){
118         controller.getActionPolicyDictionaryEntityData(response);
119         try {
120             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
121         } catch (Exception e) {
122             fail();
123             logger.error(e.getMessage(),e);
124         }
125     }
126
127     @Test
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"));
134         }catch(Exception e){
135             logger.error("Exception"+ e);
136         }
137     }
138
139     @Test
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"));
146         }catch(Exception e){
147             logger.error("Exception"+ e);
148         }
149     }
150
151     @Test
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"));
158         }catch(Exception e){
159             logger.error("Exception"+ e);
160         }
161     }
162 }