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.controller;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.eq;
28 import java.io.IOException;
29 import java.util.Collections;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.json.JSONObject;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mockito;
36 import org.onap.policy.daoImp.CommonClassDaoImpl;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
39 import org.onap.policy.rest.jpa.UserInfo;
40 import org.onap.portalsdk.core.domain.User;
41 import org.onap.portalsdk.core.web.support.UserUtils;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
45 import com.mockrunner.mock.web.MockHttpServletRequest;
46 import com.mockrunner.mock.web.MockHttpServletResponse;
48 @RunWith(PowerMockRunner.class)
49 public class PolicyExportAndImportControllerTest {
51 public void testSetAndGet(){
52 PolicyExportAndImportController controller = new PolicyExportAndImportController();
53 PolicyController policyController = new PolicyController();
54 controller.setPolicyController(policyController);
55 assertEquals(controller.getPolicyController(), policyController);
56 CommonClassDao commonClassDao = new CommonClassDaoImpl();
57 PolicyExportAndImportController.setCommonClassDao(commonClassDao);
58 assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao);
62 public void testExport() throws IOException {
63 PolicyExportAndImportController controller = new PolicyExportAndImportController();
64 MockHttpServletRequest request = new MockHttpServletRequest();
65 request.setBodyContent("{\n\"exportData\": {}\n}\n");
66 MockHttpServletResponse response = new MockHttpServletResponse();
69 controller.exportPolicy(request, response);
70 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
73 @PrepareForTest({UserUtils.class})
75 public void testImport() throws IOException {
76 // Mock user utilities
77 PowerMockito.mockStatic(UserUtils.class);
78 User user = new User();
79 when(UserUtils.getUserSession(any())).thenReturn(user);
82 UserInfo info = new UserInfo();
83 ConfigurationDataEntity configEntity = new ConfigurationDataEntity();
84 CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class);
85 when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info);
86 when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity);
87 when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList());
90 ClassLoader classLoader = getClass().getClassLoader();
91 PolicyController policyController = new PolicyController();
92 PolicyController.setCommonClassDao(commonClassDao);
93 PolicyExportAndImportController controller = new PolicyExportAndImportController();
94 PolicyExportAndImportController.setCommonClassDao(commonClassDao);
95 controller.setPolicyController(policyController);
96 HttpServletRequest request = new MockHttpServletRequest();
99 String file = new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile()).getAbsolutePath();
100 JSONObject json = controller.importRepositoryFile(file, request);
103 // Another negative case
104 file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath();
105 json = controller.importRepositoryFile(file, request);