Added Junits for ONAP-PAP-REST
[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.when;
26
27 import java.io.BufferedReader;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
32
33 import javax.servlet.http.HttpServletRequest;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.ActionPolicyDict;
42 import org.onap.policy.rest.jpa.UserInfo;
43 import org.springframework.mock.web.MockHttpServletResponse;
44
45 public class ActionPolicyDictionaryControllerTest {
46
47         private static Logger logger = FlexLogger.getLogger(ActionPolicyDictionaryControllerTest.class);
48         private static CommonClassDao commonClassDao;
49         private String jsonString = null;
50         private HttpServletRequest request = null;
51         private ActionPolicyDictionaryController controller = null;
52         private MockHttpServletResponse response = null;
53
54         @Before
55         public void setUp() throws Exception {
56                 logger.info("setUp: Entering");
57                 commonClassDao = Mockito.mock(CommonClassDao.class);
58                 List<String>  data = new ArrayList<>();
59                 List<Object>  objectData = new ArrayList<>();
60                 data.add("Test");
61                 
62                 UserInfo userInfo = new UserInfo();
63                 userInfo.setUserLoginId("Test");
64                 userInfo.setUserName("Test");
65                 
66                 ActionPolicyDict actionData = new ActionPolicyDict();
67                 actionData.setAttributeName("Test");
68                 assertTrue("Test".equals(actionData.getAttributeName()));
69                 actionData.setBody("Test");
70                 assertTrue("Test".equals(actionData.getBody()));
71                 actionData.setCreatedDate(new Date());
72                 assertTrue(actionData.getCreatedDate()!= null);
73                 actionData.setModifiedDate(new Date());
74                 assertTrue(actionData.getModifiedDate()!= null);
75                 actionData.setHeader("Test");
76                 assertTrue("Test".equals(actionData.getHeader()));
77                 actionData.setMethod("POST");
78                 assertTrue("POST".equals(actionData.getMethod()));
79                 actionData.setType("Test");
80                 assertTrue("Test".equals(actionData.getType()));
81                 actionData.setUrl("http://test.com");
82                 assertTrue("http://test.com".equals(actionData.getUrl()));
83                 actionData.setUserCreatedBy(userInfo);
84                 assertTrue(actionData.getUserCreatedBy()!= null);
85                 actionData.setUserModifiedBy(userInfo);
86                 assertTrue(actionData.getUserModifiedBy()!= null);
87                 
88                 objectData.add(actionData);
89                 when(commonClassDao.getDataByColumn(ActionPolicyDict.class, "attributeName")).thenReturn(data);
90                 when(commonClassDao.getData(ActionPolicyDict.class)).thenReturn(objectData);
91                 doNothing().when(commonClassDao).delete(new ActionPolicyDict());
92                 doNothing().when(commonClassDao).save(new ActionPolicyDict());
93                 controller = new ActionPolicyDictionaryController();
94                 controller.setCommonClassDao(commonClassDao);
95                 request = Mockito.mock(HttpServletRequest.class);
96                 response =  new MockHttpServletResponse();  
97                 logger.info("setUp: exit");
98         }
99         
100         @Test
101         public void testGetActionEntitybyName(){
102                 controller.getActionEntitybyName(response);
103                 try {
104                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
105                 } catch (Exception e) {
106                         fail();
107                         logger.error(e.getMessage(),e);
108                 }
109         }
110         
111         @Test
112         public void testGetActionPolicyDictionaryEntityData(){
113                 controller.getActionPolicyDictionaryEntityData(response);
114                 try {
115                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
116                 } catch (Exception e) {
117                         fail();
118                         logger.error(e.getMessage(),e);
119                 }
120         }
121         
122         @Test
123         public void testSaveActionPolicyDictionary(){
124                 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\"}";
125                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
126                         when(request.getReader()).thenReturn(br);
127                         controller.saveActionPolicyDictionary(request, response);
128                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryData"));
129                 }catch(Exception e){
130                         logger.error("Exception"+ e);
131                 } 
132         }
133         
134         @Test
135         public void testUpdateActionPolicyDictionary(){
136                 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\"}";
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 && response.getContentAsString().contains("actionPolicyDictionaryData"));
141                 }catch(Exception e){
142                         logger.error("Exception"+ e);
143                 } 
144         }
145         
146         @Test
147         public void testRemoveActionPolicyDictionary(){
148                 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\"}}}";
149                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
150                         when(request.getReader()).thenReturn(br);
151                         controller.removeActionPolicyDictionary(request, response);
152                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("actionPolicyDictionaryDatas"));
153                 }catch(Exception e){
154                         logger.error("Exception"+ e);
155                 } 
156         }
157 }