aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java
blob: 358108fa1b6e118336a21d44cf109baf31bad566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Engine
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.policy.controller;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.onap.policy.daoImp.CommonClassDaoImpl;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.ConfigurationDataEntity;
import org.onap.policy.rest.jpa.UserInfo;
import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.web.support.UserUtils;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.mockrunner.mock.web.MockHttpServletRequest;
import com.mockrunner.mock.web.MockHttpServletResponse;

@RunWith(PowerMockRunner.class)
public class PolicyExportAndImportControllerTest {
	@Test
	public void testSetAndGet(){
		PolicyExportAndImportController controller = new PolicyExportAndImportController();
		PolicyController policyController = new PolicyController();
		controller.setPolicyController(policyController);
		assertEquals(controller.getPolicyController(), policyController);
		CommonClassDao commonClassDao = new CommonClassDaoImpl();
		PolicyExportAndImportController.setCommonClassDao(commonClassDao);
		assertEquals(PolicyExportAndImportController.getCommonClassDao(), commonClassDao);
	}
	
	@Test
	public void testExport() throws IOException {
		PolicyExportAndImportController controller = new PolicyExportAndImportController();
		MockHttpServletRequest request = new MockHttpServletRequest();
		request.setBodyContent("{\n\"exportData\": {}\n}\n");
		MockHttpServletResponse response = new MockHttpServletResponse();
		
		// Test negative case
		controller.exportPolicy(request, response);
		assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
	}
	
	@PrepareForTest({UserUtils.class})
	@Test
	public void testImport() throws IOException {
		// Mock user utilities
		PowerMockito.mockStatic(UserUtils.class);
		User user = new User();
		when(UserUtils.getUserSession(any())).thenReturn(user);

		// Mock dao
		UserInfo info = new UserInfo();
		ConfigurationDataEntity configEntity = new ConfigurationDataEntity();
		CommonClassDao commonClassDao = Mockito.mock(CommonClassDaoImpl.class);
		when(commonClassDao.getEntityItem(eq(UserInfo.class), any(), any())).thenReturn(info);
		when(commonClassDao.getEntityItem(eq(ConfigurationDataEntity.class), any(), any())).thenReturn(configEntity);
		when(commonClassDao.getDataById(any(), any(), any())).thenReturn(Collections.emptyList());

		// Test import
		ClassLoader classLoader = getClass().getClassLoader();
		PolicyController policyController = new PolicyController();
		PolicyController.setCommonClassDao(commonClassDao);
		PolicyExportAndImportController controller = new PolicyExportAndImportController();
		PolicyExportAndImportController.setCommonClassDao(commonClassDao);
		controller.setPolicyController(policyController);
		HttpServletRequest request = new MockHttpServletRequest();
		
		// Test negative case
		String file = new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile()).getAbsolutePath();
		JSONObject json = controller.importRepositoryFile(file, request);
		assertNull(json);
		
		// Another negative case
		file = new File(classLoader.getResource("PolicyExport.xls").getFile()).getAbsolutePath();
		json = controller.importRepositoryFile(file, request);
		assertNull(json);
	}
}