99902012b16468ea3c1a5ea51aaab2c4393141e1
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / test / UpdateOthersPAPSTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 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.pap.test;
22
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
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.servlet.http.HttpServletRequest;
33
34 import org.json.JSONObject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Matchers;
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.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50 import org.springframework.mock.web.MockHttpServletResponse;
51
52 @RunWith(PowerMockRunner.class)
53 public class UpdateOthersPAPSTest {
54
55     private static Logger logger = FlexLogger.getLogger(UpdateOthersPAPSTest.class);
56     private static CommonClassDao commonClassDao;
57     private HttpServletRequest request;
58     private MockHttpServletResponse response;
59
60     @Before
61     public void setUp() throws Exception {
62         logger.info("setUp: Entering");
63         commonClassDao = mock(CommonClassDao.class);
64
65         request = mock(HttpServletRequest.class);
66         response = new MockHttpServletResponse();
67         List<Object> data = new ArrayList<>();
68         PolicyDBDaoEntity entity = new PolicyDBDaoEntity();
69         entity.setPolicyDBDaoUrl("http://localhost:8070/pap");
70         entity.setUsername("test");
71         entity.setPassword("test");
72
73         PolicyDBDaoEntity entity1 = new PolicyDBDaoEntity();
74         entity1.setPolicyDBDaoUrl("http://localhost:8071/pap");
75         entity1.setUsername("test");
76         entity1.setPassword("test");
77
78         data.add(entity);
79         data.add(entity1);
80         System.setProperty("xacml.rest.pap.url", "http://localhost:8070/pap");
81         when(commonClassDao.getData(PolicyDBDaoEntity.class)).thenReturn(data);
82     }
83
84     @Test
85     public void testNotifyOthersPAPsToUpdateConfigurations() {
86         UpdateOthersPAPS updateOtherPaps = new UpdateOthersPAPS();
87         UpdateOthersPAPS.setCommonClassDao(commonClassDao);
88         when(request.getParameter("action")).thenReturn("rename");
89         when(request.getParameter("newPolicyName")).thenReturn("com.Config_newTest.1.json");
90         when(request.getParameter("oldPolicyName")).thenReturn("com.Config_Test.1.json");
91         updateOtherPaps.notifyOthersPAPsToUpdateConfigurations(request, response);
92         try {
93             JSONObject responseString = new JSONObject(response.getContentAsString());
94             assertTrue(responseString.get("data").toString().contains("http://localhost:8071/pap"));
95         } catch (Exception e) {
96             fail();
97         }
98     }
99
100     @PrepareForTest({Policy.class})
101     @Test
102     public void testUpdateConfiguration() throws Exception {
103         UpdateOthersPAPS updateOtherPaps = new UpdateOthersPAPS();
104         UpdateObjectData data = new UpdateObjectData();
105         PowerMockito.mockStatic(Policy.class);
106         data.setNewPolicyName("com.Config_newTest.1.json");
107         data.setOldPolicyName("com.Config_Test.1.json");
108         data.setAction("rename");
109         when(Policy.getConfigHome()).thenReturn("test");
110         when(Policy.getActionHome()).thenReturn("test");
111         File mockedFile = Mockito.mock(File.class);
112         Mockito.when(mockedFile.exists()).thenReturn(true);
113         PowerMockito.whenNew(File.class).withParameterTypes(String.class).withArguments(Matchers.anyString())
114                 .thenReturn(mockedFile);
115         updateOtherPaps.updateConfiguration(data, response);
116         assertTrue(response.getStatus() == 200);
117     }
118 }