2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.xacml.rest.controller;
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;
44 public class OptimizationDictionaryControllerTest {
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;
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);
63 OptimizationModels optimziationModels = new OptimizationModels();
65 doNothing().when(commonClassDao).delete(optimziationModels);
67 OptimizationDictionaryController.setCommonClassDao(commonClassDao);
69 controller = new OptimizationDictionaryController();
71 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
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\"}}";
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");
97 public void testGetOptimizationModelsDictionaryEntityData() {
98 logger.info("testGetOptimizationModelsDictionaryEntityData: Entering");
100 MockHttpServletResponse response = new MockHttpServletResponse();
101 String modelJson = "{\"optimizationModelsDictionaryData\":[\"modelName\"]}";
103 BufferedReader br = new BufferedReader(new StringReader(modelJson));
104 request = mock(HttpServletRequest.class);
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"));
114 } catch (Exception e) {
115 fail("Exception: " + e);
118 logger.info("testGetOptimizationModelsDictionaryEntityData: exit");
122 public void testSaveOptimizationModelsDictionary() {
123 logger.info("testSaveOptimizationModelsDictionary: Entering");
125 MockHttpServletResponse response = new MockHttpServletResponse();
126 request = mock(HttpServletRequest.class);
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"));
136 } catch (Exception e) {
137 fail("Exception: " + e);
140 logger.info("testSaveOptimizationModelsDictionary: exit");
144 public void testRemoveOptimizationModelsDictionary() {
145 logger.info("testRemoveOptimizationModelsDictionary: Entering");
147 MockHttpServletResponse response = new MockHttpServletResponse();
148 request = mock(HttpServletRequest.class);
151 // mock the getReader() call
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\"}}";
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"));
172 } catch (Exception e) {
173 fail("Exception: " + e);
175 logger.info("testRemoveOptimizationModelsDictionary: exit");