Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from dcsaorg/DDT-1383
Browse files Browse the repository at this point in the history
DDT-1383 - add carrierServiceCode + universalServiceReference as opti…
  • Loading branch information
FinnNielsen authored Dec 16, 2022
2 parents 7c51610 + e3165ff commit 656bae0
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface VoyageRepository extends JpaRepository<Voyage, UUID> {
List<Voyage> findByUniversalVoyageReference(String universalVoyageReference);
List<Voyage> findByCarrierVoyageNumber(String carrierVoyageNumber);
}
public interface VoyageRepository extends JpaRepository<Voyage, UUID> { }
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package org.dcsa.edocumentation.service;

import lombok.AllArgsConstructor;
import org.dcsa.edocumentation.domain.persistence.entity.Service;
import org.dcsa.edocumentation.domain.persistence.repository.ServiceRepository;
import org.dcsa.edocumentation.service.mapping.ServiceMapper;
import org.dcsa.edocumentation.transferobjects.ServiceTO;
import org.springframework.stereotype.Service;
import org.dcsa.edocumentation.transferobjects.BookingTO;
import org.dcsa.skernel.errors.exceptions.ConcreteRequestErrorMessageException;
import org.springframework.data.domain.Example;

import javax.transaction.Transactional;
import java.util.List;

@Service
@org.springframework.stereotype.Service
@AllArgsConstructor
public class ServiceService {
private final ServiceRepository serviceRepository;
private final ServiceMapper serviceMapper;

@Transactional
public List<ServiceTO> findAll() {
return serviceRepository.findAll().stream()
.map(serviceMapper::toDTO)
.toList();
public Service resolveService(BookingTO bookingRequest) {
String carrierServiceCode = bookingRequest.carrierServiceCode();
String universalServiceReference = bookingRequest.universalServiceReference();

if (carrierServiceCode == null && universalServiceReference == null) {
return null;
}

Service example = Service.builder()
.carrierServiceCode(carrierServiceCode)
.universalServiceReference(universalServiceReference)
.build();

return serviceRepository.findAll(Example.of(example)).stream()
.findFirst()
.orElseThrow(() -> ConcreteRequestErrorMessageException.notFound(
"No services that match carrierServiceCode '" + carrierServiceCode
+ "' and universalServiceReference '" + universalServiceReference + "'"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@
import org.dcsa.edocumentation.domain.persistence.repository.VoyageRepository;
import org.dcsa.edocumentation.transferobjects.BookingTO;
import org.dcsa.skernel.errors.exceptions.ConcreteRequestErrorMessageException;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@RequiredArgsConstructor
public class VoyageService {
private final ServiceService serviceService;
private final VoyageRepository voyageRepository;

@Transactional
public Voyage resolveVoyage(BookingTO bookingRequest) {
String universalExportVoyageReference = bookingRequest.universalExportVoyageReference();
String carrierExportVoyageNumber = bookingRequest.carrierExportVoyageNumber();
if (universalExportVoyageReference != null) {
// Since universalVoyageReference is not unique in Voyage and booking does not supply a service to make it
// unique we just take the first Voyage found.
return voyageRepository.findByUniversalVoyageReference(universalExportVoyageReference).stream()
.filter(voyage -> carrierExportVoyageNumber == null || carrierExportVoyageNumber.equals(voyage.getCarrierVoyageNumber()))

if (universalExportVoyageReference == null && carrierExportVoyageNumber == null) {
if (bookingRequest.carrierServiceCode() != null || bookingRequest.universalServiceReference() != null) {
throw ConcreteRequestErrorMessageException.invalidInput(
"carrierServiceCode and/or universalServiceReference provided but both universalExportVoyageReference and carrierExportVoyageNumber are missing");
}
return null;
}

Voyage example = Voyage.builder()
.universalVoyageReference(universalExportVoyageReference)
.carrierVoyageNumber(carrierExportVoyageNumber)
.service(serviceService.resolveService(bookingRequest))
.build();

return voyageRepository.findAll(Example.of(example)).stream()
.findFirst()
.orElseThrow(() -> ConcreteRequestErrorMessageException.notFound(
"No voyages with universalVoyageReference = '" + universalExportVoyageReference
+ "' and carrierExportVoyageNumber = '" + carrierExportVoyageNumber + "'"));
} else if (carrierExportVoyageNumber != null) {
// Since carrierVoyageNumber is not unique in Voyage and booking does not supply a service to make it
// unique we just take the first Voyage found.
return voyageRepository.findByCarrierVoyageNumber(carrierExportVoyageNumber).stream()
.findFirst()
.orElseThrow(() -> ConcreteRequestErrorMessageException.notFound(
"No voyages with carrierVoyageNumber = '" + carrierExportVoyageNumber + "'"));
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface BookingMapper {
@Mapping(source = "placeOfIssue", target = "placeOfBLIssue")
@Mapping(source = "voyage.universalVoyageReference", target = "universalExportVoyageReference")
@Mapping(source = "voyage.carrierVoyageNumber", target = "carrierExportVoyageNumber")
@Mapping(source = "voyage.service.carrierServiceCode", target = "carrierServiceCode")
@Mapping(source = "voyage.service.universalServiceReference", target = "universalServiceReference")
@Mapping(source = "vessel.vesselIMONumber", target = "vesselIMONumber")
@Mapping(source = "modeOfTransport.dcsaTransportType", target = "preCarriageModeOfTransportCode")
BookingTO toDTO(Booking booking);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.dcsa.edocumentation.datafactories;

import lombok.experimental.UtilityClass;
import org.dcsa.edocumentation.domain.persistence.entity.Service;

import java.util.UUID;

@UtilityClass
public class ServiceDataFactory {
public static Service service() {
return Service.builder()
.id(UUID.fromString("498531b8-d46a-4f8f-95ef-fd661936eb54"))
.universalServiceReference("universalServiceReference")
.carrierServiceCode("carrierServiceCode")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.dcsa.edocumentation.service;

import org.dcsa.edocumentation.datafactories.ServiceDataFactory;
import org.dcsa.edocumentation.domain.persistence.entity.Service;
import org.dcsa.edocumentation.domain.persistence.repository.ServiceRepository;
import org.dcsa.edocumentation.transferobjects.BookingTO;
import org.dcsa.skernel.errors.exceptions.NotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Example;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class ServiceServiceTest {
@Mock
private ServiceRepository serviceRepository;
@InjectMocks
private ServiceService serviceService;

@BeforeEach
public void resetMocks() {
reset(serviceRepository);
}

@Test
public void resolveService_null() {
// Setup
BookingTO bookingTO = BookingTO.builder()
.universalServiceReference(null)
.carrierServiceCode(null)
.build();

// Execute
assertNull(serviceService.resolveService(bookingTO));

// Verify
verify(serviceRepository, never()).findAll(any(Example.class));
}

@Test
public void resolveService_Unknown() {
// Setup
BookingTO bookingTO = BookingTO.builder()
.universalServiceReference("serviceRef")
.carrierServiceCode("carrierRef")
.build();

// Execute
NotFoundException exception =
assertThrows(NotFoundException.class, () -> serviceService.resolveService(bookingTO));

// Verify
assertEquals("No services that match carrierServiceCode 'carrierRef' and universalServiceReference 'serviceRef'", exception.getMessage());
verify(serviceRepository).findAll(any(Example.class));
}

@Test
public void resolveService_Known() {
// Setup
BookingTO bookingTO = BookingTO.builder()
.universalServiceReference("serviceRef")
.carrierServiceCode("carrierRef")
.build();
Service expected = ServiceDataFactory.service();
when(serviceRepository.findAll(any(Example.class))).thenReturn(List.of(expected));

// Execute
Service actual = serviceService.resolveService(bookingTO);

// Verify
assertEquals(expected, actual);
verify(serviceRepository).findAll(any(Example.class));
}
}
Loading

0 comments on commit 656bae0

Please sign in to comment.