aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/UpdateOthersPAPSTest.java
blob: f9ed0ac704d2d4eba9e82891d533e5d75faee5b9 (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
109
110
111
112
113
114
115
116
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PAP-REST
 * ================================================================================
 * 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.pap.test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.pap.xacml.rest.UpdateOthersPAPS;
import org.onap.policy.pap.xacml.rest.adapters.UpdateObjectData;
import org.onap.policy.pap.xacml.rest.components.Policy;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.PolicyDBDaoEntity;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.mock.web.MockHttpServletResponse;

@RunWith(PowerMockRunner.class)
public class UpdateOthersPAPSTest {

	private static Logger logger = FlexLogger.getLogger(UpdateOthersPAPSTest.class);
	private static CommonClassDao commonClassDao;
	private HttpServletRequest request;
	private MockHttpServletResponse response;
	
	@Before
	public void setUp() throws Exception {
		logger.info("setUp: Entering");
        commonClassDao = mock(CommonClassDao.class);
        
        request = mock(HttpServletRequest.class);       
		response =  new MockHttpServletResponse();
		List<Object> data = new ArrayList<>();
		PolicyDBDaoEntity entity = new PolicyDBDaoEntity();
		entity.setPolicyDBDaoUrl("http://localhost:8070/pap");
		entity.setUsername("test");
		entity.setPassword("test");
		
		PolicyDBDaoEntity entity1 = new PolicyDBDaoEntity();
		entity1.setPolicyDBDaoUrl("http://localhost:8071/pap");
		entity1.setUsername("test");
		entity1.setPassword("test");
		
		data.add(entity);
		data.add(entity1);
		System.setProperty("xacml.rest.pap.url","http://localhost:8070/pap");
		when(commonClassDao.getData(PolicyDBDaoEntity.class)).thenReturn(data);
	}
	
	@Test
	public void testNotifyOthersPAPsToUpdateConfigurations(){
		UpdateOthersPAPS updateOtherPaps = new UpdateOthersPAPS();
		UpdateOthersPAPS.setCommonClassDao(commonClassDao);
		when(request.getParameter("action")).thenReturn("rename");
		when(request.getParameter("newPolicyName")).thenReturn("com.Config_newTest.1.json");
		when(request.getParameter("oldPolicyName")).thenReturn("com.Config_Test.1.json");
		updateOtherPaps.notifyOthersPAPsToUpdateConfigurations(request, response);
		try {
			JSONObject responseString = new JSONObject(response.getContentAsString());
			assertTrue(responseString.get("data").toString().contains("http://localhost:8071/pap")); 
		} catch (Exception e) {
			fail();
		}
	}
	
	@PrepareForTest({Policy.class})
	@Test
	public void testUpdateConfiguration() throws Exception{
		UpdateOthersPAPS updateOtherPaps = new UpdateOthersPAPS();
		UpdateObjectData data = new UpdateObjectData();
		PowerMockito.mockStatic(Policy.class);
		data.setNewPolicyName("com.Config_newTest.1.json");
		data.setOldPolicyName("com.Config_Test.1.json");
		data.setAction("rename");
		when(Policy.getConfigHome()).thenReturn("test");
		when(Policy.getActionHome()).thenReturn("test");
		File mockedFile = Mockito.mock(File.class);
		Mockito.when(mockedFile.exists()).thenReturn(true);
		PowerMockito.whenNew(File.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(mockedFile);
		updateOtherPaps.updateConfiguration(data, response);
		assertTrue(response.getStatus() == 200);
	}
}