This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from dcsaorg/DDT-1383
DDT-1383 - add carrierServiceCode + universalServiceReference as opti…
- Loading branch information
Showing
10 changed files
with
195 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 23 additions & 10 deletions
33
edocumentation-service/src/main/java/org/dcsa/edocumentation/service/ServiceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "'")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ation-service/src/test/java/org/dcsa/edocumentation/datafactories/ServiceDataFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
edocumentation-service/src/test/java/org/dcsa/edocumentation/service/ServiceServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.