5e0b7ce28219b37070e62e806d78cb2e1d11702d
[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 java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import javax.servlet.http.HttpServletRequest;
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.DCAEuuid;
43 import org.onap.policy.rest.jpa.MicroServiceLocation;
44 import org.onap.policy.rest.jpa.MicroServiceModels;
45 import org.onap.policy.rest.jpa.OptimizationModels;
46 import org.onap.policy.rest.jpa.UserInfo;
47 import org.springframework.mock.web.MockHttpServletResponse;
48
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"))
67                 .thenReturn(userInfo);
68
69         OptimizationModels optimziationModels = new OptimizationModels();
70
71         doNothing().when(commonClassDao).delete(optimziationModels);
72
73         OptimizationDictionaryController.setCommonClassDao(commonClassDao);
74
75         controller = new OptimizationDictionaryController();
76
77         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
78
79         jsonString =
80                 "{\"optimizationModelsDictionaryData\": {\"modelName\": \"test\",\"inprocess\": false,\"model\":"
81                         + " {\"name\": \"testingdata\",\"subScopename\": \"\",\"path\": [],\"type\": \"dir\","
82                         + "\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", \"version\": \"\","
83                         + "\"createdBy\": \"someone\",\"modifiedBy\": \"someone\",\"content\": \"\","
84                         + "\"recursive\": false},"
85                         + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
86                         + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
87                         + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
88                         + "\"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
89                         + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
90                         + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
91                         + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
92
93         br = new BufferedReader(new StringReader(jsonString));
94         // --- mock the getReader() call
95         when(request.getReader()).thenReturn(br);
96         new DictionaryUtils(commonClassDao);
97         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
98         mock(DictionaryUtils.class);
99         logger.info("setUp: exit");
100     }
101
102     @Test
103     public void testGetOptimizationModelsDictionaryEntityData() {
104         logger.info("testGetOptimizationModelsDictionaryEntityData: Entering");
105
106         MockHttpServletResponse response = new MockHttpServletResponse();
107         String modelJson = "{\"optimizationModelsDictionaryData\":[\"modelName\"]}";
108
109         BufferedReader br = new BufferedReader(new StringReader(modelJson));
110         request = mock(HttpServletRequest.class);
111
112         try {
113             // mock the getReader() call
114             when(request.getReader()).thenReturn(br);
115             controller.getOptimizationModelsDictionaryEntityData(response);
116             logger.info("response.getContentAsString(): " + response.getContentAsString());
117             assertTrue(response.getContentAsString() != null
118                     && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
119
120         } catch (Exception e) {
121             fail("Exception: " + e);
122         }
123
124         logger.info("testGetOptimizationModelsDictionaryEntityData: exit");
125     }
126
127     @Test
128     public void testSaveOptimizationModelsDictionary() {
129         logger.info("testSaveOptimizationModelsDictionary: Entering");
130
131         MockHttpServletResponse response = new MockHttpServletResponse();
132         request = mock(HttpServletRequest.class);
133
134         try {
135             // mock the getReader() call
136             when(request.getReader()).thenReturn(br);
137             controller.saveOptimizationModelsDictionary(request, response);
138             logger.info("response.getContentAsString(): " + response.getContentAsString());
139             assertTrue(response.getContentAsString() != null
140                     && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
141
142         } catch (Exception e) {
143             fail("Exception: " + e);
144         }
145
146         logger.info("testSaveOptimizationModelsDictionary: exit");
147     }
148
149     @Test
150     public void testRemoveOptimizationModelsDictionary() {
151         logger.info("testRemoveOptimizationModelsDictionary: Entering");
152
153         MockHttpServletResponse response = new MockHttpServletResponse();
154         request = mock(HttpServletRequest.class);
155
156         try {
157             // mock the getReader() call
158             jsonString =
159                     "{\"data\": {\"modelName\": \"test\",\"inprocess\": false,\"model\": {\"name\": \"testingdata\","
160                             + "\"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,"
161                             + "\"date\": \"2017-04-12T21:26:57.000Z\",\"version\": \"\",\"createdBy\": \"someone\","
162                             + "\"modifiedBy\": \"someone\",\"content\": \"\",\"recursive\": false},"
163                             + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
164                             + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
165                             + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
166                             + "\"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
167                             + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
168                             + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
169                             + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
170
171             BufferedReader br = new BufferedReader(new StringReader(jsonString));
172             when(request.getReader()).thenReturn(br);
173             controller.removeOptimizationModelsDictionary(request, response);
174             logger.info("response.getContentAsString(): " + response.getContentAsString());
175             assertTrue(response.getContentAsString() != null
176                     && response.getContentAsString().contains("optimizationModelsDictionaryDatas"));
177
178         } catch (Exception e) {
179             fail("Exception: " + e);
180         }
181         logger.info("testRemoveOptimizationModelsDictionary: exit");
182     }
183 }