Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAGO-59386: Fix NPE when calling api/v2/ema/scan REST API #111

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import com.solace.maas.ep.event.management.agent.repository.model.scan.ScanEntity;
import com.solace.maas.ep.event.management.agent.repository.model.scan.ScanRecipientEntity;
import com.solace.maas.ep.event.management.agent.repository.model.scan.ScanRecipientHierarchyEntity;
import com.solace.maas.ep.event.management.agent.repository.model.scan.ScanStatusEntity;
import com.solace.maas.ep.event.management.agent.repository.model.scan.ScanTypeEntity;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanRecipientHierarchyRepository;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanRepository;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanTypeRepository;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanStatusRepository;
import com.solace.maas.ep.event.management.agent.scanManager.model.ScanItemBO;
import com.solace.maas.ep.event.management.agent.scanManager.model.ScanTypeBO;
import com.solace.maas.ep.event.management.agent.util.IDGenerator;
Expand Down Expand Up @@ -50,6 +52,8 @@ public class ScanService {

private final ScanTypeRepository scanTypeRepository;

private final ScanStatusRepository scanStatusRepository;

private final ScanRouteService scanRouteService;

private final RouteService routeService;
Expand All @@ -60,12 +64,13 @@ public class ScanService {

public ScanService(ScanRepository repository,
ScanRecipientHierarchyRepository scanRecipientHierarchyRepository,
ScanTypeRepository scanTypeRepository, ScanRouteService scanRouteService,
ScanTypeRepository scanTypeRepository, ScanStatusRepository scanStatusRepository, ScanRouteService scanRouteService,
RouteService routeService, ProducerTemplate producerTemplate,
IDGenerator idGenerator) {
this.repository = repository;
this.scanRecipientHierarchyRepository = scanRecipientHierarchyRepository;
this.scanTypeRepository = scanTypeRepository;
this.scanStatusRepository = scanStatusRepository;
this.scanRouteService = scanRouteService;
this.routeService = routeService;
this.producerTemplate = producerTemplate;
Expand Down Expand Up @@ -219,15 +224,29 @@ private ScanEntity saveScanEntity(String scanId, String traceId, MessagingServic
}

private void setScanType(RouteBundle routeBundle, ScanEntity scanEntity) {


ScanTypeEntity scanType = ScanTypeEntity.builder()
.id(idGenerator.generateRandomUniqueId())
.name(routeBundle.getScanType())
.scan(scanEntity)
.build();

scanTypeRepository.save(scanType);

setScanStatus(scanType);
}

private void setScanStatus(ScanTypeEntity scanType) {
ScanStatusEntity scanStatus = ScanStatusEntity.builder()
.id(idGenerator.generateRandomUniqueId())
.status(ScanStatus.INITIATED.name())
.scanType(scanType)
.build();

scanStatusRepository.save(scanStatus);

}
protected void setupRecipientsForScan(ScanEntity scanEntity, RouteBundle routeBundle) {

for (RouteBundle recipient : routeBundle.getRecipients()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.solace.maas.ep.event.management.agent.repository.model.scan.ScanTypeEntity;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanRecipientHierarchyRepository;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanRepository;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanStatusRepository;
import com.solace.maas.ep.event.management.agent.repository.scan.ScanTypeRepository;
import com.solace.maas.ep.event.management.agent.service.logging.LoggingService;
import com.solace.maas.ep.event.management.agent.util.IDGenerator;
Expand All @@ -23,6 +24,7 @@
import org.apache.camel.ProducerTemplate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -41,9 +43,12 @@
import java.util.concurrent.CompletableFuture;

import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

@ActiveProfiles("TEST")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfig.class)
Expand All @@ -63,6 +68,9 @@ public class ScanServiceTests {
@Mock
private ScanTypeRepository scanTypeRepository;

@Mock
private ScanStatusRepository scanStatusRepository;

@Mock
private ScanRecipientHierarchyRepository scanRecipientHierarchyRepository;

Expand Down Expand Up @@ -115,6 +123,7 @@ public void testSingleScanWithRouteBundle() {
RouteEntity returnedEntity = scanServiceHelper.buildRouteEntity(routeId, "", true);
ScanEntity scanEntity = scanServiceHelper.buildScanEntity("123", "emaId1", List.of(returnedEntity), null);
ScanTypeEntity scanType = scanServiceHelper.buildScanTypeEntity("scan1", "scanType", scanEntity, null);
ScanStatusEntity scanStatus = scanServiceHelper.buildScanStatusEntity("scanstatus1", "dummyStatus");

when(idGenerator.generateRandomUniqueId())
.thenReturn("abc123");
Expand All @@ -132,6 +141,9 @@ public void testSingleScanWithRouteBundle() {
.thenReturn(scanEntity);
when(scanTypeRepository.save(scanType))
.thenReturn(scanType);
when(scanStatusRepository.save(scanStatus))
.thenReturn(scanStatus);

when(scanRecipientHierarchyRepository.save(any(ScanRecipientHierarchyEntity.class)))
.thenReturn(mock(ScanRecipientHierarchyEntity.class));

Expand All @@ -142,6 +154,12 @@ public void testSingleScanWithRouteBundle() {
mock(MessagingServiceEntity.class),
"runtimeAgent1");

ArgumentCaptor<ScanStatusEntity> scanCaptor = ArgumentCaptor.forClass(ScanStatusEntity.class);
verify(scanStatusRepository, times(8)).save(scanCaptor.capture());

// Assert that the scanStatus is initialized to "INITIATED" for all scan types
scanCaptor.getAllValues().stream().forEach(entity -> assertThat(entity.getStatus().equals(ScanStatus.INITIATED.name())));

assertThatNoException();
}

Expand Down Expand Up @@ -293,7 +311,7 @@ public void testParseRouteRecipients() {
public void testSendScanStatus() {
ScanService service = new ScanService(mock(ScanRepository.class), mock(ScanRecipientHierarchyRepository.class),
mock(ScanTypeRepository.class),
mock(ScanRouteService.class), mock(RouteService.class), template, idGenerator);
mock(ScanStatusRepository.class), mock(ScanRouteService.class), mock(RouteService.class), template, idGenerator);
service.sendScanStatus("scanId", "groupId", "messagingServiceId", "traceId",
"queueListing", ScanStatus.IN_PROGRESS);

Expand Down
Loading