2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
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;
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;
35 import javax.servlet.http.HttpServletRequest;
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;
48 public class DescriptiveDictionaryControllerTest {
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;
60 public void setUp() throws Exception {
61 logger.info("setUp: Entering");
62 commonClassDao = Mockito.mock(CommonClassDao.class);
64 data = new ArrayList<>();
67 userInfo = new UserInfo();
68 userInfo.setUserLoginId("Test");
69 userInfo.setUserName("Test");
71 doNothing().when(commonClassDao).delete(new DescriptiveScope());
72 doNothing().when(commonClassDao).save(new DescriptiveScope());
74 controller = new DescriptiveDictionaryController();
75 controller.setCommonClassDao(commonClassDao);
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");
85 public List<Object> testDescriptiveScope() {
86 List<Object> objectData = new ArrayList<>();
88 DescriptiveScope data = new DescriptiveScope();
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);
111 public void testGetDescriptiveDictionaryByNameEntityData() {
112 when(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")).thenReturn(data);
113 controller.getDescriptiveDictionaryByNameEntityData(response);
115 assertTrue(response.getContentAsString() != null
116 && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
117 } catch (Exception e) {
119 logger.error(e.getMessage(), e);
124 public void testGetDescriptiveDictionaryEntityData() {
125 when(commonClassDao.getData(DescriptiveScope.class)).thenReturn(testDescriptiveScope());
126 controller.getDescriptiveDictionaryEntityData(response);
128 assertTrue(response.getContentAsString() != null
129 && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
130 } catch (Exception e) {
132 logger.error(e.getMessage(), e);
137 public void testSaveDescriptiveDictionary() {
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);
153 public void testUpdateDescriptiveDictionary() {
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);
169 public void testRemoveDescriptiveDictionary() {
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);