2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
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;
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.io.UnsupportedEncodingException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import javax.servlet.http.HttpServletRequest;
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.DCAEuuid;
44 import org.onap.policy.rest.jpa.MicroServiceLocation;
45 import org.onap.policy.rest.jpa.MicroServiceModels;
46 import org.onap.policy.rest.jpa.OptimizationModels;
47 import org.onap.policy.rest.jpa.UserInfo;
48 import org.springframework.mock.web.MockHttpServletResponse;
51 public class OptimizationDictionaryControllerTest {
53 private static Logger logger = FlexLogger.getLogger(OptimizationDictionaryControllerTest.class);
54 private static CommonClassDao commonClassDao;
55 private String jsonString = null;
56 private HttpServletRequest request = null;
57 private OptimizationDictionaryController controller = null;
58 BufferedReader br = null;
61 public void setUp() throws Exception {
62 logger.info("setUp: Entering");
63 commonClassDao = Mockito.mock(CommonClassDao.class);
64 UserInfo userInfo = new UserInfo();
65 userInfo.setUserLoginId("testUserId");
66 userInfo.setUserName("John");
67 when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "testing")).thenReturn(userInfo);
69 OptimizationModels optimziationModels = new OptimizationModels();
71 doNothing().when(commonClassDao).delete(optimziationModels);
73 OptimizationDictionaryController.setCommonClassDao(commonClassDao);
75 controller = new OptimizationDictionaryController();
77 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
79 jsonString = "{\"optimizationModelsDictionaryData\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
80 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
81 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
82 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
83 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
84 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
85 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
86 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
88 br = new BufferedReader(new StringReader(jsonString));
89 //--- mock the getReader() call
90 when(request.getReader()).thenReturn(br);
91 new DictionaryUtils(commonClassDao);
92 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
93 mock(DictionaryUtils.class);
94 logger.info("setUp: exit");
98 public void testGetOptimizationModelsDictionaryEntityData() {
99 logger.info("testGetOptimizationModelsDictionaryEntityData: Entering");
101 MockHttpServletResponse response = new MockHttpServletResponse();
102 String modelJson = "{\"optimizationModelsDictionaryData\":[\"modelName\"]}";
104 BufferedReader br = new BufferedReader(new StringReader(modelJson));
105 request = mock(HttpServletRequest.class);
108 // mock the getReader() call
109 when(request.getReader()).thenReturn(br);
110 controller.getOptimizationModelsDictionaryEntityData(response);
111 logger.info("response.getContentAsString(): " + response.getContentAsString());
112 assertTrue( response.getContentAsString() != null && 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 && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
135 } catch (Exception e) {
136 fail("Exception: " + e);
139 logger.info("testSaveOptimizationModelsDictionary: exit");
143 public void testRemoveOptimizationModelsDictionary() {
144 logger.info("testRemoveOptimizationModelsDictionary: Entering");
146 MockHttpServletResponse response = new MockHttpServletResponse();
147 request = mock(HttpServletRequest.class);
150 // mock the getReader() call
151 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
152 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
153 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
154 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
155 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
156 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
157 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
158 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
160 BufferedReader br = new BufferedReader(new StringReader(jsonString));
161 when(request.getReader()).thenReturn(br);
162 controller.removeOptimizationModelsDictionary(request, response);
163 logger.info("response.getContentAsString(): " + response.getContentAsString());
164 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
166 } catch (Exception e) {
167 fail("Exception: " + e);
170 logger.info("testRemoveOptimizationModelsDictionary: exit");