From a2c64896e9e1b8d11260da1adb5b77913878f3f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Wed, 8 May 2024 16:21:45 +0200 Subject: [PATCH 01/19] RSWW-30 docker-compose.yml: change version to 4 to allow project name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 73234e0f..07321a99 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,5 @@ +version: "4" name: mcarchvsc -version: "3" services: rabbitmq: From 54aad4c25b3c394762dc8784dead0df01825e57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Wed, 8 May 2024 16:26:17 +0200 Subject: [PATCH 02/19] RSWW-30 docker-compose.yml: remove version field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 07321a99..d31085ff 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "4" +version: "3.9" name: mcarchvsc services: From 012fb1e93f7aaefad6e09448b74d7c97f06ed02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Wed, 8 May 2024 18:38:06 +0200 Subject: [PATCH 03/19] RSWW-30 docker-compose.yml: quote port mappings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- docker-compose.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index d31085ff..020ebf7d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,8 @@ services: image: rabbitmq:3.13-management hostname: rabbitmq ports: - - 5672:5672 - - 15672:15672 + - "5672:5672" + - "15672:15672" networks: - backend @@ -15,7 +15,7 @@ services: hostname: discovery build: ./discovery-service ports: - - 8010:8010 + - "8010:8010" depends_on: - rabbitmq networks: @@ -24,7 +24,7 @@ services: gateway: build: ./api-gateway ports: - - 8082:8082 + - "8082:8082" depends_on: - rabbitmq - discovery From 13fe0461c684d09a74e1b345270dd9cf23a9a304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Wed, 8 May 2024 18:38:15 +0200 Subject: [PATCH 04/19] RSWW-30 docker-compose-database.yml: add file to create database container MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- docker-compose-database.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 docker-compose-database.yml diff --git a/docker-compose-database.yml b/docker-compose-database.yml new file mode 100644 index 00000000..59b32b33 --- /dev/null +++ b/docker-compose-database.yml @@ -0,0 +1,31 @@ +version: '3.9' +name: mcarchvsc + +services: + db: + image: postgres + environment: + POSTGRES_USER: admin + POSTGRES_PASSWORD: admin + POSTGRES_DB: database + PGDATA: /data/postgres + volumes: + - postgres:/data/postgres + ports: + - "5432:5432" + restart: unless-stopped + networks: + - backend + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 5s + timeout: 5s + retries: 20 + +volumes: + postgres: + +networks: + backend: + name: mcarchvsc_backend + external: true \ No newline at end of file From bf9148ada5a39eb2373c9d4111ae35e52eae928f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 11:56:55 +0200 Subject: [PATCH 05/19] RSWW-36 test: TransportServiceTest: add extra test to check for empty list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../services/TransportsServiceTest.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java index ae0375c5..c191c7d4 100644 --- a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java +++ b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java @@ -29,7 +29,7 @@ public class TransportsServiceTest { private TransportCourseRepository transportCourseRepository; @Test - public void testGetAvailableTransports() { + public void testGetAvailableTransports_returnsCorrectDto() { // Arrange TransportCourse transportCourse = new TransportCourse(); Location location = new Location("Polska", "Warsaw"); @@ -45,5 +45,20 @@ public void testGetAvailableTransports() { assertEquals(1, result.getDepartures().getPlane().size()); assertEquals("Warsaw", result.getDepartures().getPlane().getFirst().getRegion()); assertEquals("Polska", result.getDepartures().getPlane().getFirst().getCountry()); + assertEquals(0, result.getDepartures().getBus().size()); + assertEquals(1, result.getArrivals().size()); + } + + @Test + public void testGetAvailableTransports_returnsEmptyList () { + // Arrange + when(transportCourseRepository.findAll()).thenReturn(Collections.emptyList()); + + // Act + AvailableTransportsDto result = transportsService.getAvailableTransports(); + + // Assert + assertEquals(0, result.getDepartures().getPlane().size()); + assertEquals(0, result.getDepartures().getBus().size()); } } \ No newline at end of file From 0dbe42ffd5e7c277c05cc68bd19c43e668ccc603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 12:07:18 +0200 Subject: [PATCH 06/19] RSWW-36 .github: add CI workflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .github/workflows/build-and-test.yml | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 .github/workflows/build-and-test.yml diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml new file mode 100644 index 00000000..72c5c04b --- /dev/null +++ b/.github/workflows/build-and-test.yml @@ -0,0 +1,43 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Travel API CI + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "dev" ] + +permissions: write-all + +jobs: + + test: + runs-on: ubuntu-latest + needs: build + if: always() + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: '21' + distribution: 'zulu' + cache: maven + - name: Build transport-service + run: mvn -B package --file transport-service/pom.xml + + - name: transport-service test reports + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + files: | + transport-service/target/surefire-reports/TEST-*.xml + From 2357e078c05e521186a6e30708079ef899c07683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 12:09:22 +0200 Subject: [PATCH 07/19] RSWW-36 workflows: build-and-test.yml: fix job dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .github/workflows/build-and-test.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 72c5c04b..37fd1d0e 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -1,11 +1,3 @@ -# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven - -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - name: Travel API CI on: @@ -17,10 +9,8 @@ on: permissions: write-all jobs: - test: runs-on: ubuntu-latest - needs: build if: always() steps: From ea38ec847617f5c85a69928c0d2e71dc6209feaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 12:17:07 +0200 Subject: [PATCH 08/19] RSWW-36 workflows: add CI tests for individual services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .github/workflows/test-hotel-service.yml | 37 +++++++++++++++++++ ...nd-test.yml => test-transport-service.yml} | 4 ++ 2 files changed, 41 insertions(+) create mode 100644 .github/workflows/test-hotel-service.yml rename .github/workflows/{build-and-test.yml => test-transport-service.yml} (89%) diff --git a/.github/workflows/test-hotel-service.yml b/.github/workflows/test-hotel-service.yml new file mode 100644 index 00000000..9a32c81e --- /dev/null +++ b/.github/workflows/test-hotel-service.yml @@ -0,0 +1,37 @@ +name: Travel API CI + +on: + push: + branches: [ "dev" ] + paths: + - 'hotel-service/**' + pull_request: + branches: [ "dev" ] + paths: + - 'hotel-service/**' + +permissions: write-all + +jobs: + test: + runs-on: ubuntu-latest + if: always() + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: '21' + distribution: 'zulu' + cache: maven + - name: Build transport-service + run: mvn -B package --file hotel-service/pom.xml + + - name: transport-service test reports + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + files: | + hotel-service/target/surefire-reports/TEST-*.xml + diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/test-transport-service.yml similarity index 89% rename from .github/workflows/build-and-test.yml rename to .github/workflows/test-transport-service.yml index 37fd1d0e..f6ea2c1b 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/test-transport-service.yml @@ -3,8 +3,12 @@ name: Travel API CI on: push: branches: [ "dev" ] + paths: + - 'transport-service/**' pull_request: branches: [ "dev" ] + paths: + - 'transport-service/**' permissions: write-all From 494f2d7f4b36cc2bd495fafc9aa752ba9f4ea76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 12:19:28 +0200 Subject: [PATCH 09/19] RSWW-36 test: TransportServiceTest: add newline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../transport/services/TransportsServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java index c191c7d4..ccb99c53 100644 --- a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java +++ b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java @@ -61,4 +61,4 @@ public void testGetAvailableTransports_returnsEmptyList () { assertEquals(0, result.getDepartures().getPlane().size()); assertEquals(0, result.getDepartures().getBus().size()); } -} \ No newline at end of file +} From 6cac48ebcae906b568b9f9078b4fde1008c83e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 12:22:17 +0200 Subject: [PATCH 10/19] RSWW-36 .github: rename CI workflows to reflect service names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .github/workflows/test-hotel-service.yml | 3 ++- .github/workflows/test-transport-service.yml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-hotel-service.yml b/.github/workflows/test-hotel-service.yml index 9a32c81e..893ab06f 100644 --- a/.github/workflows/test-hotel-service.yml +++ b/.github/workflows/test-hotel-service.yml @@ -1,4 +1,4 @@ -name: Travel API CI +name: Travel API CI - Hotel service on: push: @@ -14,6 +14,7 @@ permissions: write-all jobs: test: + name: Package and execute tests runs-on: ubuntu-latest if: always() diff --git a/.github/workflows/test-transport-service.yml b/.github/workflows/test-transport-service.yml index f6ea2c1b..5cf4f62d 100644 --- a/.github/workflows/test-transport-service.yml +++ b/.github/workflows/test-transport-service.yml @@ -1,4 +1,4 @@ -name: Travel API CI +name: Travel API CI - Transport service on: push: @@ -14,6 +14,7 @@ permissions: write-all jobs: test: + name: Package and execute tests runs-on: ubuntu-latest if: always() From 89568379f884b1a8ad9364543b07fbd793a340b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 13:21:53 +0200 Subject: [PATCH 11/19] RSWW-54 transport-service: create basic get transport by query handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- transport-service/pom.xml | 9 +++++ .../transport/TransportApplication.java | 2 ++ .../transport/bootstrap/Bootstrap.java | 6 +++- .../controllers/TransportsController.java | 13 ++++++- .../dto/{transports => }/LocationDto.java | 2 +- .../model/dto/TransportCourseDto.java | 13 +++++++ .../transport/model/dto/TransportDto.java | 18 ++++++++++ .../GetTransportsBySearchQueryRequestDto.java | 23 ++++++++++++ .../AvailableTransportsDepartures.java | 4 +-- .../response/AvailableTransportsDto.java | 4 +-- ...GetTransportsBySearchQueryResponseDto.java | 13 +++++++ .../model/mappers/LocationMapper.java | 2 +- .../transport/queues/config/QueuesConfig.java | 19 ++++++++++ .../transport/services/TransportsService.java | 35 +++++++++++++++++-- .../services/TransportsServiceTest.java | 6 +--- 15 files changed, 154 insertions(+), 15 deletions(-) rename transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/{transports => }/LocationDto.java (72%) create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java rename transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/{transports => }/response/AvailableTransportsDepartures.java (56%) rename transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/{transports => }/response/AvailableTransportsDto.java (58%) create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java diff --git a/transport-service/pom.xml b/transport-service/pom.xml index 014846ae..6096cccf 100644 --- a/transport-service/pom.xml +++ b/transport-service/pom.xml @@ -70,6 +70,15 @@ hibernate-validator 8.0.0.Final + + org.springframework.boot + spring-boot-starter-amqp + + + org.springframework.amqp + spring-rabbit-test + test + diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/TransportApplication.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/TransportApplication.java index 1a83eb69..a203d044 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/TransportApplication.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/TransportApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class TransportApplication { public static void main(String[] args) { diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java index e04cef4e..2cac7a7c 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java @@ -5,11 +5,14 @@ import org.microarchitecturovisco.transport.model.domain.Transport; import org.microarchitecturovisco.transport.model.domain.TransportCourse; import org.microarchitecturovisco.transport.model.domain.TransportType; -import org.microarchitecturovisco.transport.model.dto.transports.LocationDto; import org.microarchitecturovisco.transport.repositories.LocationRepository; import org.microarchitecturovisco.transport.repositories.TransportCourseRepository; import org.microarchitecturovisco.transport.repositories.TransportRepository; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @@ -27,6 +30,7 @@ public class Bootstrap implements CommandLineRunner { private final LocationRepository locationRepository; private final TransportRepository transportRepository; private final TransportCourseRepository transportCourseRepository; + private final RabbitTemplate rabbitTemplate; @Override public void run(String... args) throws Exception { diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java index 179f550d..6aefab18 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java @@ -1,8 +1,12 @@ package org.microarchitecturovisco.transport.controllers; import lombok.RequiredArgsConstructor; -import org.microarchitecturovisco.transport.model.dto.transports.response.AvailableTransportsDto; +import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; +import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto; +import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; import org.microarchitecturovisco.transport.services.TransportsService; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -13,6 +17,7 @@ public class TransportsController { private final TransportsService transportsService; + private final RabbitTemplate rabbitTemplate; @GetMapping("/available") public AvailableTransportsDto getAvailableTransports() { @@ -24,4 +29,10 @@ public String test() { return "test"; } + @RabbitListener(queues = "transports.requests.getTransportsBySearchQuery") + public void consumeGetTransportsRequest(GetTransportsBySearchQueryRequestDto requestDto) { + GetTransportsBySearchQueryResponseDto responseDto = transportsService.getTransportsBySearchQuery(requestDto); + + rabbitTemplate.convertAndSend("transports.responses.getTransportsBySearchQuery", responseDto); + } } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/LocationDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java similarity index 72% rename from transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/LocationDto.java rename to transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java index cf1c510a..16e10a69 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/LocationDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java @@ -1,4 +1,4 @@ -package org.microarchitecturovisco.transport.model.dto.transports; +package org.microarchitecturovisco.transport.model.dto; import lombok.Builder; import lombok.Data; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java new file mode 100644 index 00000000..b86ec165 --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java @@ -0,0 +1,13 @@ +package org.microarchitecturovisco.transport.model.dto; + +import lombok.Builder; +import lombok.Data; +import org.microarchitecturovisco.transport.model.domain.TransportType; + +@Data +@Builder +public class TransportCourseDto { + private TransportType type; + private LocationDto departureFromLocation; + private LocationDto arrivalAtLocation; +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java new file mode 100644 index 00000000..5961514f --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java @@ -0,0 +1,18 @@ +package org.microarchitecturovisco.transport.model.dto; + +import lombok.Builder; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@Builder +public class TransportDto { + private Integer idTransport; + + private LocalDateTime departureDate; + private Integer capacity; + private Float pricePerAdult; + + private TransportCourseDto transportCourse; +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java new file mode 100644 index 00000000..ebb2438e --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java @@ -0,0 +1,23 @@ +package org.microarchitecturovisco.transport.model.dto.request; + +import lombok.Builder; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +@Builder +public class GetTransportsBySearchQueryRequestDto { + private List departureLocationIdsByPlane; + private List departureLocationIdsByBus; + private List arrivalLocationIds; + + private LocalDateTime dateFrom; + private LocalDateTime dateTo; + + private Integer adults; + private Integer childrenUnderThree; + private Integer childrenUnderTen; + private Integer childrenUnderEighteen; +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/response/AvailableTransportsDepartures.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/AvailableTransportsDepartures.java similarity index 56% rename from transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/response/AvailableTransportsDepartures.java rename to transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/AvailableTransportsDepartures.java index 05d3bb7f..b200a353 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/response/AvailableTransportsDepartures.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/AvailableTransportsDepartures.java @@ -1,9 +1,9 @@ -package org.microarchitecturovisco.transport.model.dto.transports.response; +package org.microarchitecturovisco.transport.model.dto.response; import lombok.Builder; import lombok.Data; -import org.microarchitecturovisco.transport.model.dto.transports.LocationDto; +import org.microarchitecturovisco.transport.model.dto.LocationDto; import java.util.List; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/response/AvailableTransportsDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/AvailableTransportsDto.java similarity index 58% rename from transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/response/AvailableTransportsDto.java rename to transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/AvailableTransportsDto.java index f47d3c9c..4ec9fae1 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/transports/response/AvailableTransportsDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/AvailableTransportsDto.java @@ -1,8 +1,8 @@ -package org.microarchitecturovisco.transport.model.dto.transports.response; +package org.microarchitecturovisco.transport.model.dto.response; import lombok.Builder; import lombok.Data; -import org.microarchitecturovisco.transport.model.dto.transports.LocationDto; +import org.microarchitecturovisco.transport.model.dto.LocationDto; import java.util.List; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java new file mode 100644 index 00000000..3bb7fe87 --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java @@ -0,0 +1,13 @@ +package org.microarchitecturovisco.transport.model.dto.response; + +import lombok.Builder; +import lombok.Data; +import org.microarchitecturovisco.transport.model.dto.TransportDto; + +import java.util.List; + +@Data +@Builder +public class GetTransportsBySearchQueryResponseDto { + private List transportDtoList; +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/LocationMapper.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/LocationMapper.java index 15610108..242b1557 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/LocationMapper.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/LocationMapper.java @@ -1,7 +1,7 @@ package org.microarchitecturovisco.transport.model.mappers; import org.microarchitecturovisco.transport.model.domain.Location; -import org.microarchitecturovisco.transport.model.dto.transports.LocationDto; +import org.microarchitecturovisco.transport.model.dto.LocationDto; import java.util.List; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java new file mode 100644 index 00000000..a87f01b2 --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java @@ -0,0 +1,19 @@ +package org.microarchitecturovisco.transport.queues.config; + +import org.springframework.amqp.core.Queue; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class QueuesConfig { + + @Bean + public Queue getTransportsRequest() { + return new Queue("transports.requests.getTransportsBySearchQuery", false); + } + + @Bean + public Queue getTransportsResponse() { + return new Queue("transports.response.getTransportsBySearchQuery", false); + } +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java index 119f3d80..5dfa53c9 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java @@ -2,12 +2,18 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.transport.model.domain.Location; +import org.microarchitecturovisco.transport.model.domain.Transport; import org.microarchitecturovisco.transport.model.domain.TransportCourse; import org.microarchitecturovisco.transport.model.domain.TransportType; -import org.microarchitecturovisco.transport.model.dto.transports.response.AvailableTransportsDepartures; -import org.microarchitecturovisco.transport.model.dto.transports.response.AvailableTransportsDto; +import org.microarchitecturovisco.transport.model.dto.TransportCourseDto; +import org.microarchitecturovisco.transport.model.dto.TransportDto; +import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; +import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDepartures; +import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto; +import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; import org.microarchitecturovisco.transport.model.mappers.LocationMapper; import org.microarchitecturovisco.transport.repositories.TransportCourseRepository; +import org.microarchitecturovisco.transport.repositories.TransportRepository; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -18,6 +24,7 @@ public class TransportsService { private final TransportCourseRepository transportCourseRepository; + private final TransportRepository transportRepository; public AvailableTransportsDto getAvailableTransports() { @@ -57,4 +64,28 @@ public AvailableTransportsDto buildAvailableTransports( .build()) .build(); } + + public GetTransportsBySearchQueryResponseDto getTransportsBySearchQuery(GetTransportsBySearchQueryRequestDto requestDto) { + + List transports = transportRepository.findAll(); + + Transport testTransport = transports.getFirst(); + TransportCourseDto transportCourseDto = TransportCourseDto.builder() + .type(testTransport.getCourse().getType()) + .departureFromLocation(LocationMapper.map(testTransport.getCourse().getDepartureFrom())) + .arrivalAtLocation(LocationMapper.map(testTransport.getCourse().getArrivalAt())) + .build(); + + return GetTransportsBySearchQueryResponseDto.builder() + .transportDtoList( + List.of(TransportDto.builder() + .idTransport(testTransport.getId()) + .transportCourse(transportCourseDto) + .departureDate(testTransport.getDepartureDate()) + .capacity(testTransport.getCapacity()) + .pricePerAdult(testTransport.getPricePerAdult()) + .build()) + + ).build(); + } } diff --git a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java index ccb99c53..552599ae 100644 --- a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java +++ b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java @@ -1,19 +1,15 @@ package org.microarchitecturovisco.transport.services; -import static org.junit.jupiter.api.Assertions.*; - import org.junit.jupiter.api.Test; import org.microarchitecturovisco.transport.model.domain.Location; import org.microarchitecturovisco.transport.model.domain.TransportCourse; import org.microarchitecturovisco.transport.model.domain.TransportType; -import org.microarchitecturovisco.transport.model.dto.transports.response.AvailableTransportsDto; +import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto; import org.microarchitecturovisco.transport.repositories.TransportCourseRepository; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; -import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; From 89a4ecc93600d4522a6c2d8924002809ba79cdd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 13:32:50 +0200 Subject: [PATCH 12/19] RSWW-54 transport-service: mappers: add mappers for transport and transport course MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../model/mappers/TransportCourseMapper.java | 14 ++++++++++++ .../model/mappers/TransportMapper.java | 22 +++++++++++++++++++ .../transport/services/TransportsService.java | 18 ++++----------- 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportCourseMapper.java create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportMapper.java diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportCourseMapper.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportCourseMapper.java new file mode 100644 index 00000000..2796564e --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportCourseMapper.java @@ -0,0 +1,14 @@ +package org.microarchitecturovisco.transport.model.mappers; + +import org.microarchitecturovisco.transport.model.domain.TransportCourse; +import org.microarchitecturovisco.transport.model.dto.TransportCourseDto; + +public class TransportCourseMapper { + public static TransportCourseDto map(TransportCourse transportCourse) { + return TransportCourseDto.builder() + .type(transportCourse.getType()) + .arrivalAtLocation(LocationMapper.map(transportCourse.getArrivalAt())) + .departureFromLocation(LocationMapper.map(transportCourse.getDepartureFrom())) + .build(); + } +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportMapper.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportMapper.java new file mode 100644 index 00000000..ee81f114 --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/mappers/TransportMapper.java @@ -0,0 +1,22 @@ +package org.microarchitecturovisco.transport.model.mappers; + +import org.microarchitecturovisco.transport.model.domain.Transport; +import org.microarchitecturovisco.transport.model.dto.TransportDto; + +import java.util.List; + +public class TransportMapper { + public static TransportDto map(Transport transport) { + return TransportDto.builder() + .idTransport(transport.getId()) + .transportCourse(TransportCourseMapper.map(transport.getCourse())) + .departureDate(transport.getDepartureDate()) + .pricePerAdult(transport.getPricePerAdult()) + .capacity(transport.getCapacity()) + .build(); + } + + public static List mapList(List transports) { + return transports.stream().map(TransportMapper::map).toList(); + } +} diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java index 5dfa53c9..893b9cbe 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java @@ -12,6 +12,7 @@ import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto; import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; import org.microarchitecturovisco.transport.model.mappers.LocationMapper; +import org.microarchitecturovisco.transport.model.mappers.TransportMapper; import org.microarchitecturovisco.transport.repositories.TransportCourseRepository; import org.microarchitecturovisco.transport.repositories.TransportRepository; import org.springframework.stereotype.Service; @@ -69,23 +70,12 @@ public GetTransportsBySearchQueryResponseDto getTransportsBySearchQuery(GetTrans List transports = transportRepository.findAll(); - Transport testTransport = transports.getFirst(); - TransportCourseDto transportCourseDto = TransportCourseDto.builder() - .type(testTransport.getCourse().getType()) - .departureFromLocation(LocationMapper.map(testTransport.getCourse().getDepartureFrom())) - .arrivalAtLocation(LocationMapper.map(testTransport.getCourse().getArrivalAt())) - .build(); + Transport transportA = transports.getFirst(); + Transport transportB = transports.get(1); return GetTransportsBySearchQueryResponseDto.builder() .transportDtoList( - List.of(TransportDto.builder() - .idTransport(testTransport.getId()) - .transportCourse(transportCourseDto) - .departureDate(testTransport.getDepartureDate()) - .capacity(testTransport.getCapacity()) - .pricePerAdult(testTransport.getPricePerAdult()) - .build()) - + TransportMapper.mapList(List.of(transportA, transportB)) ).build(); } } From 9a73c5651670a33a797f2129b20a4ece8add56ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 13:36:10 +0200 Subject: [PATCH 13/19] RSWW-54 transport-service: get transports by search query: add uuid to request and response dto MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../model/dto/request/GetTransportsBySearchQueryRequestDto.java | 2 ++ .../dto/response/GetTransportsBySearchQueryResponseDto.java | 1 + .../transport/services/TransportsService.java | 1 + 3 files changed, 4 insertions(+) diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java index ebb2438e..02f9273e 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java @@ -9,6 +9,8 @@ @Data @Builder public class GetTransportsBySearchQueryRequestDto { + private String uuid; + private List departureLocationIdsByPlane; private List departureLocationIdsByBus; private List arrivalLocationIds; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java index 3bb7fe87..00f89c71 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java @@ -9,5 +9,6 @@ @Data @Builder public class GetTransportsBySearchQueryResponseDto { + private String uuid; private List transportDtoList; } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java index 893b9cbe..1b8faaea 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java @@ -74,6 +74,7 @@ public GetTransportsBySearchQueryResponseDto getTransportsBySearchQuery(GetTrans Transport transportB = transports.get(1); return GetTransportsBySearchQueryResponseDto.builder() + .uuid(requestDto.getUuid()) .transportDtoList( TransportMapper.mapList(List.of(transportA, transportB)) ).build(); From 53c1fe1d7289c06a99dc8a3e36c380bf72d6dcb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 14:09:59 +0200 Subject: [PATCH 14/19] RSWW-54 transport-service: add tests for get transport by query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../transport/bootstrap/Bootstrap.java | 36 +++++++++++++++++++ .../controllers/TransportsController.java | 2 ++ .../transport/model/dto/LocationDto.java | 4 ++- .../model/dto/TransportCourseDto.java | 4 ++- .../transport/model/dto/TransportDto.java | 3 +- .../GetTransportsBySearchQueryRequestDto.java | 9 ++--- ...GetTransportsBySearchQueryResponseDto.java | 3 +- .../transport/queues/config/QueuesConfig.java | 2 +- .../queues/config/RabbitMQConfig.java | 17 +++++++++ 9 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/RabbitMQConfig.java diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java index 2cac7a7c..836ffe21 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java @@ -5,10 +5,15 @@ import org.microarchitecturovisco.transport.model.domain.Transport; import org.microarchitecturovisco.transport.model.domain.TransportCourse; import org.microarchitecturovisco.transport.model.domain.TransportType; +import org.microarchitecturovisco.transport.model.dto.TransportDto; +import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; +import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; import org.microarchitecturovisco.transport.repositories.LocationRepository; import org.microarchitecturovisco.transport.repositories.TransportCourseRepository; import org.microarchitecturovisco.transport.repositories.TransportRepository; import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; @@ -121,4 +126,35 @@ public void run(String... args) throws Exception { } + @Scheduled(fixedDelay = 10000) + public void testGetTransportsBySearchQuery() { + GetTransportsBySearchQueryRequestDto testRequestDto = GetTransportsBySearchQueryRequestDto.builder() + .uuid(java.util.UUID.randomUUID().toString()) + .dateFrom(LocalDateTime.of(2024, Month.MAY, 1, 12, 0, 0)) + .dateTo(LocalDateTime.of(2024, Month.MAY, 14, 12, 0, 0)) + .departureLocationIdsByPlane(List.of(1, 2)) + .departureLocationIdsByBus(List.of()) + .arrivalLocationIds(List.of(7)) + .adults(2) + .childrenUnderThree(1) + .childrenUnderTen(1) + .childrenUnderEighteen(1) + .build(); + + rabbitTemplate.convertAndSend("transports.requests.getTransportsBySearchQuery", testRequestDto); + } + + @RabbitListener(queues = "transports.responses.getTransportsBySearchQuery") + @RabbitHandler + public void consumeGetTransportsResponse(GetTransportsBySearchQueryResponseDto responseDto) { + + System.out.println("Received transports:"); + for (TransportDto transportDto : responseDto.getTransportDtoList()) { + System.out.println(" - " + transportDto.getTransportCourse().getDepartureFromLocation().getRegion() + " <-> " + transportDto.getTransportCourse().getArrivalAtLocation().getRegion()); + System.out.println(" " + transportDto.getDepartureDate()); + } + + System.out.println("Received transports of size " + responseDto.getTransportDtoList().size()); + } + } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java index 6aefab18..62317ee4 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsController.java @@ -33,6 +33,8 @@ public String test() { public void consumeGetTransportsRequest(GetTransportsBySearchQueryRequestDto requestDto) { GetTransportsBySearchQueryResponseDto responseDto = transportsService.getTransportsBySearchQuery(requestDto); + System.out.println("Send transports response size " + responseDto.getTransportDtoList().size()); + rabbitTemplate.convertAndSend("transports.responses.getTransportsBySearchQuery", responseDto); } } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java index 16e10a69..665f6a8c 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/LocationDto.java @@ -3,9 +3,11 @@ import lombok.Builder; import lombok.Data; +import java.io.Serializable; + @Data @Builder -public class LocationDto { +public class LocationDto implements Serializable { private Integer idLocation; private String country; private String region; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java index b86ec165..e61dbe89 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportCourseDto.java @@ -4,9 +4,11 @@ import lombok.Data; import org.microarchitecturovisco.transport.model.domain.TransportType; +import java.io.Serializable; + @Data @Builder -public class TransportCourseDto { +public class TransportCourseDto implements Serializable { private TransportType type; private LocationDto departureFromLocation; private LocationDto arrivalAtLocation; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java index 5961514f..8b132b8a 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/TransportDto.java @@ -3,11 +3,12 @@ import lombok.Builder; import lombok.Data; +import java.io.Serializable; import java.time.LocalDateTime; @Data @Builder -public class TransportDto { +public class TransportDto implements Serializable { private Integer idTransport; private LocalDateTime departureDate; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java index 02f9273e..2237eb24 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/request/GetTransportsBySearchQueryRequestDto.java @@ -3,21 +3,22 @@ import lombok.Builder; import lombok.Data; +import java.io.Serializable; import java.time.LocalDateTime; import java.util.List; @Data @Builder -public class GetTransportsBySearchQueryRequestDto { +public class GetTransportsBySearchQueryRequestDto implements Serializable { private String uuid; + private LocalDateTime dateFrom; + private LocalDateTime dateTo; + private List departureLocationIdsByPlane; private List departureLocationIdsByBus; private List arrivalLocationIds; - private LocalDateTime dateFrom; - private LocalDateTime dateTo; - private Integer adults; private Integer childrenUnderThree; private Integer childrenUnderTen; diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java index 00f89c71..20c0b469 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/dto/response/GetTransportsBySearchQueryResponseDto.java @@ -4,11 +4,12 @@ import lombok.Data; import org.microarchitecturovisco.transport.model.dto.TransportDto; +import java.io.Serializable; import java.util.List; @Data @Builder -public class GetTransportsBySearchQueryResponseDto { +public class GetTransportsBySearchQueryResponseDto implements Serializable { private String uuid; private List transportDtoList; } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java index a87f01b2..a090412b 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/QueuesConfig.java @@ -14,6 +14,6 @@ public Queue getTransportsRequest() { @Bean public Queue getTransportsResponse() { - return new Queue("transports.response.getTransportsBySearchQuery", false); + return new Queue("transports.responses.getTransportsBySearchQuery", false); } } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/RabbitMQConfig.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/RabbitMQConfig.java new file mode 100644 index 00000000..d4ccbb47 --- /dev/null +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/queues/config/RabbitMQConfig.java @@ -0,0 +1,17 @@ +package org.microarchitecturovisco.transport.queues.config; + +import org.springframework.amqp.support.converter.SimpleMessageConverter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +@Configuration +public class RabbitMQConfig { + @Bean + public SimpleMessageConverter converter() { + SimpleMessageConverter converter = new SimpleMessageConverter(); + converter.setAllowedListPatterns(List.of("org.microarchitecturovisco.*", "java.*")); + return converter; + } +} From 41625c4239190be4d2f78c845a8482aa6d4187bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 17:55:49 +0200 Subject: [PATCH 15/19] RSWW-54 transport-service: add logic for get transports by search query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../transport/bootstrap/Bootstrap.java | 32 +++++++++--- .../transport/model/domain/Transport.java | 4 +- .../transport/services/TransportsService.java | 51 ++++++++++++++++--- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java index 836ffe21..656a8000 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/bootstrap/Bootstrap.java @@ -1,10 +1,7 @@ package org.microarchitecturovisco.transport.bootstrap; import lombok.RequiredArgsConstructor; -import org.microarchitecturovisco.transport.model.domain.Location; -import org.microarchitecturovisco.transport.model.domain.Transport; -import org.microarchitecturovisco.transport.model.domain.TransportCourse; -import org.microarchitecturovisco.transport.model.domain.TransportType; +import org.microarchitecturovisco.transport.model.domain.*; import org.microarchitecturovisco.transport.model.dto.TransportDto; import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; @@ -103,12 +100,33 @@ public void run(String... args) throws Exception { // generate transport for each course and every day of two months for (int day = 0; day < 60; day++) { for (TransportCourse planeCourse : planeCourses) { + int capacity = ThreadLocalRandom.current().nextInt(80, 100); + Transport transport = Transport.builder() .course(planeCourse) .departureDate(bootstrapBeginDay.plusDays(day)) - .capacity(ThreadLocalRandom.current().nextInt(80, 100)) + .capacity(capacity) .pricePerAdult(ThreadLocalRandom.current().nextFloat(100, 500)) .build(); + transport = transportRepository.save(transport); + + int numberOfReservationsToMake = ThreadLocalRandom.current().nextInt(0, 10) > 6 ? capacity : (int) ( capacity * 0.8); + + List reservations = new ArrayList<>(); + + while (numberOfReservationsToMake > 0) { + int occupiedSeats = ThreadLocalRandom.current().nextInt(1, 8); + + if (numberOfReservationsToMake - occupiedSeats < 0) continue; + + TransportReservation reservation = TransportReservation.builder() + .numberOfSeats(occupiedSeats) + .transport(transport) + .build(); + reservations.add(reservation); + numberOfReservationsToMake -= occupiedSeats; + } + transport.setTransportReservations(reservations); transports.add(transportRepository.save(transport)); } for (TransportCourse busCourse : busCourses) { @@ -132,9 +150,9 @@ public void testGetTransportsBySearchQuery() { .uuid(java.util.UUID.randomUUID().toString()) .dateFrom(LocalDateTime.of(2024, Month.MAY, 1, 12, 0, 0)) .dateTo(LocalDateTime.of(2024, Month.MAY, 14, 12, 0, 0)) - .departureLocationIdsByPlane(List.of(1, 2)) + .departureLocationIdsByPlane(List.of(1)) .departureLocationIdsByBus(List.of()) - .arrivalLocationIds(List.of(7)) + .arrivalLocationIds(List.of(6)) .adults(2) .childrenUnderThree(1) .childrenUnderTen(1) diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/domain/Transport.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/domain/Transport.java index f241f1fa..dde46de9 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/model/domain/Transport.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/model/domain/Transport.java @@ -1,8 +1,10 @@ package org.microarchitecturovisco.transport.model.domain; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.*; +import org.springframework.data.repository.cdi.Eager; import java.time.LocalDateTime; import java.util.List; @@ -32,6 +34,6 @@ public class Transport { @NotNull private float pricePerAdult; - @OneToMany(mappedBy="transport", cascade = CascadeType.ALL) + @OneToMany(mappedBy="transport", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List transportReservations; } diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java index 1b8faaea..82758cdc 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java @@ -1,10 +1,7 @@ package org.microarchitecturovisco.transport.services; import lombok.RequiredArgsConstructor; -import org.microarchitecturovisco.transport.model.domain.Location; -import org.microarchitecturovisco.transport.model.domain.Transport; -import org.microarchitecturovisco.transport.model.domain.TransportCourse; -import org.microarchitecturovisco.transport.model.domain.TransportType; +import org.microarchitecturovisco.transport.model.domain.*; import org.microarchitecturovisco.transport.model.dto.TransportCourseDto; import org.microarchitecturovisco.transport.model.dto.TransportDto; import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; @@ -70,13 +67,53 @@ public GetTransportsBySearchQueryResponseDto getTransportsBySearchQuery(GetTrans List transports = transportRepository.findAll(); - Transport transportA = transports.getFirst(); - Transport transportB = transports.get(1); + List filteredTransports = new ArrayList<>(); + + List mergedDepartureLocationIds = new ArrayList<>(); + mergedDepartureLocationIds.addAll(requestDto.getDepartureLocationIdsByBus()); + mergedDepartureLocationIds.addAll(requestDto.getDepartureLocationIdsByPlane()); + + for (Transport transport : transports) { + if ((requestDto.getDateFrom() != null || requestDto.getDateTo() != null) && + (transport.getDepartureDate().isBefore(requestDto.getDateFrom()) || transport.getDepartureDate().isAfter(requestDto.getDateTo()))) { + continue; + } + + if ((requestDto.getAdults() != null || requestDto.getChildrenUnderTen() != null || requestDto.getChildrenUnderThree() != null || requestDto.getChildrenUnderEighteen() != null ) && + !canTransportAccommodateRequestedPeople(transport, requestDto.getAdults(), requestDto.getChildrenUnderTen(), requestDto.getChildrenUnderEighteen())) { + continue; + } + + if (!mergedDepartureLocationIds.isEmpty() && !mergedDepartureLocationIds.contains(transport.getCourse().getDepartureFrom().getId())) { + continue; + } + + if (!requestDto.getArrivalLocationIds().isEmpty() && !requestDto.getArrivalLocationIds().contains(transport.getCourse().getArrivalAt().getId())) { + continue; + } + + filteredTransports.add(transport); + } return GetTransportsBySearchQueryResponseDto.builder() .uuid(requestDto.getUuid()) .transportDtoList( - TransportMapper.mapList(List.of(transportA, transportB)) + TransportMapper.mapList(filteredTransports) ).build(); } + + public boolean canTransportAccommodateRequestedPeople( + Transport transport, + Integer adults, + Integer childrenUnderTen, + Integer childrenUnderEighteen) { + return getTransportOccupiedSeats(transport) - adults - childrenUnderTen - childrenUnderEighteen >= 0; + } + + public Integer getTransportOccupiedSeats(Transport transport) { + return transport.getTransportReservations() + .stream() + .mapToInt(TransportReservation::getNumberOfSeats) + .sum(); + } } From a09a3a53d210d46e0a64b89a77eedd797cb4bc4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 May 2024 18:03:08 +0200 Subject: [PATCH 16/19] RSWW-54 test: TransportsServiceTest: add test to check if UUID in response is the same as request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../services/TransportsServiceTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java index 552599ae..5541bebe 100644 --- a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java +++ b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java @@ -4,12 +4,18 @@ import org.microarchitecturovisco.transport.model.domain.Location; import org.microarchitecturovisco.transport.model.domain.TransportCourse; import org.microarchitecturovisco.transport.model.domain.TransportType; +import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto; +import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; import org.microarchitecturovisco.transport.repositories.TransportCourseRepository; +import org.microarchitecturovisco.transport.repositories.TransportRepository; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.boot.test.context.SpringBootTest; +import java.time.LocalDateTime; +import java.time.Month; +import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -24,6 +30,9 @@ public class TransportsServiceTest { @Mock private TransportCourseRepository transportCourseRepository; + @Mock + private TransportRepository transportRepository; + @Test public void testGetAvailableTransports_returnsCorrectDto() { // Arrange @@ -57,4 +66,32 @@ public void testGetAvailableTransports_returnsEmptyList () { assertEquals(0, result.getDepartures().getPlane().size()); assertEquals(0, result.getDepartures().getBus().size()); } + + @Test + public void testGetTransportsBySearchQuery_UUID_correct() { + // Arrange + GetTransportsBySearchQueryRequestDto requestDto = GetTransportsBySearchQueryRequestDto.builder() + .uuid(java.util.UUID.randomUUID().toString()) + .dateFrom(LocalDateTime.of(2024, Month.MAY, 1, 12, 0, 0)) + .dateTo(LocalDateTime.of(2024, Month.MAY, 14, 12, 0, 0)) + .departureLocationIdsByPlane(List.of(1)) + .departureLocationIdsByBus(List.of()) + .arrivalLocationIds(List.of(6)) + .adults(2) + .childrenUnderThree(1) + .childrenUnderTen(1) + .childrenUnderEighteen(1) + .build(); + + requestDto.setDepartureLocationIdsByBus(Collections.singletonList(1)); + requestDto.setDepartureLocationIdsByPlane(Collections.singletonList(2)); + + when(transportRepository.findAll()).thenReturn(Collections.emptyList()); + + // Act + GetTransportsBySearchQueryResponseDto responseDto = transportsService.getTransportsBySearchQuery(requestDto); + + // Assert + assertEquals(requestDto.getUuid(), responseDto.getUuid()); + } } From 66fc8f0cdd49e32a1a28547cea323bdb14e219f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 May 2024 06:46:41 +0200 Subject: [PATCH 17/19] RSWW-55 test: TransportsServiceTest: add date and locationIDs tests for get transports by search query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../services/TransportsServiceTest.java | 123 +++++++++++++++++- 1 file changed, 120 insertions(+), 3 deletions(-) diff --git a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java index 5541bebe..dd03087f 100644 --- a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java +++ b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java @@ -1,9 +1,7 @@ package org.microarchitecturovisco.transport.services; import org.junit.jupiter.api.Test; -import org.microarchitecturovisco.transport.model.domain.Location; -import org.microarchitecturovisco.transport.model.domain.TransportCourse; -import org.microarchitecturovisco.transport.model.domain.TransportType; +import org.microarchitecturovisco.transport.model.domain.*; import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto; import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto; import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto; @@ -94,4 +92,123 @@ public void testGetTransportsBySearchQuery_UUID_correct() { // Assert assertEquals(requestDto.getUuid(), responseDto.getUuid()); } + + @Test + public void testGetTransportsBySearchQuery_DateRange() { + // Arrange + GetTransportsBySearchQueryRequestDto requestDto = GetTransportsBySearchQueryRequestDto.builder() + .uuid(java.util.UUID.randomUUID().toString()) + .dateFrom(LocalDateTime.of(2024, Month.MAY, 6, 12, 0, 0)) + .dateTo(LocalDateTime.of(2024, Month.MAY, 12, 12, 0, 0)) + .build(); + + Transport transportA = Transport.builder() + .id(1) + .departureDate(LocalDateTime.of(2024, Month.MAY, 5, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(1).country("Poland").region("Gdańsk").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(100) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + Transport transportB = Transport.builder() + .id(2) + .departureDate(LocalDateTime.of(2024, Month.MAY, 10, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(1).country("Poland").region("Gdańsk").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(100) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + Transport transportC = Transport.builder() + .id(3) + .departureDate(LocalDateTime.of(2024, Month.MAY, 15, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(1).country("Poland").region("Gdańsk").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(100) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + when(transportRepository.findAll()).thenReturn(List.of(transportA, transportB, transportC)); + + // Act + GetTransportsBySearchQueryResponseDto responseDto = transportsService.getTransportsBySearchQuery(requestDto); + + // Assert + assertEquals(2, responseDto.getTransportDtoList().getFirst().getIdTransport()); + assertEquals(1, responseDto.getTransportDtoList().size()); + + } + + @Test + public void testGetTransportsBySearchQuery_LocationIds() { + // Arrange + GetTransportsBySearchQueryRequestDto requestDto = GetTransportsBySearchQueryRequestDto.builder() + .uuid(java.util.UUID.randomUUID().toString()) + .departureLocationIdsByPlane(List.of(1)) + .departureLocationIdsByBus(List.of()) + .arrivalLocationIds(List.of(2)) + .build(); + + Transport transportA = Transport.builder() + .id(1) + .departureDate(LocalDateTime.of(2024, Month.MAY, 5, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(1).country("Poland").region("Gdańsk").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(100) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + Transport transportB = Transport.builder() + .id(2) + .departureDate(LocalDateTime.of(2024, Month.MAY, 10, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(5).country("Poland").region("Wrocław").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(100) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + when(transportRepository.findAll()).thenReturn(List.of(transportA, transportB)); + + // Act + GetTransportsBySearchQueryResponseDto responseDto = transportsService.getTransportsBySearchQuery(requestDto); + + // Assert + assertEquals(1, responseDto.getTransportDtoList().getFirst().getIdTransport()); + } } From e001de6ff499aa13d8ba0c977f46b17174cfb892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 May 2024 06:57:59 +0200 Subject: [PATCH 18/19] RSWW-55 transport-service: get transports by search query: handle cases for null lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../transport/services/TransportsService.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java index 82758cdc..4ef369a0 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/services/TransportsService.java @@ -70,8 +70,12 @@ public GetTransportsBySearchQueryResponseDto getTransportsBySearchQuery(GetTrans List filteredTransports = new ArrayList<>(); List mergedDepartureLocationIds = new ArrayList<>(); - mergedDepartureLocationIds.addAll(requestDto.getDepartureLocationIdsByBus()); - mergedDepartureLocationIds.addAll(requestDto.getDepartureLocationIdsByPlane()); + if (requestDto.getDepartureLocationIdsByPlane() != null) { + mergedDepartureLocationIds.addAll(requestDto.getDepartureLocationIdsByPlane()); + } + if (requestDto.getDepartureLocationIdsByBus() != null) { + mergedDepartureLocationIds.addAll(requestDto.getDepartureLocationIdsByBus()); + } for (Transport transport : transports) { if ((requestDto.getDateFrom() != null || requestDto.getDateTo() != null) && @@ -88,7 +92,9 @@ public GetTransportsBySearchQueryResponseDto getTransportsBySearchQuery(GetTrans continue; } - if (!requestDto.getArrivalLocationIds().isEmpty() && !requestDto.getArrivalLocationIds().contains(transport.getCourse().getArrivalAt().getId())) { + if (requestDto.getArrivalLocationIds() != null && + !requestDto.getArrivalLocationIds().isEmpty() && + !requestDto.getArrivalLocationIds().contains(transport.getCourse().getArrivalAt().getId())) { continue; } @@ -107,7 +113,7 @@ public boolean canTransportAccommodateRequestedPeople( Integer adults, Integer childrenUnderTen, Integer childrenUnderEighteen) { - return getTransportOccupiedSeats(transport) - adults - childrenUnderTen - childrenUnderEighteen >= 0; + return transport.getCapacity() - getTransportOccupiedSeats(transport) - adults - childrenUnderTen - childrenUnderEighteen >= 0; } public Integer getTransportOccupiedSeats(Transport transport) { From 233e708c4d9a56025380f8d1e794cce8935692bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 May 2024 06:58:48 +0200 Subject: [PATCH 19/19] RSWW-55 transport-service: test: TransportsServiceTest: test if people can fit into a mode of transport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../services/TransportsServiceTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java index dd03087f..89851758 100644 --- a/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java +++ b/transport-service/src/test/java/org/microarchitecturovisco/transport/services/TransportsServiceTest.java @@ -211,4 +211,95 @@ public void testGetTransportsBySearchQuery_LocationIds() { // Assert assertEquals(1, responseDto.getTransportDtoList().getFirst().getIdTransport()); } + + @Test + public void testGetTransportsBySearchQuery_People() { + // Arrange + GetTransportsBySearchQueryRequestDto requestDto = GetTransportsBySearchQueryRequestDto.builder() + .uuid(java.util.UUID.randomUUID().toString()) + .adults(2) + .childrenUnderThree(1) + .childrenUnderTen(1) + .childrenUnderEighteen(2) + .build(); + + // 5 seats left (just what the user needs) + Transport transportA = Transport.builder() + .id(1) + .departureDate(LocalDateTime.of(2024, Month.MAY, 5, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(1).country("Poland").region("Gdańsk").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(15) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + // full capacity + Transport transportB = Transport.builder() + .id(2) + .departureDate(LocalDateTime.of(2024, Month.MAY, 10, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(5).country("Poland").region("Wrocław").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(10) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + // 3 seats left (not enough for the user) + Transport transportC = Transport.builder() + .id(3) + .departureDate(LocalDateTime.of(2024, Month.MAY, 10, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(5).country("Poland").region("Wrocław").build()) + .arrivalAt(Location.builder().id(2).country("Tunezja").region("Tunis").build()) + .build()) + .capacity(13) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + // 30 seats left (more than the user needs) + Transport transportD = Transport.builder() + .id(4) + .departureDate(LocalDateTime.of(2024, Month.MAY, 10, 12, 0, 0)) + .course(TransportCourse.builder() + .type(TransportType.PLANE) + .departureFrom(Location.builder().id(1).country("Poland").region("Gdańsk").build()) + .arrivalAt(Location.builder().id(10).country("Egipt").region("Kair").build()) + .build()) + .capacity(40) + .pricePerAdult(200.0f) + .transportReservations(List.of( + TransportReservation.builder().id(1).numberOfSeats(5).build(), + TransportReservation.builder().id(2).numberOfSeats(2).build(), + TransportReservation.builder().id(3).numberOfSeats(3).build())) + .build(); + + when(transportRepository.findAll()).thenReturn(List.of(transportA, transportB, transportC, transportD)); + + // Act + GetTransportsBySearchQueryResponseDto responseDto = transportsService.getTransportsBySearchQuery(requestDto); + + // Assert + assertEquals(2, responseDto.getTransportDtoList().size()); + assertEquals(1, responseDto.getTransportDtoList().getFirst().getIdTransport()); + assertEquals(4, responseDto.getTransportDtoList().getLast().getIdTransport()); + + } }