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