From e601bbdc43bae9a08e2e10c5139a6f76b47860d7 Mon Sep 17 00:00:00 2001 From: Einat Vinouze Date: Tue, 16 Jul 2019 17:17:36 +0300 Subject: Implant vid-app-common org.onap.vid.job (main and test) Issue-ID: VID-378 Change-Id: I41b0bdc2c4e3635f3f3319b1cd63cefc61912dfc Signed-off-by: Einat Vinouze Signed-off-by: Ittay Stern --- .../onap/vid/job/command/ResourceCommandTest.java | 258 +++++++++++++++++---- 1 file changed, 218 insertions(+), 40 deletions(-) (limited to 'vid-app-common/src/test/java/org/onap/vid/job/command/ResourceCommandTest.java') diff --git a/vid-app-common/src/test/java/org/onap/vid/job/command/ResourceCommandTest.java b/vid-app-common/src/test/java/org/onap/vid/job/command/ResourceCommandTest.java index 9f252c5e8..2780e1515 100644 --- a/vid-app-common/src/test/java/org/onap/vid/job/command/ResourceCommandTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/job/command/ResourceCommandTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,29 +20,38 @@ package org.onap.vid.job.command; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import org.jetbrains.annotations.NotNull; +import org.onap.vid.exceptions.AbortingException; import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.exceptions.TryAgainException; import org.onap.vid.job.Job; import org.onap.vid.job.JobAdapter; +import org.onap.vid.job.JobsBrokerService; import org.onap.vid.job.NextCommand; import org.onap.vid.job.impl.JobSharedData; import org.onap.vid.model.Action; -import org.onap.vid.model.serviceInstantiation.BaseResource; +import org.onap.vid.model.serviceInstantiation.*; import org.onap.vid.mso.RestMsoImplementation; +import org.onap.vid.mso.model.ModelInfo; import org.springframework.http.HttpMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import javax.ws.rs.ProcessingException; -import java.util.Collections; -import java.util.Optional; +import java.util.*; +import java.util.stream.Collectors; import java.util.stream.Stream; +import static java.util.Collections.emptyList; +import static org.mockito.AdditionalAnswers.returnsFirstArg; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.onap.vid.job.command.ResourceCommandKt.ACTION_PHASE; import static org.onap.vid.job.command.ResourceCommandKt.INTERNAL_STATE; +import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.*; +import static org.onap.vid.model.Action.*; import static org.onap.vid.utils.Logging.getMethodCallerName; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; @@ -54,18 +63,32 @@ public class ResourceCommandTest { public static class MockCommand extends ResourceCommand { public MockCommand(InternalState mockState, Action mockPhase, Job.JobStatus mockedJobStatus) { - super(mock(RestMsoImplementation.class, RETURNS_MOCKS), mock(InProgressStatusService.class), mock(MsoResultHandlerService.class, RETURNS_MOCKS), mock(WatchChildrenJobsBL.class)); + this(mockState, mockPhase, mockedJobStatus, false); + } + public MockCommand(InternalState mockState, Action mockPhase, Job.JobStatus mockedJobStatus, boolean lateInit) { + super( + mock(RestMsoImplementation.class, RETURNS_MOCKS), + mock(InProgressStatusService.class), + mock(MsoResultHandlerService.class, RETURNS_MOCKS), + mock(WatchChildrenJobsBL.class), + mock(JobsBrokerService.class, RETURNS_MOCKS), + mock(JobAdapter.class, RETURNS_MOCKS)); this.mockedJobStatus = mockedJobStatus; this.mockState = mockState; this.mockPhase = mockPhase; - if (mockState==InternalState.INITIAL) { - init(mock(JobSharedData.class), Collections.emptyMap()); + if (!lateInit) { + init(); } - else { + when(this.getWatchChildrenJobsBL().cumulateJobStatus(any(), any())).thenReturn(mockedJobStatus); + } + + protected void init() { + if (mockState == InternalState.INITIAL) { + init(mock(JobSharedData.class), Collections.emptyMap()); + } else { init(mock(JobSharedData.class), ImmutableMap.of(INTERNAL_STATE, mockState.name(), ACTION_PHASE, mockPhase.name())); } - when(this.getWatchChildrenJobsBL().cumulateJobStatus(any(), any())).thenReturn(mockedJobStatus); } private final Job.JobStatus mockedJobStatus; @@ -76,7 +99,7 @@ public class ResourceCommandTest { @NotNull @Override public Job.JobStatus createChildren() { - if (mockState == InternalState.CREATING_CHILDREN || (mockState == InternalState.INITIAL && mockPhase== Action.Delete)) + if (mockState == InternalState.CREATING_CHILDREN || (mockState == InternalState.INITIAL && mockPhase== Delete)) return mockedJobStatus; throw (new RuntimeException("Not expected to call "+getMethodCallerName())); } @@ -95,7 +118,7 @@ public class ResourceCommandTest { @NotNull @Override - public MsoRestCallPlan planCreateMyselfRestCall(@NotNull CommandParentData commandParentData, @NotNull JobAdapter.AsyncJobRequest request, @NotNull String userId) { + public MsoRestCallPlan planCreateMyselfRestCall(@NotNull CommandParentData commandParentData, @NotNull JobAdapter.AsyncJobRequest request, @NotNull String userId, String testApi) { return mockedPlanOrThrow(InternalState.CREATE_MYSELF); } @@ -109,16 +132,19 @@ public class ResourceCommandTest { public static class MockCommandTestingStateMachine extends MockCommand { private final JobSharedData sharedData; + private final boolean isDescendantHasAction; public MockCommandTestingStateMachine(InternalState mockState, Action mockPhase, Job.JobStatus mockedJobStatus, boolean mockedNeedToDeleteMySelf) { - this(mockState, mockPhase, mockedJobStatus, mockedNeedToDeleteMySelf, false); + this(mockState, mockPhase, mockedJobStatus, mockedNeedToDeleteMySelf, false, true); } - public MockCommandTestingStateMachine(InternalState mockState, Action mockPhase, Job.JobStatus mockedJobStatus, boolean mockedNeedToDeleteMySelf, boolean isService) { - super(mockState, mockPhase, mockedJobStatus); + public MockCommandTestingStateMachine(InternalState mockState, Action mockPhase, Job.JobStatus mockedJobStatus, boolean mockedNeedToDeleteMySelf, boolean isService, boolean isDescendantHasAction) { + super(mockState, mockPhase, mockedJobStatus, true); this.mockedNeedToDeleteMySelf = mockedNeedToDeleteMySelf; this.isService = isService; this.sharedData = mock(JobSharedData.class, RETURNS_MOCKS); + this.isDescendantHasAction = isDescendantHasAction; + init(); } protected final boolean mockedNeedToDeleteMySelf; @@ -150,6 +176,11 @@ public class ResourceCommandTest { public JobSharedData getSharedData() { return sharedData; } + + @Override + protected boolean isDescendantHasAction(@NotNull Action phase) { + return isDescendantHasAction; + } } @DataProvider @@ -171,28 +202,28 @@ public class ResourceCommandTest { InternalState internalState, Job.JobStatus jobStatus, InternalState expectedState) { //there is no meaning to the constructor inputs here - MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.TERMINAL, Action.Delete, Job.JobStatus.FAILED, true); + MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.TERMINAL, Delete, Job.JobStatus.FAILED, true); assertEquals(expectedState, underTest.calcNextStateDeletePhase(jobStatus, internalState)); } @Test public void whenNoNeedToDeleteMyself_internalStateMovesFromWatchingToTerminal() { - MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.WATCHING, Action.Delete, Job.JobStatus.COMPLETED, false); + MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.WATCHING, Delete, Job.JobStatus.COMPLETED, false); assertEquals(InternalState.TERMINAL, underTest.calcNextStateDeletePhase(Job.JobStatus.COMPLETED, InternalState.WATCHING)); } @DataProvider public static Object[][] testShallStopJobDataProvider() { return new Object[][]{ - {Job.JobStatus.IN_PROGRESS, Action.None, false, false}, - {Job.JobStatus.COMPLETED_WITH_NO_ACTION, Action.None, false, false}, - {Job.JobStatus.COMPLETED, Action.None, false, false}, - {Job.JobStatus.FAILED, Action.None, false, true}, - {Job.JobStatus.COMPLETED_WITH_ERRORS, Action.None, false, true}, - {Job.JobStatus.COMPLETED_WITH_ERRORS, Action.None, true, false}, - {Job.JobStatus.FAILED, Action.None, true, false}, - {Job.JobStatus.FAILED, Action.Delete, true, true}, - {Job.JobStatus.FAILED, Action.Create, true, true}, + {Job.JobStatus.IN_PROGRESS, None, false, false}, + {Job.JobStatus.COMPLETED_WITH_NO_ACTION, None, false, false}, + {Job.JobStatus.COMPLETED, None, false, false}, + {Job.JobStatus.FAILED, None, false, true}, + {Job.JobStatus.COMPLETED_WITH_ERRORS, None, false, true}, + {Job.JobStatus.COMPLETED_WITH_ERRORS, None, true, false}, + {Job.JobStatus.FAILED, None, true, false}, + {Job.JobStatus.FAILED, Delete, true, true}, + {Job.JobStatus.FAILED, Create, true, true}, }; } @@ -200,7 +231,7 @@ public class ResourceCommandTest { @Test(dataProvider = "testShallStopJobDataProvider") public void testShallStopJob(Job.JobStatus jobStatus, Action action, boolean isService, boolean expectedResult) { //in this test, there is no meaning to constructor parameters besides isService - MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.WATCHING, Action.Delete, Job.JobStatus.COMPLETED, false, isService); + MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.WATCHING, Delete, Job.JobStatus.COMPLETED, false, isService, true); BaseResource mockedRequest = mock(BaseResource.class); when(underTest.getSharedData().getRequest()).thenReturn(mockedRequest); @@ -209,6 +240,94 @@ public class ResourceCommandTest { assertEquals(expectedResult, underTest.shallStopJob(jobStatus)); } + public static class FakeResourceCreator { + + public static Map convertToMap(List list) { + if (list==null) { + return null; + } + return list.stream().collect(Collectors.toMap(x-> UUID.randomUUID().toString(), x->x)); + } + + static ServiceInstantiation createService(List vnfs, List networks, List vnfGroups) { + return new ServiceInstantiation(mock(ModelInfo.class), null, null, null, null, null, null, null, null, null, null, null, null, null, null, + convertToMap(vnfs), + convertToMap(networks), + convertToMap(vnfGroups), + null, + null, false, 1, false,false,null, null, null, null, null, null, null); + } + + public static ServiceInstantiation createServiceWith2InstancesInEachLevel(Action action) { + return createService( + ImmutableList.of( + createVnf(ImmutableList.of(createVfModule(action), createVfModule(action)), action), + createVnf(ImmutableList.of(createVfModule(action), createVfModule(action)), action)), + ImmutableList.of( + createNetwork(action), + createNetwork(action)), + ImmutableList.of( + createGroup(ImmutableList.of(createMember(action), createMember(action)), action), + createGroup(ImmutableList.of(createMember(action), createMember(action)), action)) + ); + } + + static InstanceGroup createGroup(List groupMembers, Action action) { + return new InstanceGroup(mock(ModelInfo.class), null, action.name(), false, null, convertToMap(groupMembers), null, null, null); + } + + static InstanceGroupMember createMember(Action action) { + return new InstanceGroupMember(null, action.toString(), null, null, null); + } + + static Vnf createVnf(List vfModules, Action action) { + Map> vfModulesMap = new HashMap<>(); + vfModulesMap.put("abc",convertToMap(vfModules)); + + return new Vnf(mock(ModelInfo.class), null, null, action.toString(), null, null, null, null, null, null, false, null, vfModulesMap, null, null, null); + } + + static Vnf createVnf(Action action) { + return new Vnf(mock(ModelInfo.class), null, null, action.toString(), null, null, null, null, null, null, false, null,null, null, null, null); + } + + static VfModule createVfModule(Action action) { + return new VfModule(mock(ModelInfo.class), null, null, action.toString(), null, null, null, null, null, false, false, null, null, null, null); + } + + static Network createNetwork(Action action) { + return new Network(mock(ModelInfo.class), null, null, action.toString(), null, null, null, null, null, null, false, null, null, null, null); + } + } + + @DataProvider + public static Object[][] testIsDescendantHasActionDataProvider() { + return new Object[][]{ + {"empty service", Create, false, createService(emptyList(), emptyList(), emptyList())}, + {"instance group with None", Create, false, createService(emptyList(), emptyList(), ImmutableList.of(createGroup(emptyList(), None)))}, + {"instance group with Create", Create, true, createService(emptyList(), emptyList(), ImmutableList.of(createGroup(emptyList(), Create)))}, + {"instance group None + member Delete", Delete, true, createService(emptyList(), emptyList(), ImmutableList.of(createGroup(ImmutableList.of(createMember(Delete)), None)))}, + {"instance group None + member Create", Delete, false, createService(emptyList(), emptyList(), ImmutableList.of(createGroup(ImmutableList.of(createMember(Create)), None)))}, + {"instance group None + member Create + member Delete", Delete, true, + createService(emptyList(), emptyList(), ImmutableList.of(createGroup(ImmutableList.of(createMember(Create), createMember(Delete)), None)))}, + {"vnf Create", Delete, false, createService(ImmutableList.of(createVnf(emptyList(), Create)), emptyList(),emptyList())}, + {"vnf Create", Create, true, createService(ImmutableList.of(createVnf(emptyList(), Create)), emptyList(),emptyList())}, + {"vnf Create null VfModules internal map", Create, false, createService(ImmutableList.of(createVnf(null, Delete)), emptyList(),emptyList())}, + {"vnf Delete with null VfModules", Create, false, createService(ImmutableList.of(createVnf(Delete)), emptyList(),emptyList())}, + {"vnf None + VfModule Create", Create, true, createService(ImmutableList.of(createVnf(ImmutableList.of(createVfModule(Create)), None)), emptyList(),emptyList())}, + {"vnf None + VfModule None", Create, false, createService(ImmutableList.of(createVnf(ImmutableList.of(createVfModule(None)), None)), emptyList(),emptyList())}, + {"network Create", Create, true, createService(emptyList(), ImmutableList.of(createNetwork(Create)), emptyList())}, + {"network Delete", Create, false, createService(emptyList(), ImmutableList.of(createNetwork(Delete)), emptyList())}, + }; + } + + @Test(dataProvider = "testIsDescendantHasActionDataProvider") + public void testIsDescendantHasAction(String desc, Action action, boolean expectedResult, BaseResource request) { + //in this test, there is no meaning to constructor parameters + MockCommand underTest = new MockCommand(InternalState.WATCHING, Delete, Job.JobStatus.COMPLETED); + assertEquals(expectedResult, underTest.isDescendantHasAction(request, action)); + } + @DataProvider public static Object[][] testCallDataProvider() { return new Object[][]{ @@ -235,7 +354,7 @@ public class ResourceCommandTest { String description, InternalState internalState, Job.JobStatus currentStateResult, InternalState expectedNextState, Job.JobStatus expectedNextStatus) { - MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(internalState, Action.Delete, currentStateResult, true); + MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(internalState, Delete, currentStateResult, true); NextCommand nextCommand = underTest.call(); assertEquals(expectedNextStatus, nextCommand.getStatus()); @@ -250,12 +369,6 @@ public class ResourceCommandTest { } } - @Test(expectedExceptions = IllegalStateException.class) - public void whenCommandInUnMappedState_exceptionIsThrown() { - MockCommandTestingStateMachine underTest = new MockCommandTestingStateMachine(InternalState.TERMINAL, Action.Delete, Job.JobStatus.COMPLETED, true); - underTest.call(); - } - @DataProvider public static Object[][] InProgressDataProvider() { return Stream.of(Job.JobStatus.values()) @@ -266,7 +379,7 @@ public class ResourceCommandTest { @Test(dataProvider = "InProgressDataProvider") public void whenGetResultFromMso_InProgressReturnThem(Job.JobStatus mockedJobStatus) { Job.JobStatus expectedJobStatus = (mockedJobStatus== Job.JobStatus.PAUSE) ? Job.JobStatus.IN_PROGRESS : mockedJobStatus; - MockCommand underTest = new MockCommand(InternalState.IN_PROGRESS, Action.Delete, mockedJobStatus); + MockCommand underTest = new MockCommand(InternalState.IN_PROGRESS, Delete, mockedJobStatus); when(underTest.getInProgressStatusService().call(any(), any(), any())).thenReturn(mockedJobStatus); assertEquals(expectedJobStatus, underTest.inProgress()); } @@ -282,22 +395,22 @@ public class ResourceCommandTest { @Test(dataProvider = "InProgressExceptionsDataProvider") public void whenInProgressStatusServiceThrowException_InProgressReturnStatus(Exception exception, Job.JobStatus expectedJobStatus) { - MockCommand underTest = new MockCommand(InternalState.IN_PROGRESS, Action.Delete, expectedJobStatus); + MockCommand underTest = new MockCommand(InternalState.IN_PROGRESS, Delete, expectedJobStatus); when(underTest.getInProgressStatusService().call(any(), any(), any())).thenThrow(exception); assertEquals(expectedJobStatus, underTest.inProgress()); } @DataProvider public static Object[][] testIsNeedToDeleteMySelfDataProvider() { - return Stream.of(Action.values()) + return Stream.of(values()) .map(status -> new Object[] { status }) .toArray(Object[][]::new); } @Test(dataProvider = "testIsNeedToDeleteMySelfDataProvider") public void testIsNeedToDeleteMySelf(Action action) { - boolean expectedResult = (action== Action.Delete); - MockCommand underTest = new MockCommand(InternalState.DELETE_MYSELF, Action.Delete, Job.JobStatus.IN_PROGRESS); + boolean expectedResult = (action== Delete); + MockCommand underTest = new MockCommand(InternalState.DELETE_MYSELF, Delete, Job.JobStatus.IN_PROGRESS); BaseResource mockedBaseResource = mock(BaseResource.class); when(underTest.getSharedData().getRequest()).thenReturn(mockedBaseResource); when(mockedBaseResource.getAction()).thenReturn(action); @@ -315,9 +428,74 @@ public class ResourceCommandTest { @Test(dataProvider = "testWatchingDataProvider") public void testWatching(String desc, Job.JobStatus childrenJobsStatus, Job.JobStatus expectedJobStatus) { - MockCommand underTest = new MockCommand(InternalState.WATCHING, Action.Delete, Job.JobStatus.IN_PROGRESS); + MockCommand underTest = new MockCommand(InternalState.WATCHING, Delete, Job.JobStatus.IN_PROGRESS); when(underTest.getWatchChildrenJobsBL().retrieveChildrenJobsStatus(any())).thenReturn(childrenJobsStatus); assertEquals(expectedJobStatus, underTest.watchChildren()); } + @DataProvider + public static Object[][] testCalcInitialStateDataProvider() { + return new Object[][]{ + {Delete, true, Delete, InternalState.CREATING_CHILDREN}, + {Delete, false, Delete, InternalState.DELETE_MYSELF}, + {Delete, false, Create, InternalState.TERMINAL}, + {Delete, true, Create, InternalState.CREATING_CHILDREN}, + {Create, true, Create, InternalState.CREATE_MYSELF}, + {Create, false, Create, InternalState.CREATE_MYSELF}, + {Create, false, Delete, InternalState.TERMINAL}, + {Create, true, Delete, InternalState.CREATING_CHILDREN}, + {Create, true, Resume, InternalState.RESUME_MYSELF}, + {Delete, false, Resume, InternalState.TERMINAL}, + }; + } + + @Test(dataProvider = "testCalcInitialStateDataProvider") + public void testCalcInitialState(Action phase, boolean isDescendantHasAction, Action action, InternalState expectedState) { + ResourceCommand underTest = mock(ResourceCommand.class); + when(underTest.calcInitialState(any(), any())).thenCallRealMethod(); + when(underTest.isDescendantHasAction(eq(phase))).thenReturn(isDescendantHasAction); + when(underTest.getActionType()).thenReturn(action); + when(underTest.isNeedToDeleteMyself()).thenCallRealMethod(); + when(underTest.isNeedToCreateMyself()).thenCallRealMethod(); + when(underTest.isNeedToResumeMySelf()).thenCallRealMethod(); + + Map commandData = ImmutableMap.of(INTERNAL_STATE, InternalState.INITIAL.name()); + assertEquals(expectedState, underTest.calcInitialState(commandData, phase)); + } + + + //throw exception when call to create children + //create children is just example, it could be any other method that called by ResourceCommand.invokeCommand + public static class MockCommandThrowExceptionOnCreateChildren extends MockCommandTestingStateMachine { + + private final RuntimeException exceptionToThrow; + + public MockCommandThrowExceptionOnCreateChildren(RuntimeException exceptionToThrow) { + super(InternalState.CREATING_CHILDREN, Delete, Job.JobStatus.COMPLETED, true); + this.exceptionToThrow = exceptionToThrow; + doAnswer(returnsFirstArg()).when(this.getWatchChildrenJobsBL()).cumulateJobStatus(any(), any()); + } + + @NotNull + @Override + public Job.JobStatus createChildren() { + throw exceptionToThrow; + } + } + + @DataProvider + public static Object[][] exceptionAndStateProvider() { + return new Object[][]{ + {new TryAgainException(new Exception()), Job.JobStatus.RESOURCE_IN_PROGRESS}, + {new AbortingException(new Exception()), Job.JobStatus.FAILED}, + }; + } + + @Test(dataProvider = "exceptionAndStateProvider") + public void whenKnownExceptionThrownInCommandInvocation_thenStateIsAsExpected(RuntimeException exception, Job.JobStatus expectedNextStatus) { + MockCommandTestingStateMachine underTest = new MockCommandThrowExceptionOnCreateChildren(exception); + NextCommand nextCommand = underTest.call(); + assertEquals(expectedNextStatus, nextCommand.getStatus()); + } + } -- cgit 1.2.3-korg