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.when;
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;
33 import javax.servlet.http.HttpServletRequest;
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.DescriptiveScope;
42 import org.onap.policy.rest.jpa.UserInfo;
43 import org.springframework.mock.web.MockHttpServletResponse;
45 public class DescriptiveDictionaryControllerTest {
47 private static Logger logger = FlexLogger.getLogger(DescriptiveDictionaryControllerTest.class);
48 private static CommonClassDao commonClassDao;
49 private String jsonString = null;
50 private HttpServletRequest request = null;
51 private DescriptiveDictionaryController controller = null;
52 private MockHttpServletResponse response = null;
53 private UserInfo userInfo;
54 private List<String> data;
57 public void setUp() throws Exception {
58 logger.info("setUp: Entering");
59 commonClassDao = Mockito.mock(CommonClassDao.class);
61 data = new ArrayList<>();
64 userInfo = new UserInfo();
65 userInfo.setUserLoginId("Test");
66 userInfo.setUserName("Test");
68 doNothing().when(commonClassDao).delete(new DescriptiveScope());
69 doNothing().when(commonClassDao).save(new DescriptiveScope());
71 controller = new DescriptiveDictionaryController();
72 controller.setCommonClassDao(commonClassDao);
74 request = Mockito.mock(HttpServletRequest.class);
75 response = new MockHttpServletResponse();
76 logger.info("setUp: exit");
79 public List<Object> testDescriptiveScope(){
80 List<Object> objectData = new ArrayList<>();
82 DescriptiveScope data = new DescriptiveScope();
84 assertTrue(1 == data.getId());
85 data.setScopeName("Test");
86 assertTrue("Test".equals(data.getScopeName()));
87 data.setDescription("Test");
88 assertTrue("Test".equals(data.getDescription()));
89 data.setSearch("Test");
90 assertTrue("Test".equals(data.getSearch()));
91 data.setCreatedDate(new Date());
92 assertTrue(data.getCreatedDate()!= null);
93 data.setModifiedDate(new Date());
94 assertTrue(data.getModifiedDate()!= null);
95 data.setUserCreatedBy(userInfo);
96 assertTrue(data.getUserCreatedBy()!= null);
97 data.setUserModifiedBy(userInfo);
98 assertTrue(data.getUserModifiedBy()!= null);
105 public void testGetDescriptiveDictionaryByNameEntityData(){
106 when(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")).thenReturn(data);
107 controller.getDescriptiveDictionaryByNameEntityData(response);
109 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
110 } catch (Exception e) {
112 logger.error(e.getMessage(),e);
117 public void testGetDescriptiveDictionaryEntityData(){
118 when(commonClassDao.getData(DescriptiveScope.class)).thenReturn(testDescriptiveScope());
119 controller.getDescriptiveDictionaryEntityData(response);
121 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
122 } catch (Exception e) {
124 logger.error(e.getMessage(),e);
129 public void testSaveDescriptiveDictionary(){
130 jsonString = "{\"descriptiveScopeDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"search\":\"Test\"},\"userid\":\"demo\"}";
131 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
132 when(request.getReader()).thenReturn(br);
133 controller.saveDescriptiveDictionary(request, response);
134 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
136 logger.error("Exception"+ e);
141 public void testUpdateDescriptiveDictionary(){
142 jsonString = "{\"descriptiveScopeDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"id\":1,\"search\":\"Test\"},\"userid\":\"demo\"}";
143 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
144 when(request.getReader()).thenReturn(br);
145 controller.saveDescriptiveDictionary(request, response);
146 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
148 logger.error("Exception"+ e);
153 public void testRemoveDescriptiveDictionary(){
154 jsonString = "{\"data\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"descriptiveScopeName\":\"Test\",\"id\":1,\"search\":\"Test\"},\"userid\":\"demo\"}";
155 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
156 when(request.getReader()).thenReturn(br);
157 controller.removeDescriptiveDictionary(request, response);
158 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
160 logger.error("Exception"+ e);