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.pap.test;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.ArgumentMatchers.anyString;
30 import java.util.ArrayList;
31 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
35 import org.json.JSONObject;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.pap.xacml.rest.UpdateOthersPAPS;
43 import org.onap.policy.pap.xacml.rest.adapters.UpdateObjectData;
44 import org.onap.policy.pap.xacml.rest.components.Policy;
45 import org.onap.policy.rest.dao.CommonClassDao;
46 import org.onap.policy.rest.jpa.PolicyDbDaoEntity;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PowerMockIgnore;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import org.springframework.mock.web.MockHttpServletResponse;
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore({"com.sun.org.apache.xalan.*", "com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
55 @PrepareForTest(UpdateOthersPAPS.class)
56 public class UpdateOthersPAPSTest {
58 private static Logger logger = FlexLogger.getLogger(UpdateOthersPAPSTest.class);
59 private static CommonClassDao commonClassDao;
60 private HttpServletRequest request;
61 private MockHttpServletResponse response;
64 public void setUp() throws Exception {
65 logger.info("setUp: Entering");
66 commonClassDao = mock(CommonClassDao.class);
68 request = mock(HttpServletRequest.class);
69 response = new MockHttpServletResponse();
70 List<Object> data = new ArrayList<>();
71 PolicyDbDaoEntity entity = new PolicyDbDaoEntity();
72 entity.setPolicyDbDaoUrl("http://localhost:8070/pap");
73 entity.setUsername("test");
74 entity.setPassword("test");
76 PolicyDbDaoEntity entity1 = new PolicyDbDaoEntity();
77 entity1.setPolicyDbDaoUrl("http://localhost:8071/pap");
78 entity1.setUsername("test");
79 entity1.setPassword("test");
83 System.setProperty("xacml.rest.pap.url", "http://localhost:8070/pap");
84 when(commonClassDao.getData(PolicyDbDaoEntity.class)).thenReturn(data);
88 public void testNotifyOthersPAPsToUpdateConfigurations() {
89 UpdateOthersPAPS updateOtherPaps = new UpdateOthersPAPS();
90 UpdateOthersPAPS.setCommonClassDao(commonClassDao);
91 when(request.getParameter("action")).thenReturn("rename");
92 when(request.getParameter("newPolicyName")).thenReturn("com.Config_newTest.1.json");
93 when(request.getParameter("oldPolicyName")).thenReturn("com.Config_Test.1.json");
94 updateOtherPaps.notifyOthersPAPsToUpdateConfigurations(request, response);
96 JSONObject responseString = new JSONObject(response.getContentAsString());
97 assertTrue(responseString.get("data").toString().contains("http://localhost:8071/pap"));
98 } catch (Exception e) {
103 @PrepareForTest({Policy.class})
105 public void testUpdateConfiguration() throws Exception {
106 UpdateOthersPAPS updateOtherPaps = new UpdateOthersPAPS();
107 UpdateObjectData data = new UpdateObjectData();
108 PowerMockito.mockStatic(Policy.class);
109 data.setNewPolicyName("com.Config_newTest.1.json");
110 data.setOldPolicyName("com.Config_Test.1.json");
111 data.setAction("rename");
112 when(Policy.getConfigHome()).thenReturn("test");
113 when(Policy.getActionHome()).thenReturn("test");
114 File mockedFile = Mockito.mock(File.class);
115 Mockito.when(mockedFile.exists()).thenReturn(true);
116 PowerMockito.whenNew(File.class).withParameterTypes(String.class).withArguments(anyString())
117 .thenReturn(mockedFile);
118 updateOtherPaps.updateConfiguration(data, response);
119 assertTrue(response.getStatus() == 200);