From 3ec05a1093e6399c8e732cecfa1dfd5c50a8cb09 Mon Sep 17 00:00:00 2001 From: "m.kowalski3" Date: Wed, 15 May 2019 09:29:31 +0200 Subject: Improve test coverage in SdcSingleController Change-Id: Ibbdbf890aef92f5fb3a37bc75a502591d6134147 Issue-ID: CLAMP-355 Signed-off-by: Marcin Kowalski --- .../sdc/controller/SdcSingleControllerItCase.java | 51 +++++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java b/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java index 55657c97..0f0ecaed 100644 --- a/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java @@ -5,6 +5,8 @@ * Copyright (C) 2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -23,9 +25,13 @@ package org.onap.clamp.clds.it.sdc.controller; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; - import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; @@ -33,12 +39,16 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfigurationTest; +import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException; import org.onap.clamp.clds.sdc.controller.SdcSingleController; +import org.onap.clamp.clds.sdc.controller.SdcSingleControllerStatus; import org.onap.clamp.clds.sdc.controller.installer.CsarHandler; import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.notification.IResourceInstance; +import org.slf4j.MDC; +import org.slf4j.spi.MDCAdapter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -59,24 +69,26 @@ public class SdcSingleControllerItCase { private SdcSingleController sdcSingleController; + private CsarInstaller csarInstaller = mock(CsarInstaller.class); + private INotificationData buildFakeSdcNotification() { // BUild what is needed for CSAR - IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class); + IArtifactInfo serviceArtifact = mock(IArtifactInfo.class); Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE); Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME); List servicesList = new ArrayList<>(); servicesList.add(serviceArtifact); - INotificationData notifData = Mockito.mock(INotificationData.class); + INotificationData notifData = mock(INotificationData.class); Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList); // Build what is needed for UUID Mockito.when(notifData.getServiceInvariantUUID()).thenReturn(SERVICE_UUID); // Build fake resource with one artifact BLUEPRINT - IResourceInstance resource1 = Mockito.mock(IResourceInstance.class); + IResourceInstance resource1 = mock(IResourceInstance.class); Mockito.when(resource1.getResourceType()).thenReturn("VF"); Mockito.when(resource1.getResourceInvariantUUID()).thenReturn(RESOURCE1_UUID); Mockito.when(resource1.getResourceInstanceName()).thenReturn(RESOURCE1_INSTANCE_NAME); // Create a fake artifact for resource - IArtifactInfo blueprintArtifact = Mockito.mock(IArtifactInfo.class); + IArtifactInfo blueprintArtifact = mock(IArtifactInfo.class); Mockito.when(blueprintArtifact.getArtifactType()).thenReturn(CsarHandler.BLUEPRINT_TYPE); List artifactsListForResource = new ArrayList<>(); artifactsListForResource.add(blueprintArtifact); @@ -92,7 +104,7 @@ public class SdcSingleControllerItCase { */ @Before public void init() { - sdcSingleController = new SdcSingleController(clampProp, Mockito.mock(CsarInstaller.class), + sdcSingleController = new SdcSingleController(clampProp, csarInstaller, SdcSingleControllerConfigurationTest.loadControllerConfiguration("clds/sdc-controller-config-TLS.json", "sdc-controller1"), null) { @@ -101,9 +113,36 @@ public class SdcSingleControllerItCase { @Test public void testTreatNotification() { + //when sdcSingleController.treatNotification(buildFakeSdcNotification()); + //then Assertions.assertThat(sdcSingleController.getNbOfNotificationsOngoing()).isEqualTo(0); + } + @Test + public void testCloseSdc() throws SdcControllerException { + //when + sdcSingleController.closeSdc(); + //then + assertThat(sdcSingleController.getControllerStatus()).isEqualTo(SdcSingleControllerStatus.STOPPED); } + @Test + public void testActivateCallback() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, ClassNotFoundException { + //given + MDCAdapter mdcAdapter = MDC.getMDCAdapter(); + Class innerClass = Class.forName("org.onap.clamp.clds.sdc.controller.SdcSingleController$SdcNotificationCallBack"); + Constructor constructor = innerClass.getDeclaredConstructor(SdcSingleController.class, SdcSingleController.class); + constructor.setAccessible(true); + Object child = constructor.newInstance(sdcSingleController,sdcSingleController); + Method method = child.getClass().getDeclaredMethod("activateCallback",INotificationData.class); + method.setAccessible(true); + //when + method.invoke(child,buildFakeSdcNotification()); + //then + assertThat(mdcAdapter.get("ResponseCode")).isEqualTo("0"); + assertThat(mdcAdapter.get("StatusCode")).isEqualTo("COMPLETE"); + assertThat(mdcAdapter.get("ResponseDescription")).isEqualTo("SDC Notification received and processed successfully"); + assertThat(mdcAdapter.get("ClassName")).isEqualTo(child.getClass().getName()); + } } -- cgit 1.2.3-korg