d990b90027bcb5946b81a3020a667d9d9ac4496f
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / OptimizationDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.xacml.rest.controller;
24
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import com.mockrunner.mock.web.MockHttpServletRequest;
35 import java.io.BufferedReader;
36 import java.io.StringReader;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.core.Response;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
45 import org.onap.policy.rest.dao.CommonClassDao;
46 import org.onap.policy.rest.jpa.OptimizationModels;
47 import org.onap.policy.rest.jpa.UserInfo;
48 import org.springframework.mock.web.MockHttpServletResponse;
49
50 public class OptimizationDictionaryControllerTest {
51
52     private static Logger logger = FlexLogger.getLogger(OptimizationDictionaryControllerTest.class);
53     private static CommonClassDao commonClassDao;
54     private String jsonString = null;
55     private HttpServletRequest request = null;
56     private OptimizationDictionaryController controller = null;
57     BufferedReader br = null;
58
59     @Before
60     public void setUp() throws Exception {
61         logger.info("setUp: Entering");
62         commonClassDao = Mockito.mock(CommonClassDao.class);
63         UserInfo userInfo = new UserInfo();
64         userInfo.setUserLoginId("testUserId");
65         userInfo.setUserName("John");
66         when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "testing")).thenReturn(userInfo);
67
68         OptimizationModels optimziationModels = new OptimizationModels();
69
70         doNothing().when(commonClassDao).delete(optimziationModels);
71
72         OptimizationDictionaryController.setCommonClassDao(commonClassDao);
73
74         controller = new OptimizationDictionaryController();
75
76         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
77
78         jsonString = "{\"optimizationModelsDictionaryData\": {\"modelName\": \"test\",\"inprocess\": false,\"model\":"
79             + " {\"name\": \"testingdata\",\"subScopename\": \"\",\"path\": [],\"type\": \"dir\","
80             + "\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", \"version\": \"\","
81             + "\"createdBy\": \"someone\",\"modifiedBy\": \"someone\",\"content\": \"\"," + "\"recursive\": false},"
82             + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
83             + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
84             + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
85             + "\"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
86             + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
87             + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
88             + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
89
90         br = new BufferedReader(new StringReader(jsonString));
91         // --- mock the getReader() call
92         when(request.getReader()).thenReturn(br);
93         new DictionaryUtils(commonClassDao);
94         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
95         mock(DictionaryUtils.class);
96         logger.info("setUp: exit");
97     }
98
99     @Test
100     public void testGetOptimizationModelsDictionaryEntityData() {
101         logger.info("testGetOptimizationModelsDictionaryEntityData: Entering");
102
103         MockHttpServletResponse response = new MockHttpServletResponse();
104         String modelJson = "{\"optimizationModelsDictionaryData\":[\"modelName\"]}";
105
106         BufferedReader br = new BufferedReader(new StringReader(modelJson));
107         request = mock(HttpServletRequest.class);
108
109         try {
110             // mock the getReader() call
111             when(request.getReader()).thenReturn(br);
112             controller.getOptimizationModelsDictionaryEntityData(response);
113             logger.info("response.getContentAsString(): " + response.getContentAsString());
114             assertTrue(response.getContentAsString() != null
115                 && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
116
117         } catch (Exception e) {
118             fail("Exception: " + e);
119         }
120
121         logger.info("testGetOptimizationModelsDictionaryEntityData: exit");
122     }
123
124     @Test
125     public void testSaveOptimizationModelsDictionary() {
126         logger.info("testSaveOptimizationModelsDictionary: Entering");
127
128         MockHttpServletResponse response = new MockHttpServletResponse();
129         request = mock(HttpServletRequest.class);
130
131         try {
132             // mock the getReader() call
133             when(request.getReader()).thenReturn(br);
134             controller.saveOptimizationModelsDictionary(request, response);
135             logger.info("response.getContentAsString(): " + response.getContentAsString());
136             assertTrue(response.getContentAsString() != null
137                 && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
138
139         } catch (Exception e) {
140             fail("Exception: " + e);
141         }
142
143         logger.info("testSaveOptimizationModelsDictionary: exit");
144     }
145
146     @Test
147     public void testRemoveOptimizationModelsDictionary() {
148         logger.info("testRemoveOptimizationModelsDictionary: Entering");
149
150         MockHttpServletResponse response = new MockHttpServletResponse();
151         request = mock(HttpServletRequest.class);
152
153         try {
154             // mock the getReader() call
155             jsonString =
156                 "{\"data\": {\"modelName\": \"test\",\"inprocess\": false,\"model\": {\"name\": \"testingdata\","
157                     + "\"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,"
158                     + "\"date\": \"2017-04-12T21:26:57.000Z\",\"version\": \"\",\"createdBy\": \"someone\","
159                     + "\"modifiedBy\": \"someone\",\"content\": \"\",\"recursive\": false},"
160                     + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
161                     + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
162                     + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
163                     + "\"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
164                     + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
165                     + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
166                     + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
167
168             BufferedReader br = new BufferedReader(new StringReader(jsonString));
169             when(request.getReader()).thenReturn(br);
170             controller.removeOptimizationModelsDictionary(request, response);
171             logger.info("response.getContentAsString(): " + response.getContentAsString());
172             assertTrue(response.getContentAsString() != null
173                 && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
174
175         } catch (Exception e) {
176             fail("Exception: " + e);
177         }
178         logger.info("testRemoveOptimizationModelsDictionary: exit");
179     }
180
181     @Test
182     public void testGet() {
183         OptimizationDictionaryController controller = new OptimizationDictionaryController(commonClassDao);
184         MockHttpServletResponse response = new MockHttpServletResponse();
185         controller.getOptimizationModelsDictionaryByNameEntityData(response);
186         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
187     }
188
189     @Test
190     public void testSave() {
191         OptimizationDictionaryController controller = new OptimizationDictionaryController(commonClassDao);
192         MockHttpServletRequest req = new MockHttpServletRequest();
193         MockHttpServletResponse response = new MockHttpServletResponse();
194         req.setBodyContent("{\n\"modelType\": \"type.yml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", "
195             + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, "
196             + "\"classMap\": \"{\\\"dep\\\":\\\"{\\\"dependency\\\":\\\"depval\\\"}\\\"}\" }\n");
197         // + "\"classMap\": \"{\\\"dep\\\":\\\"dependency\\\"}\" }\n");
198         assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response))
199             .isInstanceOf(NullPointerException.class);
200
201         req.setBodyContent("{\n\"modelType\": \"type.xml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", "
202             + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, "
203             + "\"classMap\": \"{\\\"dep\\\": {\\\"dependency\\\":\\\"depval\\\"} }\" }\n");
204         assertThatCode(() -> controller.saveOptimizationModelsDictionary(req, response)).doesNotThrowAnyException();
205
206         req.setupAddParameter("apiflag", "api");
207         assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response))
208             .isInstanceOf(NullPointerException.class);
209     }
210
211 }