aboutsummaryrefslogtreecommitdiffstats
path: root/examples/examples-periodic
diff options
context:
space:
mode:
authorRossC <ross.carter@est.tech>2020-05-20 15:12:22 +0100
committerRossC <ross.carter@est.tech>2020-05-20 18:16:42 +0100
commit7535f8327e6c2654876a8188541cd68f3b712628 (patch)
treec9a0625be75e7d06b1b0020156e446518588c9e4 /examples/examples-periodic
parent3f29d382bbea5587e340877c25d262a763f3a25f (diff)
Bump Apex to 2.3.3
Issue-ID: POLICY-2514 Change-Id: I5b130b052150eb0e51759c751b83ab9d6f199759 Signed-off-by: RossC <ross.carter@est.tech>
Diffstat (limited to 'examples/examples-periodic')
-rw-r--r--examples/examples-periodic/pom.xml2
1 files changed, 1 insertions, 1 deletions
diff --git a/examples/examples-periodic/pom.xml b/examples/examples-periodic/pom.xml
index b4b80fef2..ef7b9c99f 100644
--- a/examples/examples-periodic/pom.xml
+++ b/examples/examples-periodic/pom.xml
@@ -22,7 +22,7 @@
<parent>
<groupId>org.onap.policy.apex-pdp.examples</groupId>
<artifactId>examples</artifactId>
- <version>2.3.2-SNAPSHOT</version>
+ <version>2.3.3-SNAPSHOT</version>
</parent>
<artifactId>examples-periodic</artifactId>
/a> 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * 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 org.onap.appc.dg.util.impl;

import com.att.eelf.configuration.EELFLogger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.onap.ccsdk.sli.adaptors.aai.AAIService;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
import org.onap.appc.exceptions.APPCException;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest({FrameworkUtil.class, Thread.class})
public class ExecuteNodeActionImplTest {
    private ExecuteNodeActionImpl mockedExecuteNodeActionImpl = PowerMockito.spy(new ExecuteNodeActionImpl());
    @Mock
    private EELFLogger eelfLogger;
    @Mock
    private AAIService aaiService;

    private final String resourceType = "resourceType";
    private final String prefix = "prefix";
    private final String resourceKey = "resourceKey";
    private final String attributeName = "attributeName";
    private final String attributeValue = "attributeValue";

    private Map<String, String> params = new HashMap<>();
    private SvcLogicContext svcLogicContext = new SvcLogicContext();
    private SvcLogicResource.QueryStatus queryStatus = SvcLogicResource.QueryStatus.SUCCESS;

    @Before
    public void setUp() throws Exception {
        Whitebox.setInternalState(mockedExecuteNodeActionImpl, "aaiService", aaiService);

        params.put("resourceType", resourceType);
        params.put("prefix", prefix);
        params.put("resourceKey", resourceKey);
        params.put("attributeName", attributeName);
        params.put("attributeValue", attributeValue);
    }

    @Test
    public void testGetAAIservice() throws Exception {
        // sref is not null
        mockStatic(FrameworkUtil.class);
        Bundle mockedBundle = mock(Bundle.class);
        BundleContext mockedBundleContext = mock(BundleContext.class);
        ServiceReference mockedServiceReference = mock(ServiceReference.class);
        PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
        PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
        PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
                .getServiceReference(AAIService.class.getName());

        Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "initialize");
        verify(mockedBundleContext, times(1)).getService(mockedServiceReference);

        // sref is null
        PowerMockito.doReturn(null).when(mockedBundleContext).getServiceReference(AAIService.class.getName());
        Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
        verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
    }

    @Test
    public void testWaitMethod() throws Exception {
        mockStatic(Thread.class);
        params.put("waitTime", "1");
        mockedExecuteNodeActionImpl.waitMethod(params, svcLogicContext);
        verifyStatic(times(1));
    }

    @Test
    public void testGetResource() throws Exception {
        AAIService aaiService = setupForResourceTests();
        PowerMockito.doReturn(queryStatus).when(aaiService).query(Mockito.<String>any(), Mockito.anyBoolean(),
                Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(),
                Mockito.any(SvcLogicContext.class));

        mockedExecuteNodeActionImpl.getResource(params, svcLogicContext);

        verify(aaiService, times(1)).query(resourceType, false, null, resourceKey, prefix, null, svcLogicContext);
        assertEquals(queryStatus.toString(), svcLogicContext.getAttribute("getResource_result"));
    }

    @Test
    public void testPostResource() throws Exception {
        AAIService aaiService = setupForResourceTests();
        PowerMockito.doReturn(queryStatus).when(aaiService).update(eq(resourceType), eq(resourceKey), anyMap(),
                eq(prefix), eq(svcLogicContext));

        mockedExecuteNodeActionImpl.postResource(params, svcLogicContext);

        verify(aaiService, times(1)).update(eq(resourceType), eq(resourceKey), anyMap(), eq(prefix),
                eq(svcLogicContext));
        assertEquals(svcLogicContext.getAttribute("postResource_result"), queryStatus.toString());
    }

    @Test
    public void testDeleteResource() throws Exception {
        AAIService aaiService = setupForResourceTests();

        PowerMockito.doReturn(queryStatus).when(aaiService).delete(eq(resourceType), eq(resourceKey),
                eq(svcLogicContext));

        mockedExecuteNodeActionImpl.deleteResource(params, svcLogicContext);

        verify(aaiService, times(1)).delete(eq(resourceType), eq(resourceKey), eq(svcLogicContext));
        assertEquals(svcLogicContext.getAttribute("deleteResource_result"), queryStatus.toString());
    }

    @Test
    public void testGetVnfHierarchySuccess() throws Exception {
        AAIService aaiService = setupForResourceTests();

        PowerMockito.doReturn(queryStatus).when(aaiService).query(Mockito.<String>any(), Mockito.anyBoolean(),
                Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(),
                Mockito.any(SvcLogicContext.class));

        mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);

        assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
        assertEquals("SUCCESS", svcLogicContext.getAttribute("getVnfHierarchy_result"));
    }

    @Test(expected = APPCException.class)
    public void testGetVnfHierarchyFailure() throws Exception {
        queryStatus = SvcLogicResource.QueryStatus.FAILURE;
        AAIService aaiService = setupForResourceTests();
        PowerMockito.doReturn(queryStatus).when(aaiService).query(Mockito.<String>any(), Mockito.anyBoolean(),
                Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(),
                Mockito.any(SvcLogicContext.class));

        mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);

        assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
        assertEquals("FAILURE", svcLogicContext.getAttribute("getVnfHierarchy_result"));
        assertTrue(svcLogicContext.getAttribute("output.status.message") != null);
    }

    @Test
    public void testPopulateVnfcsDetailsinContext() throws Exception {
        Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
        Set<String> vServersList = new HashSet<>();
        vnfcHierarchyMap.put("SMP", vServersList);
        vServersList.add("smp-0-url");
        vServersList.add("smp-1-url");

        AAIService aaiService = setupForResourceTests();
        PowerMockito.when(aaiService.query(eq("vnfc"), eq(false), anyString(), eq("vnfc-name = 'SMP'"),
                eq("vnfcRetrived"), anyString(), any(SvcLogicContext.class))).thenReturn(queryStatus);

        Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", vnfcHierarchyMap,
                svcLogicContext);

        verify(mockedExecuteNodeActionImpl, times(1)).getResource(anyMap(), any(SvcLogicContext.class));
        assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].TYPE"));
        assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].NAME"));
        assertEquals("2", svcLogicContext.getAttribute("VNF.VNFC[0].VM_COUNT"));
        assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[0].URL")));
        assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[1].URL")));
    }

    private AAIService setupForResourceTests() {
        mockStatic(FrameworkUtil.class);
        Bundle mockedBundle = mock(Bundle.class);
        BundleContext mockedBundleContext = mock(BundleContext.class);
        ServiceReference mockedServiceReference = mock(ServiceReference.class);
        PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
        PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
        PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
                .getServiceReference(AAIService.class.getName());
        AAIService aaiService = PowerMockito.mock(AAIService.class);
        PowerMockito.doReturn(aaiService).when(mockedBundleContext).getService(mockedServiceReference);
        return aaiService;
    }
}