diff options
author | Einat Vinouze <einat.vinouze@intl.att.com> | 2019-09-25 16:06:48 +0300 |
---|---|---|
committer | Einat Vinouze <einat.vinouze@intl.att.com> | 2019-10-03 14:52:55 +0300 |
commit | 9131e3944d1cf571b6a6d57f402a30d8468237a3 (patch) | |
tree | fb3e0a848fbaf321837660bb88cce78a94407d18 | |
parent | 579e5fa551a9563c5747c6e73a479f81ab947f53 (diff) |
sort base resource according it's position field
Issue-ID: VID-646
Signed-off-by: Einat Vinouze <einat.vinouze@intl.att.com>
Change-Id: I35705cd4a239e30bc6d86da7cde05495648d2a04
Signed-off-by: Einat Vinouze <einat.vinouze@intl.att.com>
4 files changed, 166 insertions, 52 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/job/command/ResourceCommand.kt b/vid-app-common/src/main/java/org/onap/vid/job/command/ResourceCommand.kt index ac5c2751c..df97f89f8 100644 --- a/vid-app-common/src/main/java/org/onap/vid/job/command/ResourceCommand.kt +++ b/vid-app-common/src/main/java/org/onap/vid/job/command/ResourceCommand.kt @@ -427,14 +427,17 @@ abstract class ResourceCommand( protected fun pushChildrenJobsToBroker(children:Collection<BaseResource>, dataForChild: Map<String, Any>, jobType: JobType?=null): List<String> { - var counter = 0; - return children - .map {Pair(it, counter++)} + return setPositionWhereIsMissing(children) .map { jobAdapter.createChildJob(jobType ?: it.first.jobType, it.first, sharedData, dataForChild, it.second) } .map { jobsBrokerService.add(it) } .map { it.toString() } } + protected fun setPositionWhereIsMissing(children: Collection<BaseResource>): List<Pair<BaseResource, Int>> { + var orderingPosition = children.map{ defaultIfNull(it.position, 0) }.max() ?: 0 + return children + .map {Pair(it, it.position ?: ++orderingPosition)} + } } 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 9501614b7..986f5d0f5 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 @@ -20,8 +20,45 @@ package org.onap.vid.job.command; +import static java.util.Collections.emptyList; +import static org.mockito.AdditionalAnswers.returnsFirstArg; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.RETURNS_MOCKS; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +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.createGroup; +import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.createMember; +import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.createNetwork; +import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.createService; +import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.createVfModule; +import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.createVnf; +import static org.onap.vid.model.Action.Create; +import static org.onap.vid.model.Action.Delete; +import static org.onap.vid.model.Action.None; +import static org.onap.vid.model.Action.Resume; +import static org.onap.vid.model.Action.values; +import static org.onap.vid.utils.Logging.getMethodCallerName; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.ws.rs.ProcessingException; +import kotlin.Pair; import org.jetbrains.annotations.NotNull; import org.onap.vid.exceptions.AbortingException; import org.onap.vid.exceptions.GenericUncheckedException; @@ -32,32 +69,19 @@ 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.*; +import org.onap.vid.model.serviceInstantiation.BaseResource; +import org.onap.vid.model.serviceInstantiation.InstanceGroup; +import org.onap.vid.model.serviceInstantiation.InstanceGroupMember; +import org.onap.vid.model.serviceInstantiation.Network; +import org.onap.vid.model.serviceInstantiation.ServiceInstantiation; +import org.onap.vid.model.serviceInstantiation.VfModule; +import org.onap.vid.model.serviceInstantiation.Vnf; 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.*; -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; -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertFalse; - public class ResourceCommandTest { public static class MockCommand extends ResourceCommand { @@ -503,4 +527,30 @@ public class ResourceCommandTest { assertEquals(expectedNextStatus, nextCommand.getStatus()); } + @DataProvider + public static Object[][] resourcePosition() { + return new Object[][]{ + {1, 2, 3, ImmutableList.of(1,2,3)}, + {null, 1, 100, ImmutableList.of(101,1,100)}, + {null, null, null, ImmutableList.of(1,2,3)}, + {1,2,2, ImmutableList.of(1,2,2)} + }; + } + + @Test(dataProvider = "resourcePosition") + public void sortChildren_sortAccordingToPosition(Integer firstPosition, Integer secondPosition, Integer thirdPosition, List<Integer> expectedPositions){ + BaseResource mockedRequest1 = mock(BaseResource.class); + when(mockedRequest1.getPosition()).thenReturn(firstPosition); + BaseResource mockedRequest2 = mock(BaseResource.class); + when(mockedRequest2.getPosition()).thenReturn(secondPosition); + BaseResource mockedRequest3 = mock(BaseResource.class); + when(mockedRequest3.getPosition()).thenReturn(thirdPosition); + + MockCommand underTest = new MockCommand(InternalState.CREATING_CHILDREN, Create, Job.JobStatus.IN_PROGRESS); + List<Pair<BaseResource, Integer>> sortedList = underTest.setPositionWhereIsMissing(ImmutableList.of(mockedRequest1, mockedRequest2, mockedRequest3)); + + assertEquals(sortedList.get(0).getSecond(),expectedPositions.get(0)); + assertEquals(sortedList.get(1).getSecond(),expectedPositions.get(1)); + assertEquals(sortedList.get(2).getSecond(),expectedPositions.get(2)); + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/job/command/ServiceInProgressStatusCommandTest.java b/vid-app-common/src/test/java/org/onap/vid/job/command/ServiceInProgressStatusCommandTest.java index 787ff608d..992325049 100644 --- a/vid-app-common/src/test/java/org/onap/vid/job/command/ServiceInProgressStatusCommandTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/job/command/ServiceInProgressStatusCommandTest.java @@ -143,9 +143,14 @@ public class ServiceInProgressStatusCommandTest { UUID uuid = UUID.randomUUID(); String userId = "mockedUserID"; String testApi = "VNF_API"; + + // Create components setPosition in order to verify on the creation order on createChildJob + Network network = createNetwork(Create); + network.setPosition(0); Vnf vnf1 = createVnf(emptyList(), Create); + vnf1.setPosition(1); Vnf vnf2 = createVnf(emptyList(), Create); - Network network = createNetwork(Create); + vnf2.setPosition(2); ServiceInstantiation serviceInstantiation = createService( ImmutableList.of(vnf1, vnf2), ImmutableList.of(network), diff --git a/vid-app-common/src/test/java/org/onap/vid/services/JobsBrokerServiceTest.java b/vid-app-common/src/test/java/org/onap/vid/services/JobsBrokerServiceTest.java index 40546e967..20c9d1443 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/JobsBrokerServiceTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/JobsBrokerServiceTest.java @@ -21,8 +21,52 @@ package org.onap.vid.services; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.stream.Collectors.toList; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.mockito.Mockito.when; +import static org.onap.vid.job.Job.JobStatus.COMPLETED; +import static org.onap.vid.job.Job.JobStatus.COMPLETED_WITH_ERRORS; +import static org.onap.vid.job.Job.JobStatus.COMPLETED_WITH_NO_ACTION; +import static org.onap.vid.job.Job.JobStatus.CREATING; +import static org.onap.vid.job.Job.JobStatus.FAILED; +import static org.onap.vid.job.Job.JobStatus.IN_PROGRESS; +import static org.onap.vid.job.Job.JobStatus.PAUSE; +import static org.onap.vid.job.Job.JobStatus.PENDING; +import static org.onap.vid.job.Job.JobStatus.PENDING_RESOURCE; +import static org.onap.vid.job.Job.JobStatus.RESOURCE_IN_PROGRESS; +import static org.onap.vid.job.Job.JobStatus.STOPPED; +import static org.onap.vid.testUtils.TestUtils.generateRandomAlphaNumeric; +import static org.onap.vid.utils.Streams.not; +import static org.testng.Assert.assertNotNull; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import java.lang.reflect.Method; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import javax.inject.Inject; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; @@ -37,20 +81,21 @@ import org.mockito.MockitoAnnotations; import org.onap.portalsdk.core.domain.support.DomainVo; import org.onap.portalsdk.core.service.DataAccessService; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.config.DataSourceConfig; +import org.onap.vid.config.JobAdapterConfig; import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.vid.exceptions.OperationNotAllowedException; import org.onap.vid.job.Job; import org.onap.vid.job.JobAdapter; +import org.onap.vid.job.JobAdapter.AsyncJobRequest; import org.onap.vid.job.JobType; import org.onap.vid.job.JobsBrokerService; import org.onap.vid.job.command.JobCommandFactoryTest; import org.onap.vid.job.impl.JobDaoImpl; import org.onap.vid.job.impl.JobSchedulerInitializer; +import org.onap.vid.job.impl.JobSharedData; import org.onap.vid.job.impl.JobsBrokerServiceInDatabaseImpl; -import org.onap.vid.services.VersionService; import org.onap.vid.utils.DaoUtils; -import org.onap.vid.config.DataSourceConfig; -import org.onap.vid.config.JobAdapterConfig; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.Assert; @@ -59,31 +104,6 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import javax.inject.Inject; -import java.lang.reflect.Method; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.*; -import java.util.concurrent.*; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; - -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.stream.Collectors.toList; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.both; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.mockito.Mockito.when; -import static org.onap.vid.job.Job.JobStatus.*; -import static org.onap.vid.utils.Streams.not; -import static org.onap.vid.testUtils.TestUtils.generateRandomAlphaNumeric; -import static org.testng.Assert.assertNotNull; -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertFalse; - @ContextConfiguration(classes = {DataSourceConfig.class, SystemProperties.class, JobAdapterConfig.class}) public class JobsBrokerServiceTest extends AbstractTestNGSpringContextTests { @@ -944,4 +964,40 @@ public class JobsBrokerServiceTest extends AbstractTestNGSpringContextTests { broker.delete(new UUID(111, 111)); } + public static class MockAsyncRequest implements AsyncJobRequest { + public String value; + + public MockAsyncRequest() {} + + public MockAsyncRequest(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + + @Test + public void twoJobsWithSamePosition_bothJobsArePulled(){ + UUID uuid = UUID.randomUUID(); + int positionInBulk = RandomUtils.nextInt(); + String userId = "userId"; + + Optional<Job> firstPulledJob = createAddAndPullJob(uuid, positionInBulk, userId, "first value"); + Optional<Job> secondPulledJob = createAddAndPullJob(uuid, positionInBulk, userId, "second value"); + + MockAsyncRequest firstValue = (MockAsyncRequest) firstPulledJob.get().getSharedData().getRequest(); + MockAsyncRequest secondValue = (MockAsyncRequest) secondPulledJob.get().getSharedData().getRequest(); + assertThat(ImmutableList.of(firstValue.value, secondValue.value), + containsInAnyOrder("first value", "second value")); + } + + private Optional<Job> createAddAndPullJob(UUID uuid, int positionInBulk, String userId, String s) { + JobDaoImpl job1 = createNewJob(positionInBulk, uuid, userId, CREATING, null, + LocalDateTime.now().minusSeconds(1), false); + job1.setSharedData(new JobSharedData(null, userId, new MockAsyncRequest(s), "testApi")); + broker.add(job1); + return broker.pull(CREATING, userId); + } } |