2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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=========================================================
21 package org.onap.policy.controller;
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.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.when;
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
34 import java.io.IOException;
35 import java.util.Collections;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
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.PowerMockIgnore;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
55 @RunWith(PowerMockRunner.class)
56 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
57 public class PolicyExportAndImportControllerTest {
59 public void testSetAndGet() {
60 PolicyExportAndImportController controller = new PolicyExportAndImportController();
61 PolicyController policyController = new PolicyController();
62 controller.setPolicyController(policyController);
63 assertEquals(controller.getPolicyController(), policyController);
64 CommonClassDao commonClassDao = new CommonClassDaoImpl();
65 PolicyExportAndImportController.setCommonClassDao(commonClassDao);
66 assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao);
70 public void testExport() throws IOException {
71 PolicyExportAndImportController controller = new PolicyExportAndImportController();
72 MockHttpServletRequest request = new MockHttpServletRequest();
73 request.setBodyContent("{\n\"exportData\": {}\n}\n");
74 MockHttpServletResponse response = new MockHttpServletResponse();
77 controller.exportPolicy(request, response);
78 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
81 @PrepareForTest({UserUtils.class})
83 public void testImport() throws IOException {
84 // Mock user utilities
85 PowerMockito.mockStatic(UserUtils.class);
86 User user = new User();
87 when(UserUtils.getUserSession(any())).thenReturn(user);
90 UserInfo info = new UserInfo();
91 ConfigurationDataEntity configEntity = new ConfigurationDataEntity();
92 CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class);
93 when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info);
94 when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity);
95 when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList());
98 PolicyController policyController = new PolicyController();
99 PolicyController.setCommonClassDao(commonClassDao);
100 PolicyExportAndImportController controller = new PolicyExportAndImportController();
101 PolicyExportAndImportController.setCommonClassDao(commonClassDao);
102 controller.setPolicyController(policyController);
103 HttpServletRequest request = new MockHttpServletRequest();
104 ClassLoader classLoader = getClass().getClassLoader();
106 // Test negative case
107 String file = new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile())
109 JSONObject json = controller.importRepositoryFile(file, request);
112 // Another negative case
113 file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath();
114 json = controller.importRepositoryFile(file, request);
118 String jsonString = "{ configName:\"abc\", uuid:\"someone\", location:\"somewhere\", policyScope:\"test\", "
119 + "service:\"sdnc\", version:\"1810\"}";
120 String errorMsg = controller.validatMatchRequiredFields("TestPolicy", jsonString);
121 assertTrue(errorMsg != null);