edc08f31b36a2a484d174462edb50b8c6b98daa3
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pap.xacml.rest.controller;
21
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;
27
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;
33
34 import javax.servlet.http.HttpServletRequest;
35
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;
49
50
51 public class OptimizationDictionaryControllerTest {
52         
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;
59
60         @Before
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);
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 = "{\"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\"} }";
87     
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");
95         }
96
97         @Test
98         public void testGetOptimizationModelsDictionaryEntityData() {
99                 logger.info("testGetOptimizationModelsDictionaryEntityData: Entering");
100
101                 MockHttpServletResponse response =  new MockHttpServletResponse();
102                 String modelJson = "{\"optimizationModelsDictionaryData\":[\"modelName\"]}";
103                 
104             BufferedReader br = new BufferedReader(new StringReader(modelJson));
105             request = mock(HttpServletRequest.class);   
106         
107                 try {
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"));
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 && 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 = "{\"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\"} }";
159                         
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"));
165
166                 } catch (Exception e) {
167                         fail("Exception: " + e);
168                 }
169                 
170                 logger.info("testRemoveOptimizationModelsDictionary: exit");
171         }
172
173 }