f12299334783cb371398904e3ae08168f82c688b
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / DescriptiveDictionaryControllerTest.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.DescriptiveScope;
45 import org.onap.policy.rest.jpa.UserInfo;
46 import org.springframework.mock.web.MockHttpServletResponse;
47
48 public class DescriptiveDictionaryControllerTest {
49
50     private static Logger logger = FlexLogger.getLogger(DescriptiveDictionaryControllerTest.class);
51     private static CommonClassDao commonClassDao;
52     private String jsonString = null;
53     private HttpServletRequest request = null;
54     private DescriptiveDictionaryController controller = null;
55     private MockHttpServletResponse response = null;
56     private UserInfo userInfo;
57     private List<String> data;
58
59     @Before
60     public void setUp() throws Exception {
61         logger.info("setUp: Entering");
62         commonClassDao = Mockito.mock(CommonClassDao.class);
63
64         data = new ArrayList<>();
65         data.add("Test");
66
67         userInfo = new UserInfo();
68         userInfo.setUserLoginId("Test");
69         userInfo.setUserName("Test");
70
71         doNothing().when(commonClassDao).delete(new DescriptiveScope());
72         doNothing().when(commonClassDao).save(new DescriptiveScope());
73
74         controller = new DescriptiveDictionaryController();
75         controller.setCommonClassDao(commonClassDao);
76
77         request = Mockito.mock(HttpServletRequest.class);
78         response = new MockHttpServletResponse();
79         new DictionaryUtils(commonClassDao);
80         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
81         mock(DictionaryUtils.class);
82         logger.info("setUp: exit");
83     }
84
85     public List<Object> testDescriptiveScope() {
86         List<Object> objectData = new ArrayList<>();
87
88         DescriptiveScope data = new DescriptiveScope();
89         data.setId(1);
90         assertTrue(1 == data.getId());
91         data.setScopeName("Test");
92         assertTrue("Test".equals(data.getScopeName()));
93         data.setDescription("Test");
94         assertTrue("Test".equals(data.getDescription()));
95         data.setSearch("Test");
96         assertTrue("Test".equals(data.getSearch()));
97         data.setCreatedDate(new Date());
98         assertTrue(data.getCreatedDate() != null);
99         data.setModifiedDate(new Date());
100         assertTrue(data.getModifiedDate() != null);
101         data.setUserCreatedBy(userInfo);
102         assertTrue(data.getUserCreatedBy() != null);
103         data.setUserModifiedBy(userInfo);
104         assertTrue(data.getUserModifiedBy() != null);
105         objectData.add(data);
106
107         return objectData;
108     }
109
110     @Test
111     public void testGetDescriptiveDictionaryByNameEntityData() {
112         when(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")).thenReturn(data);
113         controller.getDescriptiveDictionaryByNameEntityData(response);
114         try {
115             assertTrue(response.getContentAsString() != null
116                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
117         } catch (Exception e) {
118             fail();
119             logger.error(e.getMessage(), e);
120         }
121     }
122
123     @Test
124     public void testGetDescriptiveDictionaryEntityData() {
125         when(commonClassDao.getData(DescriptiveScope.class)).thenReturn(testDescriptiveScope());
126         controller.getDescriptiveDictionaryEntityData(response);
127         try {
128             assertTrue(response.getContentAsString() != null
129                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
130         } catch (Exception e) {
131             fail();
132             logger.error(e.getMessage(), e);
133         }
134     }
135
136     @Test
137     public void testSaveDescriptiveDictionary() {
138         jsonString =
139                 "{\"descriptiveScopeDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":"
140                 + "\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\","
141                 + "\"descriptiveScopeName\":\"Test\",\"search\":\"Test\"},\"userid\":\"demo\"}";
142         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
143             when(request.getReader()).thenReturn(br);
144             controller.saveDescriptiveDictionary(request, response);
145             assertTrue(response.getContentAsString() != null
146                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
147         } catch (Exception e) {
148             logger.error("Exception" + e);
149         }
150     }
151
152     @Test
153     public void testUpdateDescriptiveDictionary() {
154         jsonString =
155                 "{\"descriptiveScopeDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":"
156                 + "\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\","
157                 + "\"descriptiveScopeName\":\"Test\",\"id\":1,\"search\":\"Test\"},\"userid\":\"demo\"}";
158         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
159             when(request.getReader()).thenReturn(br);
160             controller.saveDescriptiveDictionary(request, response);
161             assertTrue(response.getContentAsString() != null
162                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
163         } catch (Exception e) {
164             logger.error("Exception" + e);
165         }
166     }
167
168     @Test
169     public void testRemoveDescriptiveDictionary() {
170         jsonString =
171                 "{\"data\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\","
172                 + "\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"id\":1,"
173                 + "\"search\":\"Test\"},\"userid\":\"demo\"}";
174         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
175             when(request.getReader()).thenReturn(br);
176             controller.removeDescriptiveDictionary(request, response);
177             assertTrue(response.getContentAsString() != null
178                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
179         } catch (Exception e) {
180             logger.error("Exception" + e);
181         }
182     }
183 }