Format java POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / PolicyExportAndImportControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018-2019 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
21 package org.onap.policy.controller;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.when;
29
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.Collections;
36
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.json.JSONObject;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.policy.daoImp.CommonClassDaoImpl;
45 import org.onap.policy.rest.dao.CommonClassDao;
46 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
47 import org.onap.policy.rest.jpa.UserInfo;
48 import org.onap.portalsdk.core.domain.User;
49 import org.onap.portalsdk.core.web.support.UserUtils;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
53
54 @RunWith(PowerMockRunner.class)
55 public class PolicyExportAndImportControllerTest {
56     @Test
57     public void testSetAndGet() {
58         PolicyExportAndImportController controller = new PolicyExportAndImportController();
59         PolicyController policyController = new PolicyController();
60         controller.setPolicyController(policyController);
61         assertEquals(controller.getPolicyController(), policyController);
62         CommonClassDao commonClassDao = new CommonClassDaoImpl();
63         PolicyExportAndImportController.setCommonClassDao(commonClassDao);
64         assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao);
65     }
66
67     @Test
68     public void testExport() throws IOException {
69         PolicyExportAndImportController controller = new PolicyExportAndImportController();
70         MockHttpServletRequest request = new MockHttpServletRequest();
71         request.setBodyContent("{\n\"exportData\": {}\n}\n");
72         MockHttpServletResponse response = new MockHttpServletResponse();
73
74         // Test negative case
75         controller.exportPolicy(request, response);
76         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
77     }
78
79     @PrepareForTest({UserUtils.class})
80     @Test
81     public void testImport() throws IOException {
82         // Mock user utilities
83         PowerMockito.mockStatic(UserUtils.class);
84         User user = new User();
85         when(UserUtils.getUserSession(any())).thenReturn(user);
86
87         // Mock dao
88         UserInfo info = new UserInfo();
89         ConfigurationDataEntity configEntity = new ConfigurationDataEntity();
90         CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class);
91         when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info);
92         when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity);
93         when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList());
94
95         // Test import
96         PolicyController policyController = new PolicyController();
97         PolicyController.setCommonClassDao(commonClassDao);
98         PolicyExportAndImportController controller = new PolicyExportAndImportController();
99         PolicyExportAndImportController.setCommonClassDao(commonClassDao);
100         controller.setPolicyController(policyController);
101         HttpServletRequest request = new MockHttpServletRequest();
102         ClassLoader classLoader = getClass().getClassLoader();
103
104         // Test negative case
105         String file = new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile())
106                 .getAbsolutePath();
107         JSONObject json = controller.importRepositoryFile(file, request);
108         assertNull(json);
109
110         // Another negative case
111         file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath();
112         json = controller.importRepositoryFile(file, request);
113         assertNull(json);
114
115         // test validation
116         String jsonString = "{ configName:\"abc\", uuid:\"someone\", location:\"somewhere\", policyScope:\"test\", "
117                 + "service:\"sdnc\", version:\"1810\"}";
118         String errorMsg = controller.validatMatchRequiredFields("TestPolicy", jsonString);
119         assertTrue(errorMsg != null);
120     }
121 }