5310ddc931850ad0074d2c64d9ecc2b43e791886
[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.mock;
26 import static org.mockito.Mockito.when;
27
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;
33
34 import javax.servlet.http.HttpServletRequest;
35
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;
46
47 public class DescriptiveDictionaryControllerTest {
48         
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;
57
58         @Before
59         public void setUp() throws Exception {
60                 logger.info("setUp: Entering");
61                 commonClassDao = Mockito.mock(CommonClassDao.class);
62                 
63                 data = new ArrayList<>();
64                 data.add("Test");
65                 
66                 userInfo = new UserInfo();
67                 userInfo.setUserLoginId("Test");
68                 userInfo.setUserName("Test");
69         
70                 doNothing().when(commonClassDao).delete(new DescriptiveScope());
71                 doNothing().when(commonClassDao).save(new DescriptiveScope());
72                 
73                 controller = new DescriptiveDictionaryController();
74                 controller.setCommonClassDao(commonClassDao);
75                 
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");
82         }
83         
84         public List<Object> testDescriptiveScope(){
85                 List<Object>  objectData = new ArrayList<>();
86                 
87                 DescriptiveScope data = new DescriptiveScope();
88                 data.setId(1);
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);
105                 
106                 return objectData;
107         }
108         
109         @Test
110         public void testGetDescriptiveDictionaryByNameEntityData(){
111                 when(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")).thenReturn(data);
112                 controller.getDescriptiveDictionaryByNameEntityData(response);
113                 try {
114                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
115                 } catch (Exception e) {
116                         fail();
117                         logger.error(e.getMessage(),e);
118                 }
119         }
120         
121         @Test
122         public void testGetDescriptiveDictionaryEntityData(){
123                 when(commonClassDao.getData(DescriptiveScope.class)).thenReturn(testDescriptiveScope());
124                 controller.getDescriptiveDictionaryEntityData(response);
125                 try {
126                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
127                 } catch (Exception e) {
128                         fail();
129                         logger.error(e.getMessage(),e);
130                 }
131         }
132         
133         @Test
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"));
140                 }catch(Exception e){
141                         logger.error("Exception"+ e);
142                 } 
143         }
144         
145         @Test
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"));
152                 }catch(Exception e){
153                         logger.error("Exception"+ e);
154                 } 
155         }
156         
157         @Test
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"));
164                 }catch(Exception e){
165                         logger.error("Exception"+ e);
166                 } 
167         }
168 }