Search in sources :

Example 6 with Work

use of com.redhat.service.bridge.manager.models.Work in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class WorkManagerImplTest method processWorkQueue.

@Test
void processWorkQueue() {
    Work work1 = new Work();
    work1.setType("Type1");
    Work work2 = new Work();
    work2.setType("Type2");
    when(workDAO.findByWorkerId(anyString())).thenReturn(List.of(work1, work2));
    manager.processWorkQueue();
    verify(eventBus).requestAndForget("Type1", work1);
    verify(eventBus).requestAndForget("Type2", work2);
}
Also used : Work(com.redhat.service.bridge.manager.models.Work) Test(org.junit.jupiter.api.Test)

Example 7 with Work

use of com.redhat.service.bridge.manager.models.Work in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class WorkManagerImplTest method scheduleDoesNotFireEventForExistingWork.

@Test
void scheduleDoesNotFireEventForExistingWork() {
    Work existing = mock(Work.class);
    when(workDAO.findByManagedResource(resource)).thenReturn(existing);
    manager.schedule(resource);
    verify(eventBus, never()).requestAndForget(anyString(), any(Work.class));
}
Also used : Work(com.redhat.service.bridge.manager.models.Work) Test(org.junit.jupiter.api.Test)

Example 8 with Work

use of com.redhat.service.bridge.manager.models.Work in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ConnectorWorkerTest method handleWorkProvisioningWithKnownResourceMultiplePasses.

@ParameterizedTest
@EnumSource(value = ManagedResourceStatus.class, names = { "ACCEPTED", "PROVISIONING" })
void handleWorkProvisioningWithKnownResourceMultiplePasses(ManagedResourceStatus status) {
    Work work = new Work();
    work.setManagedResourceId(RESOURCE_ID);
    work.setSubmittedAt(ZonedDateTime.now());
    ConnectorEntity connectorEntity = spy(new ConnectorEntity());
    connectorEntity.setStatus(status);
    Connector connector = new Connector();
    connector.setStatus(new ConnectorStatusStatus().state(ConnectorState.READY));
    when(connectorsDAO.findById(RESOURCE_ID)).thenReturn(connectorEntity);
    when(connectorsDAO.getEntityManager()).thenReturn(entityManager);
    when(entityManager.merge(connectorEntity)).thenReturn(connectorEntity);
    // Managed Connector will not be available immediately
    when(connectorsApi.getConnector(connectorEntity)).thenReturn(null, connector);
    worker.handleWork(work);
    verify(rhoasService).createTopicAndGrantAccessFor(connectorEntity.getTopicName(), RhoasTopicAccessType.PRODUCER);
    verify(connectorsApi).createConnector(connectorEntity);
    assertThat(connectorEntity.getStatus()).isEqualTo(ManagedResourceStatus.PROVISIONING);
    assertThat(connectorEntity.getDependencyStatus()).isEqualTo(ManagedResourceStatus.PROVISIONING);
    // This emulates a subsequent invocation by WorkManager
    worker.handleWork(work);
    verify(rhoasService, times(2)).createTopicAndGrantAccessFor(connectorEntity.getTopicName(), RhoasTopicAccessType.PRODUCER);
    verify(connectorsApi, atMostOnce()).createConnector(connectorEntity);
    assertThat(connectorEntity.getPublishedAt()).isNotNull();
    assertThat(connectorEntity.getStatus()).isEqualTo(ManagedResourceStatus.READY);
    assertThat(connectorEntity.getDependencyStatus()).isEqualTo(ManagedResourceStatus.READY);
    verify(workManager, never()).complete(work);
    verify(connectorEntity, atLeastOnce()).setModifiedAt(any(ZonedDateTime.class));
}
Also used : Connector(com.openshift.cloud.api.connector.models.Connector) ZonedDateTime(java.time.ZonedDateTime) Work(com.redhat.service.bridge.manager.models.Work) ConnectorEntity(com.redhat.service.bridge.manager.models.ConnectorEntity) ConnectorStatusStatus(com.openshift.cloud.api.connector.models.ConnectorStatusStatus) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with Work

use of com.redhat.service.bridge.manager.models.Work in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorWorkerTest method handleWorkProvisioningWithKnownResourceWithConnector.

@ParameterizedTest
@MethodSource("srcHandleWorkProvisioningWithKnownResourceWithConnector")
void handleWorkProvisioningWithKnownResourceWithConnector(ManagedResourceStatus status, ManagedResourceStatus connectorStatusWhenComplete, boolean isWorkComplete) {
    Work work = new Work();
    work.setManagedResourceId(RESOURCE_ID);
    work.setSubmittedAt(ZonedDateTime.now());
    Processor processor = new Processor();
    processor.setStatus(status);
    ConnectorEntity connector = new ConnectorEntity();
    connector.setStatus(ManagedResourceStatus.ACCEPTED);
    when(processorDAO.findById(RESOURCE_ID)).thenReturn(processor);
    when(processorDAO.getEntityManager()).thenReturn(entityManager);
    when(entityManager.merge(processor)).thenReturn(processor);
    when(connectorsDAO.findByProcessorId(processor.getId())).thenReturn(connector);
    doAnswer((i) -> {
        // Emulate ConnectorWorker completing work
        connector.setStatus(connectorStatusWhenComplete);
        return connector;
    }).when(connectorWorker).handleWork(any(Work.class));
    worker.handleWork(work);
    assertThat(processor.getDependencyStatus()).isEqualTo(connectorStatusWhenComplete);
    verify(connectorWorker).handleWork(workArgumentCaptor.capture());
    Work connectorWork = workArgumentCaptor.getValue();
    assertThat(connectorWork).isNotNull();
    assertThat(connectorWork.getId()).isEqualTo(work.getId());
    assertThat(connectorWork.getManagedResourceId()).isEqualTo(connector.getId());
    assertThat(connectorWork.getSubmittedAt()).isEqualTo(work.getSubmittedAt());
    verify(workManager, times(isWorkComplete ? 1 : 0)).complete(any(Work.class));
}
Also used : Processor(com.redhat.service.bridge.manager.models.Processor) Work(com.redhat.service.bridge.manager.models.Work) ConnectorEntity(com.redhat.service.bridge.manager.models.ConnectorEntity) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with Work

use of com.redhat.service.bridge.manager.models.Work in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class WorkManagerImplTest method scheduleFiresEventForNewWork.

@Test
void scheduleFiresEventForNewWork() {
    when(resource.getId()).thenReturn(RESOURCE_ID);
    when(workDAO.findByManagedResource(resource)).thenReturn(null);
    manager.schedule(resource);
    verify(workDAO).persist(workArgumentCaptor.capture());
    Work work = workArgumentCaptor.getValue();
    assertThat(work).isNotNull();
    assertThat(work.getType()).contains(Processor.class.getName());
    assertThat(work.getManagedResourceId()).isEqualTo(RESOURCE_ID);
    verify(eventBus).requestAndForget(anyString(), eq(work));
}
Also used : Processor(com.redhat.service.bridge.manager.models.Processor) Work(com.redhat.service.bridge.manager.models.Work) Test(org.junit.jupiter.api.Test)

Aggregations

Work (com.redhat.service.bridge.manager.models.Work)18 Test (org.junit.jupiter.api.Test)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 ConnectorEntity (com.redhat.service.bridge.manager.models.ConnectorEntity)5 Processor (com.redhat.service.bridge.manager.models.Processor)5 ZonedDateTime (java.time.ZonedDateTime)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 Connector (com.openshift.cloud.api.connector.models.Connector)2 ConnectorStatusStatus (com.openshift.cloud.api.connector.models.ConnectorStatusStatus)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Transactional (javax.transaction.Transactional)1