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