diff options
Diffstat (limited to 'PyPDPServer/src/test/java/testpypdp')
4 files changed, 1562 insertions, 0 deletions
diff --git a/PyPDPServer/src/test/java/testpypdp/AuthorizationTest.java b/PyPDPServer/src/test/java/testpypdp/AuthorizationTest.java new file mode 100644 index 000000000..228e926a8 --- /dev/null +++ b/PyPDPServer/src/test/java/testpypdp/AuthorizationTest.java @@ -0,0 +1,382 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * Copyright (C) 2017 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 testpypdp; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.openecomp.policy.pypdp.authorization.AuthenticationFilter; + +/* + * Authentication Filter Testing + */ +public class AuthorizationTest { + private static final String MASTERCLIENT= "cHl0aG9uOnRlc3Q="; + /*private static final String CONFIGCLIENT= "Y29uZmlnOmNvbmZpZw=="; + private static final String ACTIONCLIENT= "YWN0aW9uOmFjdGlvbg=="; + private static final String DECIDECLIENT= "ZGVjaWRlOmRlY2lkZQ=="; + private static final String CREATECLIENT= "Y3JlYXRlOmNyZWF0ZQ=="; + private static final String DELETECLIENT= "ZGVsZXRlOmRlbGV0ZQ==";*/ + + private AuthenticationFilter authenticationFilter = new AuthenticationFilter(); + + @Before + public void setUp() throws Exception{ + authenticationFilter.init(null); + } + + @Test + public void testDoFilterError() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getRequestURI()).thenReturn("error"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + + @Test + public void testDoFilterNotification() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getRequestURI()).thenReturn("org.openecomp.policy.pypdp.notifications swagger api-docs configuration"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + /*@Test + public void testDoFilterWrongAuthenticaton() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("error"); + when(httpServletRequest.getRequestURI()).thenReturn("getConfig"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongClientAuthenticaton() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("getConfig"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn("Error"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + @Test + public void testDoFilterWrongClientAuthenticatonCount() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("count"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn("Error"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + /*@Test + public void testDoFilterWrongGetConfigAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("getConfig"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(ACTIONCLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongSendEventAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("sendEvent"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(CONFIGCLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongUpdatePolicyAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("updatePolicy"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(ACTIONCLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongCreatePolicyAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("createPolicy"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(ACTIONCLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongPushPolicyAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("pushPolicy"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(DELETECLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongDeletePolicyAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("deletePolicy"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(DECIDECLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + /*@Test + public void testDoFilterWrongDecidePolicyAuthorization() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("getDecision"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(CREATECLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + }*/ + + @Test + public void testDoFilterAuthorizedError() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("error"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(MASTERCLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + // verify if unauthorized + verify(httpServletResponse).setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + + @Test + public void testDoFilterAuthorizedPDPs() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("pdps paps"); + when(httpServletRequest.getHeader("ClientAuth")).thenReturn(MASTERCLIENT); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + @Test + public void testDoFilterDecideAuthorized() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("getDecision"); + when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + @Test + public void testDoFilterDeleteAuthorized() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("deletePolicy"); + when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + @Test + public void testDoFilterEventAuthorized() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("sendEvent"); + when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + @Test + public void testDoFilterCreateAuthorized() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("createPolicy pushPolicy updatePolicy"); + when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + @Test + public void testDoFilterConfigAuthorized() throws IOException, ServletException { + // create the objects to be mocked + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + // + when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl"); + when(httpServletRequest.getRequestURI()).thenReturn("getConfig"); + when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL"); + + authenticationFilter.doFilter(httpServletRequest, httpServletResponse, + filterChain); + + verify(filterChain).doFilter(httpServletRequest,httpServletResponse); + } + + @After + public void tearDown(){ + authenticationFilter.destroy(); + } +} diff --git a/PyPDPServer/src/test/java/testpypdp/ConfigRequestTest.java b/PyPDPServer/src/test/java/testpypdp/ConfigRequestTest.java new file mode 100644 index 000000000..30e94fed9 --- /dev/null +++ b/PyPDPServer/src/test/java/testpypdp/ConfigRequestTest.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * Copyright (C) 2017 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 testpypdp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; +import java.util.HashMap; +import java.util.Properties; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.openecomp.policy.pypdp.model_pojo.PyPolicyConfig; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.policy.api.PolicyConfigStatus; +import org.openecomp.policy.api.PolicyType; +import org.openecomp.policy.std.StdPolicyConfig; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +import org.openecomp.policy.pypdp.ConfigRequest; + +public class ConfigRequestTest { + + private StdPolicyConfig config; + private ConfigRequest request; + + @Before + public void setUp() { + request = new ConfigRequest(null); + config = new StdPolicyConfig(); + config.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_RETRIEVED); + } + + @Test + public void checkResponsePropertiesTest() { + config.setPolicyType(PolicyType.PROPERTIES); + Properties prop = new Properties(); + prop.put("Key", "value"); + config.setProperties(prop); + PyPolicyConfig pConfig = request.checkResponse(config); + HashMap<String, String> result = new HashMap<String, String>(); + result.put("Key", "value"); + assertEquals(pConfig.getProperty(), result); + } + + @Test + public void checkResponseDocumentTest() throws Exception { + config.setPolicyType(PolicyType.XML); + String xmlString = "<test></test>"; + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + builder = factory.newDocumentBuilder(); + Document document = builder.parse(new InputSource(new StringReader( + xmlString))); + config.setDocument(document); + PyPolicyConfig pConfig = request.checkResponse(config); + assertNotNull(pConfig.getConfig()); + } +} diff --git a/PyPDPServer/src/test/java/testpypdp/NotificationControllerTest.java b/PyPDPServer/src/test/java/testpypdp/NotificationControllerTest.java new file mode 100644 index 000000000..c2d9089e3 --- /dev/null +++ b/PyPDPServer/src/test/java/testpypdp/NotificationControllerTest.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * Copyright (C) 2017 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 testpypdp; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.openecomp.policy.pypdp.notifications.NotificationController; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.policy.api.LoadedPolicy; +import org.openecomp.policy.api.NotificationType; +import org.openecomp.policy.api.PDPNotification; +import org.openecomp.policy.api.RemovedPolicy; +import org.openecomp.policy.api.UpdateType; +import org.openecomp.policy.std.StdLoadedPolicy; +import org.openecomp.policy.std.StdPDPNotification; +import org.openecomp.policy.std.StdRemovedPolicy; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class NotificationControllerTest { + private NotificationController notificationController = new NotificationController(); + private StdPDPNotification notification; + + @Before + public void setUp() { + notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.BOTH); + Collection<StdRemovedPolicy> removedPolicies = new ArrayList<StdRemovedPolicy>(); + Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<StdLoadedPolicy>(); + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + StdLoadedPolicy updatedPolicy = new StdLoadedPolicy(); + removedPolicy.setPolicyName("Test"); + removedPolicy.setVersionNo("1"); + removedPolicies.add(removedPolicy); + updatedPolicy.setPolicyName("Testing"); + updatedPolicy.setVersionNo("1"); + updatedPolicy.setUpdateType(UpdateType.NEW); + Map<String, String> matches = new HashMap<String, String>(); + matches.put("key", "value"); + updatedPolicy.setMatches(matches); + loadedPolicies.add(updatedPolicy); + notification.setRemovedPolicies(removedPolicies); + notification.setLoadedPolicies(loadedPolicies); + NotificationController.record(notification); + } + + @Test + public void notificationReceivedUpdateTest() throws Exception{ + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.UPDATE); + Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<StdLoadedPolicy>(); + StdLoadedPolicy updatedPolicy = new StdLoadedPolicy(); + updatedPolicy.setPolicyName("Test"); + updatedPolicy.setVersionNo("1"); + updatedPolicy.setUpdateType(UpdateType.NEW); + Map<String, String> matches = new HashMap<String, String>(); + matches.put("key", "value"); + updatedPolicy.setMatches(matches); + loadedPolicies.add(updatedPolicy); + notification.setLoadedPolicies(loadedPolicies); + notificationController.notificationReceived(notification); + Boolean result = false; + PDPNotification newNotification= jsonStringToNotification(NotificationController.record(notification)); + for(LoadedPolicy loadedPolicy: newNotification.getLoadedPolicies()){ + if(loadedPolicy.getPolicyName().equals("Test") && loadedPolicy.getVersionNo().equals("1")){ + result = true; + } + } + assertEquals(result,true); + /*assertEquals( + NotificationController.record(notification), + "{\"removedPolicies\":[],\"updatedPolicies\":[{\"policyName\":\"Test\",\"versionNo\":\"1\",\"matches\":{\"key\":\"value\"},\"updateType\":\"NEW\"},{\"policyName\":\"Testing\",\"versionNo\":\"1\",\"matches\":{\"key\":\"value\"},\"updateType\":\"NEW\"}]}");*/ + } + + @Test + public void notificationReceivedRemovedTest() throws Exception{ + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.REMOVE); + Collection<StdRemovedPolicy> removedPolicies = new ArrayList<StdRemovedPolicy>(); + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + removedPolicy.setPolicyName("Testing"); + removedPolicy.setVersionNo("1"); + removedPolicies.add(removedPolicy); + notification.setRemovedPolicies(removedPolicies); + notificationController.notificationReceived(notification); + Boolean result = false; + PDPNotification newNotification= jsonStringToNotification(NotificationController.record(notification)); + for(RemovedPolicy removed: newNotification.getRemovedPolicies()){ + if(removed.getPolicyName().equals("Testing") && removed.getVersionNo().equals("1")){ + result = true; + } + } + assertEquals(result,true); + /*assertEquals( + NotificationController.record(notification), + "{\"removedPolicies\":[{\"policyName\":\"Test\",\"versionNo\":\"1\"},{\"policyName\":\"Testing\",\"versionNo\":\"1\"}],\"updatedPolicies\":[]}");*/ + } + + public StdPDPNotification jsonStringToNotification(String json) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + return notification = mapper.readValue(json, StdPDPNotification.class); + } + +} diff --git a/PyPDPServer/src/test/java/testpypdp/PolicyEngineServicesTest.java b/PyPDPServer/src/test/java/testpypdp/PolicyEngineServicesTest.java new file mode 100644 index 000000000..a451c84f0 --- /dev/null +++ b/PyPDPServer/src/test/java/testpypdp/PolicyEngineServicesTest.java @@ -0,0 +1,969 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * Copyright (C) 2017 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 testpypdp; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.ws.rs.core.MediaType; + +import org.openecomp.policy.pypdp.model_pojo.PepConfigFirewallPolicyRequest; +import org.openecomp.policy.pypdp.model_pojo.PepConfigPolicyNameRequest; +import org.openecomp.policy.pypdp.model_pojo.PepConfigPolicyRequest; +import org.openecomp.policy.pypdp.model_pojo.PepPushPolicyRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.policy.api.ConfigRequestParameters; +import org.openecomp.policy.api.DeletePolicyParameters; +import org.openecomp.policy.api.EventRequestParameters; +import org.openecomp.policy.api.PolicyParameters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.context.WebApplicationContext; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.openecomp.policy.pypdp.controller.Application; +import org.openecomp.policy.pypdp.controller.PolicyEngineServices; + +/** + * Test for Policy Engine REST Services + * + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = { Application.class, + PolicyEngineServices.class }) +@WebAppConfiguration +public class PolicyEngineServicesTest { + private MockMvc mockMvc; + // Don't Change this. + private static final String CONFIG_ERROR_MESSAGE = "[{\"policyConfigMessage\": \"PE300 - Data Issue: PolicyFile Name is empty\"," + + "\"policyConfigStatus\": \"CONFIG_NOT_FOUND\"," + + "\"type\": null," + + "\"config\": null," + + "\"policyName\": null," + + "\"policyVersion\": null," + + "\"matchingConditions\": null," + + "\"responseAttributes\": null," + "\"property\": null" + "}]"; + private static final String VALID_JSON = "{\"serviceTypeId\": \"/v0/firewall/pan\",\"configName\": \"rule1607\",\"deploymentOption\":{\"deployNow\": false},\"securityZoneId\": \"/v0/firewall/pan\",\"serviceGroups\": [{\"name\": \"1607Group\",\"description\": null,\"members\": [{\"type\": \"REFERENCE\",\"name\": \"SList\"},{\"type\": \"REFERENCE\",\"name\": \"Syslog\"}]}, {\"name\": \"Syslog\",\"description\": \"NA\",\"type\": \"SERVICE\",\"transportProtocol\": \"udp\",\"appProtocol\": null,\"ports\": \"514\"}, {\"name\": \"SList\",\"description\": \"Service List\",\"type\": \"SERVICE\",\"transportProtocol\": \"tcp\",\"appProtocol\": null,\"ports\": \"8080\"}],\"addressGroups\": [{\"name\": \"1607Group\",\"description\": null,\"members\": [{\"type\": \"REFERENCE\",\"name\": \"10.11.12.13/14\"},{\"type\": \"REFERENCE\",\"name\": \"10.11.12.13/14\"}]},{\"name\": \"PL_CCE3\",\"description\": \"CCE Routers\",\"members\":[{\"type\": \"REFERENCE\",\"name\": \"10.11.12.13/14\"}]}],\"firewallRuleList\": [{\"position\": \"1\",\"ruleName\": \"1607Rule\",\"fromZones\": [\"Trusted\"],\"toZones\": [\"Untrusted\"],\"negateSource\": false,\"negateDestination\": false,\"sourceList\": [{\"type\": \"REFERENCE\",\"name\": \"PL_CCE3\"}, {\"type\": \"REFERENCE\",\"name\": \"1607Group\"}],\"destinationList\": [{\"type\": \"REFERENCE\",\"name\": \"1607Group\"}],\"sourceServices\": [],\"destServices\": [{\"type\": \"REFERENCE\",\"name\": \"1607Group\"}],\"action\": \"accept\",\"description\": \"Rule for 1607 templates\",\"enabled\": true,\"log\": true}]}"; + private static final String INVALID_JSON = "{\"test\": \"value}"; + + @Autowired + private PolicyEngineServices policyEngineServicesMock; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setup() throws Exception { + this.mockMvc = webAppContextSetup(webApplicationContext).build(); + + } + + // Tests for getConfig API + @Test + public void getConfigUsingNoHeader() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setPolicyName(".*"); + mockMvc.perform( + post("/getConfig").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().is(400)); + } + + @Test + public void getConfigUsingErrorHeader() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setPolicyName(".*"); + mockMvc.perform( + post("/getConfig").header("X-ECOMP-RequestID", "Error123") + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isOk()); + } + + @Test + public void getConfigEmptyEcompName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setEcompName(""); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void getConfigEmptyPolicyName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setPolicyName(""); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void getConfigUsingErrorPolicyName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setPolicyName("test"); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void getConfigUsingErrorEcompName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setEcompName("test"); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void getConfigUsingALLPolicyName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setPolicyName(".*"); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isOk()); + } + + @Test + public void getConfigUsingNullPolicyName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void getConfigUsingNullEcompName() throws Exception { + ConfigRequestParameters pep = new ConfigRequestParameters(); + pep.setEcompName(null); + mockMvc.perform( + post("/getConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for GetConfigByPolicyName API + @Test + public void getConfigByPolicyNameUsingErrorHeader() throws Exception { + PepConfigPolicyNameRequest pep = new PepConfigPolicyNameRequest(); + pep.setPolicyName(null); + mockMvc.perform( + post("/getConfigByPolicyName") + .header("X-ECOMP-RequestID", "ERROR123") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().is(400)); + } + + @Test + public void getConfigByPolicyNameUsingNoHeader() throws Exception { + PepConfigPolicyNameRequest pep = new PepConfigPolicyNameRequest(); + pep.setPolicyName(null); + mockMvc.perform( + post("/getConfigByPolicyName").content( + this.ObjectToJsonString(pep)).contentType( + MediaType.APPLICATION_JSON)).andExpect(status().is(400)); + } + + @Test + public void getConfigByPolicyNameUsingEmptyPolicyName() throws Exception { + PepConfigPolicyNameRequest pep = new PepConfigPolicyNameRequest(); + pep.setPolicyName(""); + mockMvc.perform( + post("/getConfigByPolicyName") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(CONFIG_ERROR_MESSAGE)); + } + + @Test + public void getConfigByPolicyNameUsingNullPolicyName() throws Exception { + PepConfigPolicyNameRequest pep = new PepConfigPolicyNameRequest(); + pep.setPolicyName(null); + mockMvc.perform( + post("/getConfigByPolicyName") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(CONFIG_ERROR_MESSAGE)); + } + + @Test + public void getConfigByPolicyNameUsingALLPolicyName() throws Exception { + PepConfigPolicyNameRequest pep = new PepConfigPolicyNameRequest(); + pep.setPolicyName(".*"); + mockMvc.perform( + post("/getConfigByPolicyName") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isOk()); + } + + // Tests for SendEvent API + @Test + public void sendEventUsingNoHeader() throws Exception { + EventRequestParameters pep = new EventRequestParameters(); + pep.setEventAttributes(null); + mockMvc.perform( + post("/sendEvent").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void sendEventUsingErrorHeader() throws Exception { + EventRequestParameters pep = new EventRequestParameters(); + pep.setEventAttributes(null); + mockMvc.perform( + post("/sendEvent").header("X-ECOMP-RequestID", "ERROR123") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void sendEventUsingNullEventAttributes() throws Exception { + EventRequestParameters pep = new EventRequestParameters(); + pep.setEventAttributes(null); + mockMvc.perform( + post("/sendEvent") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void sendEventUsingEmptyEventAttributes() throws Exception { + EventRequestParameters pep = new EventRequestParameters(); + Map<String, String> emptyMap = new HashMap<String, String>(); + pep.setEventAttributes(emptyMap); + mockMvc.perform( + post("/sendEvent") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void sendEventUsingErrorEventAttributes() throws Exception { + EventRequestParameters pep = new EventRequestParameters(); + Map<String, String> eventMap = new HashMap<String, String>(); + eventMap.put("key", "value"); + pep.setEventAttributes(eventMap); + mockMvc.perform( + post("/sendEvent") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for Push Policy API + @Test + public void pushPolicyUsingNoHeader() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + mockMvc.perform( + put("/pushPolicy").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void pushPolicyUsingErrorHeader() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + mockMvc.perform( + put("/pushPolicy").header("X-ECOMP-RequestID", "Error123") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void pushPolicyUsingNullRequest() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + mockMvc.perform( + put("/pushPolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void pushPolicyUsingNoScope() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + pep.setPolicyName("Tarun"); + mockMvc.perform( + put("/pushPolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void pushPolicyUsingScopeinName() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + pep.setPolicyName("Test.PolicyName"); + mockMvc.perform( + put("/pushPolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void pushPolicyUsingPolicyType() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + pep.setPolicyName("Test.PolicyName"); + pep.setPolicyType("CONFIG BASE"); + mockMvc.perform( + put("/pushPolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void pushPolicyUsingErrorRequest() throws Exception { + PepPushPolicyRequest pep = new PepPushPolicyRequest(); + pep.setPolicyName("Test.PolicyName"); + pep.setPolicyType("CONFIG BASE"); + pep.setPdpGroup("default"); + mockMvc.perform( + put("/pushPolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for Delete Policy API + @Test + public void deletePolicyUsingNoHeader() throws Exception { + DeletePolicyParameters pep = new DeletePolicyParameters(); + mockMvc.perform( + delete("/deletePolicy").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void deletePolicyUsingErrorHeader() throws Exception { + DeletePolicyParameters pep = new DeletePolicyParameters(); + mockMvc.perform( + delete("/deletePolicy").header("X-ECOMP-RequestID", "ERROR123") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void deletePolicyUsingErrorPolicyName() throws Exception { + DeletePolicyParameters pep = new DeletePolicyParameters(); + pep.setPolicyName("test"); + mockMvc.perform( + delete("/deletePolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void deletePolicyUsingErrorPolicyComponent() throws Exception { + DeletePolicyParameters pep = new DeletePolicyParameters(); + pep.setPolicyName("test"); + pep.setPolicyComponent("test"); + mockMvc.perform( + delete("/deletePolicy") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for CreatePolicy API + @Test + public void createPolicyUsingNoHeader() throws Exception { + PolicyParameters pep = new PolicyParameters(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/createPolicy").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createPolicyUsingErrorHeader() throws Exception { + PolicyParameters pep = new PolicyParameters(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/createPolicy").content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", "Error123") + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createPolicyUsingNullParameters() throws Exception { + PolicyParameters pep = new PolicyParameters(); + mockMvc.perform( + put("/createPolicy") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createPolicyUsingEmptyPolicyName() throws Exception { + PolicyParameters pep = new PolicyParameters(); + pep.setPolicyName(""); + mockMvc.perform( + put("/createPolicy") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for UpdatePolicy API + @Test + public void updatePolicyUsingNoHeader() throws Exception { + PolicyParameters pep = new PolicyParameters(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/updatePolicy").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updatePolicyUsingErrorHeader() throws Exception { + PolicyParameters pep = new PolicyParameters(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/updatePolicy").content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", "Error123") + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updatePolicyUsingNullParameters() throws Exception { + PolicyParameters pep = new PolicyParameters(); + mockMvc.perform( + put("/updatePolicy") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updatePolicyUsingEmptyPolicyName() throws Exception { + PolicyParameters pep = new PolicyParameters(); + pep.setPolicyName(""); + mockMvc.perform( + put("/updatePolicy") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for createConfig API + @Test + public void createConfigUsingNoHeader() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/createConfig").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createConfigUsingErrorHeader() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/createConfig").content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", "Error123") + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createConfigUsingNullParameters() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + mockMvc.perform( + put("/createConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createConfigUsingEmptyPolicyName() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName(""); + mockMvc.perform( + put("/createConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createConfigUsingEmptyConfigName() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + pep.setEcompName("ecomp"); + mockMvc.perform( + put("/createConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createConfigUsingEmptyPolicyScope() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + pep.setEcompName("ecomp"); + pep.setConfigName("config"); + mockMvc.perform( + put("/createConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createConfigUsingErrorPolicyScope() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + pep.setEcompName("ecomp"); + pep.setConfigName("config"); + pep.setPolicyScope("test"); + mockMvc.perform( + put("/createConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Test API for updateConfig API + @Test + public void updateConfigUsingNoHeader() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/updateConfig").content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateConfigUsingErrorHeader() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/updateConfig").content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", "Error123") + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateConfigUsingNullParameters() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + mockMvc.perform( + put("/updateConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateConfigUsingEmptyPolicyName() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName(""); + mockMvc.perform( + put("/updateConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateConfigUsingEmptyConfigName() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + pep.setEcompName("ecomp"); + mockMvc.perform( + put("/updateConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateConfigUsingEmptyPolicyScope() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + pep.setEcompName("ecomp"); + pep.setConfigName("config"); + mockMvc.perform( + put("/updateConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateConfigUsingErrorPolicyScope() throws Exception { + PepConfigPolicyRequest pep = new PepConfigPolicyRequest(); + pep.setPolicyName("test"); + pep.setEcompName("ecomp"); + pep.setConfigName("config"); + pep.setPolicyScope("test"); + mockMvc.perform( + put("/updateConfig") + .content(this.ObjectToJsonString(pep)) + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + // Tests for createFirewallConfig API + @Test + public void createFirewallConfigUsingNoHeader() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/createFirewallConfig").content( + this.ObjectToJsonString(pep)).contentType( + MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createFirewallConfigUsingErrorHeader() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/createFirewallConfig") + .header("X-ECOMP-RequestID", "Error123") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createFirewallConfigUsingNullParameters() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + mockMvc.perform( + put("/createFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createFirewallConfigUsingEmptyPolicyName() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName(""); + mockMvc.perform( + put("/createFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createFirewallConfigUsingEmptyPolicyScope() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + pep.setFirewallJson(VALID_JSON); + pep.setPolicyScope(""); + mockMvc.perform( + put("/createFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createFirewallConfigUsingInValidJSON() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + pep.setFirewallJson(INVALID_JSON); + pep.setPolicyScope("test"); + mockMvc.perform( + put("/createFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void createFirewallConfigUsingValidJSON() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + pep.setFirewallJson(VALID_JSON); + pep.setPolicyScope("test"); + mockMvc.perform( + put("/createFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isOk()); + } + + // Tests for updateFirewallConfig API + @Test + public void updateFirewallConfigUsingNoHeader() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/updateFirewallConfig").content( + this.ObjectToJsonString(pep)).contentType( + MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateFirewallConfigUsingErrorHeader() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + mockMvc.perform( + put("/updateFirewallConfig") + .header("X-ECOMP-RequestID", "Error123") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateFirewallConfigUsingNullParameters() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + mockMvc.perform( + put("/updateFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateFirewallConfigUsingEmptyPolicyName() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName(""); + mockMvc.perform( + put("/updateFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateFirewallConfigUsingEmptyPolicyScope() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + pep.setFirewallJson(VALID_JSON); + pep.setPolicyScope(""); + mockMvc.perform( + put("/updateFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateFirewallConfigUsingInValidJSON() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + pep.setFirewallJson(INVALID_JSON); + pep.setPolicyScope("test"); + mockMvc.perform( + put("/updateFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isBadRequest()); + } + + @Test + public void updateFirewallConfigUsingValidJSON() throws Exception { + PepConfigFirewallPolicyRequest pep = new PepConfigFirewallPolicyRequest(); + pep.setPolicyName("test"); + pep.setFirewallJson(VALID_JSON); + pep.setPolicyScope("test"); + mockMvc.perform( + put("/updateFirewallConfig") + .header("X-ECOMP-RequestID", + UUID.randomUUID().toString()) + .header("ClientAuth", "Basic bTAzNzQyOlBvbGljeVIwY2sk") + .content(this.ObjectToJsonString(pep)) + .contentType(MediaType.APPLICATION_JSON)).andExpect( + status().isOk()); + } + + //Health Check Tests + @Test + public void getCountTest() throws Exception { + mockMvc.perform(get("/count")) + .andExpect(status().isOk()); + } + + @Test + public void getPDPsTest() throws Exception { + mockMvc.perform(get("/pdps")) + .andExpect(status().isOk()); + } + + @Test + public void getPAPsTest() throws Exception { + mockMvc.perform(get("/paps")) + .andExpect(status().isOk()); + } + + // Helper Method to create JSONString from a given Object. + public String ObjectToJsonString(Object o) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.writeValueAsString(o); + } + +} |