From 10e7b82bfc65b11df9497577ff6f16ed10564d69 Mon Sep 17 00:00:00 2001 From: "adheli.tavares" Date: Wed, 7 Dec 2022 14:59:37 +0000 Subject: SLAs for async methods - undeploy policy timing - deploy policy timing - unit tests for actuator endpoints Issue-ID: POLICY-4469 Change-Id: I3298888e59824347b8d2a57fb531c10f2f4ca125 Signed-off-by: adheli.tavares --- .../onap/policy/pap/main/rest/ProviderSuper.java | 17 ++-- .../pap/main/rest/TestActuatorEndpoints.java | 99 ++++++++++++++++++++++ .../pap/main/rest/TestPdpGroupDeleteProvider.java | 32 ++----- 3 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestActuatorEndpoints.java (limited to 'main/src/test/java') diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java index b5667145..857309c8 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java @@ -3,7 +3,7 @@ * ONAP PAP * ================================================================================ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021-2022 Nordix Foundation. * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,8 +33,9 @@ import static org.mockito.Mockito.when; import io.micrometer.core.instrument.MeterRegistry; import java.io.File; import java.util.ArrayList; -import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.junit.Before; import org.mockito.ArgumentCaptor; @@ -160,9 +161,8 @@ public class ProviderSuper { * Gets the input to the create() method. * * @return the input that was passed to the dao.updatePdpGroups() method - * @throws Exception if an error occurred */ - protected List getGroupCreates() throws Exception { + protected List getGroupCreates() { verify(pdpGroupService).createPdpGroups(updateCaptor.capture()); return copyList(updateCaptor.getValue()); @@ -172,9 +172,8 @@ public class ProviderSuper { * Gets the input to the update() method. * * @return the input that was passed to the dao.updatePdpGroups() method - * @throws Exception if an error occurred */ - protected List getGroupUpdates() throws Exception { + protected List getGroupUpdates() { verify(pdpGroupService).updatePdpGroups(updateCaptor.capture()); return copyList(updateCaptor.getValue()); @@ -191,7 +190,7 @@ public class ProviderSuper { verify(reqmap, times(count)).addRequest(any(), captor.capture()); - return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList()); + return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList()); } /** @@ -205,7 +204,7 @@ public class ProviderSuper { verify(reqmap, times(count)).addRequest(captor.capture(), any()); - return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList()); + return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList()); } /** @@ -216,7 +215,7 @@ public class ProviderSuper { */ private List copyList(List source) { List newlst = new ArrayList<>(source); - Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName())); + newlst.sort(Comparator.comparing(PdpGroup::getName)); return newlst; } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestActuatorEndpoints.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestActuatorEndpoints.java new file mode 100644 index 00000000..967da6c3 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestActuatorEndpoints.java @@ -0,0 +1,99 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.runner.RunWith; +import org.onap.policy.common.utils.services.Registry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.core.annotation.Order; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT) +@ActiveProfiles("test") +@AutoConfigureMockMvc +@ContextConfiguration +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +public class TestActuatorEndpoints { + + @Autowired + private WebApplicationContext context; + + @Autowired + private TestSecurityConfig securityConfig; + + @Autowired + private MockMvc mock; + + @BeforeClass + public static void setupClass() { + Registry.newRegistry(); + } + + @BeforeEach + void setup() { + mock = MockMvcBuilders.webAppContextSetup(context).build(); + } + + @Test + public void testMetricsEndpoint() throws Exception { + mock.perform(get("/plain-metrics")).andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isNotEmpty()); + } + + @Test + public void testPrometheusEndpoint() throws Exception { + mock.perform(get("/metrics")).andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isNotEmpty()); + } + + @TestConfiguration + @Order(1) + public static class TestSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity httpSecurity) throws Exception { + httpSecurity.csrf().disable() + .authorizeRequests().anyRequest().permitAll(); + } + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java index 449d48f2..0b6c57e7 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java @@ -3,7 +3,7 @@ * ONAP PAP * ================================================================================ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2020-2021 Nordix Foundation. + * Modifications Copyright (C) 2020-2022 Nordix Foundation. * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,7 +35,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.Arrays; import java.util.List; import java.util.Set; import javax.ws.rs.core.Response.Status; @@ -96,20 +95,6 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { updater = prov.makeUpdater(session, policy1, fullIdent); } - @Test - public void testDeleteGroup_Inctive() throws Exception { - PdpGroup group = loadGroup("deleteGroup.json"); - - when(session.getGroup(GROUP1_NAME)).thenReturn(group); - - prov.deleteGroup(GROUP1_NAME); - - verify(session).deleteGroupFromDb(group); - - // should be no PDP requests - verify(session, never()).addRequests(any(), any()); - } - @Test public void testDeleteGroup_Active() throws Exception { PdpGroup group = loadGroup("deleteGroup.json"); @@ -123,7 +108,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } @Test - public void testDeleteGroup_NotFound() throws Exception { + public void testDeleteGroup_NotFound() { assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class) .hasMessage("group not found") .extracting(ex -> ((PfModelException) ex).getErrorResponse().getResponseCode()) @@ -140,7 +125,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { verify(session).deleteGroupFromDb(group); - // should done no requests for the PDPs + // should have done no requests for the PDPs verify(session, never()).addRequests(any(), any()); } @@ -163,12 +148,12 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { */ @Test public void testUndeploy_Full() throws Exception { - when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + when(toscaService.getFilteredPolicyList(any())).thenReturn(List.of(policy1)); PdpGroup group = loadGroup("undeploy.json"); - when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group)); - when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group)); + when(toscaService.getFilteredPolicyList(any())).thenReturn(List.of(policy1)); PdpGroupDeleteProvider deleteProvider = new PdpGroupDeleteProvider(); super.initialize(deleteProvider); @@ -190,11 +175,11 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { assertEquals("pdpA", req.getName()); assertEquals(GROUP1_NAME, req.getPdpGroup()); assertEquals("pdpTypeA", req.getPdpSubgroup()); - assertEquals(Arrays.asList(policy1.getIdentifier()), req.getPoliciesToBeUndeployed()); + assertEquals(List.of(policy1.getIdentifier()), req.getPoliciesToBeUndeployed()); } @Test - public void testUndeployPolicy_NotFound() throws Exception { + public void testUndeployPolicy_NotFound() { when(session.isUnchanged()).thenReturn(true); assertThatThrownBy(() -> prov.undeploy(optIdent, DEFAULT_USER)).isInstanceOf(PfModelException.class) @@ -282,6 +267,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } private class MyProvider extends PdpGroupDeleteProvider { + private MyProvider() { super.initialize(); } -- cgit 1.2.3-korg