diff options
Diffstat (limited to 'vid-app-common/src/test/java')
4 files changed, 250 insertions, 55 deletions
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/mso/rest/OutgoingRequestHeadersTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java index 414bcf4ac..422443510 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java @@ -20,6 +20,7 @@ package org.onap.vid.mso.rest; +import static org.apache.commons.io.IOUtils.toInputStream; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; import static org.hamcrest.CoreMatchers.is; @@ -32,9 +33,18 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.matchesPattern; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableList; +import io.joshworks.restclient.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -53,10 +63,12 @@ import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.aai.util.ServletRequestHelper; import org.onap.vid.aai.util.SystemPropertyHelper; +import org.onap.vid.client.SyncRestClient; import org.onap.vid.controller.filter.PromiseRequestIdFilter; import org.onap.vid.logging.Headers; import org.onap.vid.mso.MsoProperties; @@ -82,6 +94,8 @@ public class OutgoingRequestHeadersTest { @InjectMocks private RestMsoImplementation restMsoImplementation; + private MsoRestClientNew msoRestClientNew; + @Mock private SystemPropertyHelper systemPropertyHelper; @@ -97,6 +111,9 @@ public class OutgoingRequestHeadersTest { @Mock private Logging loggingService; + @Mock + SyncRestClient syncRestClient; + @InjectMocks private AAIRestInterface aaiRestInterface; @@ -108,6 +125,9 @@ public class OutgoingRequestHeadersTest { MockitoAnnotations.initMocks(this); when(servletRequestHelper.extractOrGenerateRequestId()).thenAnswer(invocation -> UUID.randomUUID().toString()); when(systemPropertiesWrapper.getProperty(MsoProperties.MSO_PASSWORD)).thenReturn("OBF:1vub1ua51uh81ugi1u9d1vuz"); + when(systemPropertiesWrapper.getProperty(SystemProperties.APP_DISPLAY_NAME)).thenReturn("vid"); + //the ctor of MsoRestClientNew require the above lines as preconditions + msoRestClientNew = new MsoRestClientNew(syncRestClient, "baseUrl",systemPropertiesWrapper); } @BeforeMethod @@ -136,9 +156,9 @@ public class OutgoingRequestHeadersTest { f.accept(restMsoImplementation); Invocation.Builder fakeBuilder = mocks.getFakeBuilder(); - Object requestIdValue = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder); + String requestIdValue = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder); assertEquals(requestIdValue, captureHeaderKeyAndReturnItsValue(fakeBuilder, "X-ONAP-RequestID")); - Object invocationId1 = assertRequestHeaderIsUUID(fakeBuilder, "X-InvocationID"); + String invocationId1 = assertRequestHeaderIsUUID(fakeBuilder, "X-InvocationID"); assertThat((String) captureHeaderKeyAndReturnItsValue(fakeBuilder, "Authorization"), startsWith("Basic ")); verifyXOnapPartnerNameHeaderWasAdded(fakeBuilder); @@ -152,7 +172,7 @@ public class OutgoingRequestHeadersTest { Invocation.Builder fakeBuilder2 = mocks2.getFakeBuilder(); //then - Object requestIdValue2 = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder2); + String requestIdValue2 = verifyXEcompRequestIdHeaderWasAdded(fakeBuilder2); assertEquals(requestIdValue, requestIdValue2); Object invocationId2 = assertRequestHeaderIsUUID(fakeBuilder2, "X-InvocationID"); @@ -170,6 +190,62 @@ public class OutgoingRequestHeadersTest { } @DataProvider + public Object[][] msoRestClientNewMethods() { + return Stream.<ThrowingConsumer<MsoRestClientNew>>of( + client -> client.createInstance(new Object(), "/any path") + ).map(l -> ImmutableList.of(l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{}); + } + + @Test(dataProvider = "msoRestClientNewMethods") + public void msoRestClientNewHeadersTest(Consumer<MsoRestClientNew> f) throws Exception { + Map[] captor = setMocksForMsoRestClientNew(); + + f.accept(msoRestClientNew); + Map headers = captor[0]; + + String ecompRequestId = assertRequestHeaderIsUUID(headers, "X-ECOMP-RequestID"); + String onapRequestID = assertRequestHeaderIsUUID(headers, "X-ONAP-RequestID"); + assertEquals(ecompRequestId, onapRequestID); + + + String invocationId1 = assertRequestHeaderIsUUID(headers, "X-InvocationID"); + assertThat((String) headers.get("Authorization"), startsWith("Basic ")); + assertThat(headers.get("X-ONAP-PartnerName"), is("VID.VID")); + + //validate requestId is same in next call but invocationId is different + + //given + captor = setMocksForMsoRestClientNew(); + + //when + f.accept(msoRestClientNew); + headers = captor[0]; + + //then + assertEquals(headers.get("X-ONAP-RequestID"), onapRequestID); + String invocationId2 = assertRequestHeaderIsUUID(headers, "X-InvocationID"); + assertNotEquals(invocationId1, invocationId2); + + } + + private Map[] setMocksForMsoRestClientNew() { + reset(syncRestClient); + HttpResponse<String> httpResponse = mock(HttpResponse.class); + String expectedResponse = "myResponse"; + when(httpResponse.getStatus()).thenReturn(202); + when(httpResponse.getBody()).thenReturn(expectedResponse); + when(httpResponse.getRawBody()).thenReturn(toInputStream(expectedResponse, StandardCharsets.UTF_8)); + final Map[] headersCapture = new Map[1]; + when(syncRestClient.post(anyString(), anyMap(), any(), eq(String.class))).thenAnswer( + invocation -> { + headersCapture[0] = (Map)invocation.getArguments()[1]; + return httpResponse; + }); + + return headersCapture; + } + + @DataProvider public Object[][] aaiMethods() { return Stream.<ThrowingConsumer<AAIRestInterface>>of( @@ -201,17 +277,25 @@ public class OutgoingRequestHeadersTest { // // } - private Object verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) { + private String verifyXEcompRequestIdHeaderWasAdded(Invocation.Builder fakeBuilder) { final String requestIdHeader = "x-ecomp-requestid"; return assertRequestHeaderIsUUID(fakeBuilder, requestIdHeader); } - private Object assertRequestHeaderIsUUID(Invocation.Builder fakeBuilder, String headerName) { + private String assertRequestHeaderIsUUID(Invocation.Builder fakeBuilder, String headerName) { Object headerValue = captureHeaderKeyAndReturnItsValue(fakeBuilder, headerName); + return assertRequestHeaderIsUUID(headerName, headerValue); + } + + private String assertRequestHeaderIsUUID(Map headers, String headerName) { + return assertRequestHeaderIsUUID(headerName, headers.get(headerName)); + } + + private String assertRequestHeaderIsUUID(String headerName, Object headerValue) { final String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; assertThat("header '" + headerName + "' should be a uuid", headerValue, allOf(instanceOf(String.class), hasToString(matchesPattern(uuidRegex)))); - return headerValue; + return (String)headerValue; } private void verifyXOnapPartnerNameHeaderWasAdded(Invocation.Builder fakeBuilder) { 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); + } } |