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.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.eq;
29 import java.io.IOException;
30 import java.util.Collections;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.json.JSONObject;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mockito;
37 import org.onap.policy.daoImp.CommonClassDaoImpl;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
40 import org.onap.policy.rest.jpa.UserInfo;
41 import org.onap.portalsdk.core.domain.User;
42 import org.onap.portalsdk.core.web.support.UserUtils;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import com.mockrunner.mock.web.MockHttpServletRequest;
47 import com.mockrunner.mock.web.MockHttpServletResponse;
49 @RunWith(PowerMockRunner.class)
50 public class PolicyExportAndImportControllerTest {
52 public void testSetAndGet() {
53 PolicyExportAndImportController controller = new PolicyExportAndImportController();
54 PolicyController policyController = new PolicyController();
55 controller.setPolicyController(policyController);
56 assertEquals(controller.getPolicyController(), policyController);
57 CommonClassDao commonClassDao = new CommonClassDaoImpl();
58 PolicyExportAndImportController.setCommonClassDao(commonClassDao);
59 assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao);
63 public void testExport() throws IOException {
64 PolicyExportAndImportController controller = new PolicyExportAndImportController();
65 MockHttpServletRequest request = new MockHttpServletRequest();
66 request.setBodyContent("{\n\"exportData\": {}\n}\n");
67 MockHttpServletResponse response = new MockHttpServletResponse();
70 controller.exportPolicy(request, response);
71 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
74 @PrepareForTest({UserUtils.class})
76 public void testImport() throws IOException {
77 // Mock user utilities
78 PowerMockito.mockStatic(UserUtils.class);
79 User user = new User();
80 when(UserUtils.getUserSession(any())).thenReturn(user);
83 UserInfo info = new UserInfo();
84 ConfigurationDataEntity configEntity = new ConfigurationDataEntity();
85 CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class);
86 when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info);
87 when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity);
88 when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList());
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();
97 ClassLoader classLoader = getClass().getClassLoader();
101 new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile())
103 JSONObject json = controller.importRepositoryFile(file, request);
106 // Another negative case
107 file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath();
108 json = controller.importRepositoryFile(file, request);
112 String jsonString = "{ configName:\"abc\", uuid:\"someone\", location:\"somewhere\", policyScope:\"test\", "
113 + "service:\"sdnc\", version:\"1810\"}";
114 String errorMsg = controller.validatMatchRequiredFields("TestPolicy", jsonString);
115 assertTrue(errorMsg != null);