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