SLAs for async methods
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / ProviderSuper.java
index 852516b..857309c 100644 (file)
@@ -2,7 +2,9 @@
  * ============LICENSE_START=======================================================
  * ONAP PAP
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
+ * 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");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.pap.main.rest;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 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;
@@ -42,30 +47,42 @@ import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.pap.concepts.PolicyNotification;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpGroups;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
-import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.pap.main.PapConstants;
-import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
 import org.onap.policy.pap.main.notification.PolicyNotifier;
+import org.onap.policy.pap.main.service.PdpGroupService;
+import org.onap.policy.pap.main.service.PolicyAuditService;
+import org.onap.policy.pap.main.service.PolicyStatusService;
+import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
 
 /**
  * Super class for TestPdpGroupDeployProviderXxx classes.
  */
 public class ProviderSuper {
     private static final Coder coder = new StandardCoder();
+    public static final String DEFAULT_USER = "PAP_TEST";
 
     @Mock
-    protected PolicyModelsProvider dao;
+    protected PdpGroupService pdpGroupService;
 
     @Mock
-    protected PolicyNotifier notifier;
+    protected PolicyStatusService policyStatusService;
 
+    @Mock
+    protected PolicyAuditService policyAuditService;
+
+    @Mock
+    protected ToscaServiceTemplateService toscaService;
+
+    @Mock
+    protected PolicyNotifier notifier;
 
     /**
      * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
@@ -75,9 +92,9 @@ public class ProviderSuper {
 
     protected Object lockit;
     protected PdpModifyRequestMap reqmap;
-    protected PolicyModelsProviderFactoryWrapper daofact;
     protected ToscaPolicy policy1;
-
+    protected PapStatisticsManager statsmanager;
+    protected MeterRegistry meterRegistry;
 
     /**
      * Configures DAO, captors, and various mocks.
@@ -87,27 +104,42 @@ public class ProviderSuper {
 
         Registry.newRegistry();
 
-        MockitoAnnotations.initMocks(this);
+        MockitoAnnotations.openMocks(this);
 
         reqmap = mock(PdpModifyRequestMap.class);
 
         lockit = new Object();
-        daofact = mock(PolicyModelsProviderFactoryWrapper.class);
         policy1 = loadPolicy("policy.json");
+        statsmanager = mock(PapStatisticsManager.class);
 
-        when(daofact.create()).thenReturn(dao);
+        meterRegistry = mock(MeterRegistry.class);
 
         List<PdpGroup> groups = loadGroups("groups.json");
 
-        when(dao.getFilteredPdpGroups(any())).thenReturn(groups);
+        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
 
-        when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgumentAt(0, List.class));
-        when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgumentAt(0, List.class));
+        when(pdpGroupService.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
+        when(pdpGroupService.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
 
         Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
         Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
-        Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
-        Registry.register(PapConstants.REG_POLICY_NOTIFIER, notifier);
+        Registry.register(PapConstants.REG_STATISTICS_MANAGER, statsmanager);
+        Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry);
+
+    }
+
+    /**
+     * Initialize services to the provider for tests.
+     *
+     * @param prov the provider
+     */
+    public void initialize(ProviderBase prov) {
+        prov.setPdpGroupService(pdpGroupService);
+        prov.setPolicyAuditService(policyAuditService);
+        prov.setPolicyStatusService(policyStatusService);
+        prov.setToscaService(toscaService);
+        prov.setPolicyNotifier(notifier);
+        prov.initialize();
     }
 
     protected void assertGroup(List<PdpGroup> groups, String name) {
@@ -129,10 +161,9 @@ 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<PdpGroup> getGroupCreates() throws Exception {
-        verify(dao).createPdpGroups(updateCaptor.capture());
+    protected List<PdpGroup> getGroupCreates() {
+        verify(pdpGroupService).createPdpGroups(updateCaptor.capture());
 
         return copyList(updateCaptor.getValue());
     }
@@ -141,10 +172,9 @@ 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<PdpGroup> getGroupUpdates() throws Exception {
-        verify(dao).updatePdpGroups(updateCaptor.capture());
+    protected List<PdpGroup> getGroupUpdates() {
+        verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
 
         return copyList(updateCaptor.getValue());
     }
@@ -160,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());
     }
 
     /**
@@ -174,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());
     }
 
     /**
@@ -185,7 +215,7 @@ public class ProviderSuper {
      */
     private List<PdpGroup> copyList(List<PdpGroup> source) {
         List<PdpGroup> newlst = new ArrayList<>(source);
-        Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName()));
+        newlst.sort(Comparator.comparing(PdpGroup::getName));
         return newlst;
     }
 
@@ -266,6 +296,15 @@ public class ProviderSuper {
         }
     }
 
+    /**
+     * Verifies that an empty notification was published.
+     */
+    protected void checkEmptyNotification() {
+        ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
+        verify(notifier).publish(captor.capture());
+        assertThat(captor.getValue().isEmpty()).isTrue();
+    }
+
     /**
      * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
      * the list and decode it into the wrapper before extracting the list contents.