Consolidate PolicyRestAdapter setup
[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.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.io.BufferedReader;
32 import java.io.StringReader;
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.pap.xacml.rest.util.DictionaryUtils;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.OptimizationModels;
43 import org.onap.policy.rest.jpa.UserInfo;
44 import org.springframework.mock.web.MockHttpServletResponse;
45
46 public class OptimizationDictionaryControllerTest {
47
48     private static Logger logger = FlexLogger.getLogger(OptimizationDictionaryControllerTest.class);
49     private static CommonClassDao commonClassDao;
50     private String jsonString = null;
51     private HttpServletRequest request = null;
52     private OptimizationDictionaryController controller = null;
53     BufferedReader br = null;
54
55     @Before
56     public void setUp() throws Exception {
57         logger.info("setUp: Entering");
58         commonClassDao = Mockito.mock(CommonClassDao.class);
59         UserInfo userInfo = new UserInfo();
60         userInfo.setUserLoginId("testUserId");
61         userInfo.setUserName("John");
62         when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "testing")).thenReturn(userInfo);
63
64         OptimizationModels optimziationModels = new OptimizationModels();
65
66         doNothing().when(commonClassDao).delete(optimziationModels);
67
68         OptimizationDictionaryController.setCommonClassDao(commonClassDao);
69
70         controller = new OptimizationDictionaryController();
71
72         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
73
74         jsonString = "{\"optimizationModelsDictionaryData\": {\"modelName\": \"test\",\"inprocess\": false,\"model\":"
75                 + " {\"name\": \"testingdata\",\"subScopename\": \"\",\"path\": [],\"type\": \"dir\","
76                 + "\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", \"version\": \"\","
77                 + "\"createdBy\": \"someone\",\"modifiedBy\": \"someone\",\"content\": \"\"," + "\"recursive\": false},"
78                 + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
79                 + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
80                 + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
81                 + "\"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
82                 + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
83                 + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
84                 + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
85
86         br = new BufferedReader(new StringReader(jsonString));
87         // --- mock the getReader() call
88         when(request.getReader()).thenReturn(br);
89         new DictionaryUtils(commonClassDao);
90         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
91         mock(DictionaryUtils.class);
92         logger.info("setUp: exit");
93     }
94
95     @Test
96     public void testGetOptimizationModelsDictionaryEntityData() {
97         logger.info("testGetOptimizationModelsDictionaryEntityData: Entering");
98
99         MockHttpServletResponse response = new MockHttpServletResponse();
100         String modelJson = "{\"optimizationModelsDictionaryData\":[\"modelName\"]}";
101
102         BufferedReader br = new BufferedReader(new StringReader(modelJson));
103         request = mock(HttpServletRequest.class);
104
105         try {
106             // mock the getReader() call
107             when(request.getReader()).thenReturn(br);
108             controller.getOptimizationModelsDictionaryEntityData(response);
109             logger.info("response.getContentAsString(): " + response.getContentAsString());
110             assertTrue(response.getContentAsString() != null
111                     && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
112
113         } catch (Exception e) {
114             fail("Exception: " + e);
115         }
116
117         logger.info("testGetOptimizationModelsDictionaryEntityData: exit");
118     }
119
120     @Test
121     public void testSaveOptimizationModelsDictionary() {
122         logger.info("testSaveOptimizationModelsDictionary: Entering");
123
124         MockHttpServletResponse response = new MockHttpServletResponse();
125         request = mock(HttpServletRequest.class);
126
127         try {
128             // mock the getReader() call
129             when(request.getReader()).thenReturn(br);
130             controller.saveOptimizationModelsDictionary(request, response);
131             logger.info("response.getContentAsString(): " + response.getContentAsString());
132             assertTrue(response.getContentAsString() != null
133                     && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
134
135         } catch (Exception e) {
136             fail("Exception: " + e);
137         }
138
139         logger.info("testSaveOptimizationModelsDictionary: exit");
140     }
141
142     @Test
143     public void testRemoveOptimizationModelsDictionary() {
144         logger.info("testRemoveOptimizationModelsDictionary: Entering");
145
146         MockHttpServletResponse response = new MockHttpServletResponse();
147         request = mock(HttpServletRequest.class);
148
149         try {
150             // mock the getReader() call
151             jsonString =
152                     "{\"data\": {\"modelName\": \"test\",\"inprocess\": false,\"model\": {\"name\": \"testingdata\","
153                             + "\"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,"
154                             + "\"date\": \"2017-04-12T21:26:57.000Z\",\"version\": \"\",\"createdBy\": \"someone\","
155                             + "\"modifiedBy\": \"someone\",\"content\": \"\",\"recursive\": false},"
156                             + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
157                             + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
158                             + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
159                             + "\"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
160                             + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
161                             + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
162                             + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
163
164             BufferedReader br = new BufferedReader(new StringReader(jsonString));
165             when(request.getReader()).thenReturn(br);
166             controller.removeOptimizationModelsDictionary(request, response);
167             logger.info("response.getContentAsString(): " + response.getContentAsString());
168             assertTrue(response.getContentAsString() != null
169                     && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
170
171         } catch (Exception e) {
172             fail("Exception: " + e);
173         }
174         logger.info("testRemoveOptimizationModelsDictionary: exit");
175     }
176 }