1854105323244ec83fa25db5cfd8e2eac11ae56f
[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 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.DescriptiveScope;
42 import org.onap.policy.rest.jpa.UserInfo;
43 import org.springframework.mock.web.MockHttpServletResponse;
44
45 public class DescriptiveDictionaryControllerTest {
46         
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;
55
56         @Before
57         public void setUp() throws Exception {
58                 logger.info("setUp: Entering");
59                 commonClassDao = Mockito.mock(CommonClassDao.class);
60                 
61                 data = new ArrayList<>();
62                 data.add("Test");
63                 
64                 userInfo = new UserInfo();
65                 userInfo.setUserLoginId("Test");
66                 userInfo.setUserName("Test");
67         
68                 doNothing().when(commonClassDao).delete(new DescriptiveScope());
69                 doNothing().when(commonClassDao).save(new DescriptiveScope());
70                 
71                 controller = new DescriptiveDictionaryController();
72                 controller.setCommonClassDao(commonClassDao);
73                 
74                 request = Mockito.mock(HttpServletRequest.class);
75                 response =  new MockHttpServletResponse();  
76                 logger.info("setUp: exit");
77         }
78         
79         public List<Object> testDescriptiveScope(){
80                 List<Object>  objectData = new ArrayList<>();
81                 
82                 DescriptiveScope data = new DescriptiveScope();
83                 data.setId(1);
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);
99                 objectData.add(data);
100                 
101                 return objectData;
102         }
103         
104         @Test
105         public void testGetDescriptiveDictionaryByNameEntityData(){
106                 when(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")).thenReturn(data);
107                 controller.getDescriptiveDictionaryByNameEntityData(response);
108                 try {
109                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
110                 } catch (Exception e) {
111                         fail();
112                         logger.error(e.getMessage(),e);
113                 }
114         }
115         
116         @Test
117         public void testGetDescriptiveDictionaryEntityData(){
118                 when(commonClassDao.getData(DescriptiveScope.class)).thenReturn(testDescriptiveScope());
119                 controller.getDescriptiveDictionaryEntityData(response);
120                 try {
121                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
122                 } catch (Exception e) {
123                         fail();
124                         logger.error(e.getMessage(),e);
125                 }
126         }
127         
128         @Test
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"));
135                 }catch(Exception e){
136                         logger.error("Exception"+ e);
137                 } 
138         }
139         
140         @Test
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"));
147                 }catch(Exception e){
148                         logger.error("Exception"+ e);
149                 } 
150         }
151         
152         @Test
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"));
159                 }catch(Exception e){
160                         logger.error("Exception"+ e);
161                 } 
162         }
163 }