2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
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;
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;
34 import javax.servlet.http.HttpServletRequest;
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.DescriptiveScope;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.springframework.mock.web.MockHttpServletResponse;
47 public class DescriptiveDictionaryControllerTest {
49 private static Logger logger = FlexLogger.getLogger(DescriptiveDictionaryControllerTest.class);
50 private static CommonClassDao commonClassDao;
51 private String jsonString = null;
52 private HttpServletRequest request = null;
53 private DescriptiveDictionaryController controller = null;
54 private MockHttpServletResponse response = null;
55 private UserInfo userInfo;
56 private List<String> data;
59 public void setUp() throws Exception {
60 logger.info("setUp: Entering");
61 commonClassDao = Mockito.mock(CommonClassDao.class);
63 data = new ArrayList<>();
66 userInfo = new UserInfo();
67 userInfo.setUserLoginId("Test");
68 userInfo.setUserName("Test");
70 doNothing().when(commonClassDao).delete(new DescriptiveScope());
71 doNothing().when(commonClassDao).save(new DescriptiveScope());
73 controller = new DescriptiveDictionaryController();
74 controller.setCommonClassDao(commonClassDao);
76 request = Mockito.mock(HttpServletRequest.class);
77 response = new MockHttpServletResponse();
78 new DictionaryUtils(commonClassDao);
79 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
80 mock(DictionaryUtils.class);
81 logger.info("setUp: exit");
84 public List<Object> testDescriptiveScope(){
85 List<Object> objectData = new ArrayList<>();
87 DescriptiveScope data = new DescriptiveScope();
89 assertTrue(1 == data.getId());
90 data.setScopeName("Test");
91 assertTrue("Test".equals(data.getScopeName()));
92 data.setDescription("Test");
93 assertTrue("Test".equals(data.getDescription()));
94 data.setSearch("Test");
95 assertTrue("Test".equals(data.getSearch()));
96 data.setCreatedDate(new Date());
97 assertTrue(data.getCreatedDate()!= null);
98 data.setModifiedDate(new Date());
99 assertTrue(data.getModifiedDate()!= null);
100 data.setUserCreatedBy(userInfo);
101 assertTrue(data.getUserCreatedBy()!= null);
102 data.setUserModifiedBy(userInfo);
103 assertTrue(data.getUserModifiedBy()!= null);
104 objectData.add(data);
110 public void testGetDescriptiveDictionaryByNameEntityData(){
111 when(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")).thenReturn(data);
112 controller.getDescriptiveDictionaryByNameEntityData(response);
114 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
115 } catch (Exception e) {
117 logger.error(e.getMessage(),e);
122 public void testGetDescriptiveDictionaryEntityData(){
123 when(commonClassDao.getData(DescriptiveScope.class)).thenReturn(testDescriptiveScope());
124 controller.getDescriptiveDictionaryEntityData(response);
126 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
127 } catch (Exception e) {
129 logger.error(e.getMessage(),e);
134 public void testSaveDescriptiveDictionary(){
135 jsonString = "{\"descriptiveScopeDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"search\":\"Test\"},\"userid\":\"demo\"}";
136 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
137 when(request.getReader()).thenReturn(br);
138 controller.saveDescriptiveDictionary(request, response);
139 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
141 logger.error("Exception"+ e);
146 public void testUpdateDescriptiveDictionary(){
147 jsonString = "{\"descriptiveScopeDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"id\":1,\"search\":\"Test\"},\"userid\":\"demo\"}";
148 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
149 when(request.getReader()).thenReturn(br);
150 controller.saveDescriptiveDictionary(request, response);
151 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
153 logger.error("Exception"+ e);
158 public void testRemoveDescriptiveDictionary(){
159 jsonString = "{\"data\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"id\":1,\"search\":\"Test\"},\"userid\":\"demo\"}";
160 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
161 when(request.getReader()).thenReturn(br);
162 controller.removeDescriptiveDictionary(request, response);
163 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
165 logger.error("Exception"+ e);