Skip to content

Commit

Permalink
Task 26 : Revise classes with Builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Sep 29, 2024
1 parent e10ec1c commit bc3c350
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,31 @@ public class DataLoader {
public CommandLineRunner loadInitialData() {
return args -> {
List<StoreEntity> stores = Arrays.asList(
new StoreEntity("Ataşehir MMM Migros", 40.9923307, 29.1244229),
new StoreEntity("Novada MMM Migros", 40.986106, 29.1161293),
new StoreEntity("Beylikdüzü 5M Migros", 41.0066851, 28.6552262),
new StoreEntity("Ortaköy MMM Migros", 41.055783, 29.0210292),
new StoreEntity("Caddebostan MMM Migros", 40.9632463, 29.0630908)
StoreEntity.builder()
.name("Ataşehir MMM Migros")
.lat(40.9923307)
.lng(29.1244229)
.build(),
StoreEntity.builder()
.name("Novada MMM Migros")
.lat(40.986106)
.lng(29.1161293)
.build(),
StoreEntity.builder()
.name("Beylikdüzü 5M Migros")
.lat(41.0066851)
.lng(28.6552262)
.build(),
StoreEntity.builder()
.name("Ortaköy MMM Migros")
.lat(41.055783)
.lng(29.0210292)
.build(),
StoreEntity.builder()
.name("Caddebostan MMM Migros")
.lat(40.9632463)
.lng(29.0630908)
.build()
);
storeRepository.saveAll(stores);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class LogCourierLocationRequest {
private Double lng;

@JsonFormat(pattern = "dd/MM/yyyy HH:mm")
@FutureOrPresent(message = "Timestamp must be after the store creation time")
private LocalDateTime timestamp;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.casestudy.migroscouriertracking.courier.model.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.time.LocalDateTime;
import java.util.UUID;
Expand All @@ -16,6 +14,8 @@
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CourierEntity {

@Id
Expand All @@ -38,42 +38,5 @@ public class CourierEntity {
@Column(nullable = false)
private LocalDateTime timestamp;

/**
* Constructs a new CourierEntity with the specified parameters.
*
* @param courierId the ID of the courier
* @param lat the latitude of the courier's location
* @param lng the longitude of the courier's location
* @param storeName the name of the store associated with the courier's location
* @param timestamp the timestamp of the location record
*/
public CourierEntity(String courierId, double lat, double lng,
String storeName, LocalDateTime timestamp) {
this.courierId = courierId;
this.lat = lat;
this.lng = lng;
this.storeName = storeName;
this.timestamp = timestamp;
}

/**
* Constructs a new CourierEntity with the specified parameters including an ID.
*
* @param id the unique identifier for the courier
* @param courierId the ID of the courier
* @param lat the latitude of the courier's location
* @param lng the longitude of the courier's location
* @param name the name of the store associated with the courier's location
* @param timestamp the timestamp of the location record
*/
public CourierEntity(String id, String courierId, double lat, double lng, String name, LocalDateTime timestamp) {
this.id=id;
this.courierId = courierId;
this.lat = lat;
this.lng = lng;
this.storeName = name;
this.timestamp = timestamp;
}

}

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.casestudy.migroscouriertracking.courier.model.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.time.LocalDateTime;
import java.util.UUID;
Expand All @@ -16,6 +14,8 @@
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class StoreEntity {

@Id
Expand All @@ -32,36 +32,9 @@ public class StoreEntity {
@Column(nullable = false)
private Double lng;

@Builder.Default
@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt = LocalDateTime.now();

/**
* Constructs a new StoreEntity with the specified parameters.
*
* @param name the name of the store
* @param lat the latitude of the store's location
* @param lng the longitude of the store's location
*/
public StoreEntity(String name, Double lat, Double lng) {
this.name = name;
this.lat = lat;
this.lng = lng;
}

/**
* Constructs a new StoreEntity with the specified parameters including creation time.
*
* @param name the name of the store
* @param lat the latitude of the store's location
* @param lng the longitude of the store's location
* @param createdAt the timestamp when the store was created
*/
public StoreEntity(String name, double lat, double lng, LocalDateTime createdAt) {
this.name = name;
this.lat = lat;
this.lng = lng;
this.createdAt=createdAt;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public void logCourierLocation(LogCourierLocationRequest logRequest) {

CourierEntity lastTravel = findLastTravelEntry(courierId, store.getName(), timestamp);
if (lastTravel == null || DistanceUtils.isMoreThanOneMinuteAgo(lastTravel.getTimestamp(), timestamp)) {
CourierEntity courier = new CourierEntity(courierId, lat, lng, store.getName(), timestamp);
CourierEntity courier = CourierEntity.builder()
.courierId(courierId)
.lat(lat)
.lng(lng)
.storeName(store.getName())
.timestamp(timestamp)
.build();
courierRepository.save(courier);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
Expand All @@ -37,12 +38,12 @@ class CourierControllerTest extends AbstractRestControllerTest {
public void logCourierLocation_shouldReturnSuccessMessage() throws Exception {

// Given
LogCourierLocationRequest logRequest = new LogCourierLocationRequest(
"123e4567-e89b-12d3-a456-426614174000",
37.7749,
-122.4194,
LocalDateTime.now()
);
LogCourierLocationRequest logRequest = LogCourierLocationRequest.builder()
.courierId(UUID.randomUUID().toString())
.lat(37.7749)
.lng(-122.4194)
.timestamp(LocalDateTime.now().plusMinutes(1))
.build();

// When
doNothing().when(courierService).logCourierLocation(any());
Expand All @@ -64,10 +65,25 @@ public void logCourierLocation_shouldReturnSuccessMessage() throws Exception {
public void getPastTravels_shouldReturnListOfTravels() throws Exception {

// Given
String courierId = "123e4567-e89b-12d3-a456-426614174000";
String courierId = UUID.randomUUID().toString();

List<Courier> travels = List.of(
new Courier("1", courierId, 37.7749, -122.4194, "store1", LocalDateTime.now()),
new Courier("2", courierId, 37.7750, -122.4183, "store1", LocalDateTime.now().minusHours(1))
Courier.builder()
.id(UUID.randomUUID().toString())
.courierId(courierId)
.lat(37.7749)
.lng(-122.4194)
.storeName("store1")
.timestamp(LocalDateTime.now())
.build(),
Courier.builder()
.id(UUID.randomUUID().toString())
.courierId(courierId)
.lat(37.7750)
.lng(-122.4183)
.storeName("store1")
.timestamp(LocalDateTime.now().minusHours(1))
.build()
);

List<CourierResponse> response = courierToCourierResponseMapper.map(travels);
Expand All @@ -92,16 +108,22 @@ public void getPastTravels_shouldReturnListOfTravels() throws Exception {
public void getTravelsByCourierIdStoreNameAndTimeRange_shouldReturnFilteredTravels() throws Exception {

// Given
TravelQueryRequest request = new TravelQueryRequest(
"123e4567-e89b-12d3-a456-426614174000",
"Ataşehir MMM Migros",
LocalDateTime.parse("28/09/2024 01:58", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")),
LocalDateTime.parse("28/09/2024 02:00", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"))
);
TravelQueryRequest request = TravelQueryRequest.builder()
.courierId(UUID.randomUUID().toString())
.storeName("Ataşehir MMM Migros")
.start(LocalDateTime.parse("28/09/2024 01:58", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")))
.end(LocalDateTime.parse("28/09/2024 02:00", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")))
.build();

List<Courier> travels = List.of(
new Courier("1", request.getCourierId(), 37.7749, -122.4194, request.getStoreName(),
LocalDateTime.parse("28/09/2024 01:59", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")))
Courier.builder()
.id(UUID.randomUUID().toString())
.courierId(request.getCourierId())
.lat(37.7749)
.lng(-122.4194)
.storeName(request.getStoreName())
.timestamp(LocalDateTime.parse("28/09/2024 01:59", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")))
.build()
);

List<CourierResponse> response = courierToCourierResponseMapper.map(travels);
Expand All @@ -127,7 +149,7 @@ public void getTravelsByCourierIdStoreNameAndTimeRange_shouldReturnFilteredTrave
public void getTotalTravelDistance_shouldReturnTotalDistanceTraveledByCourier() throws Exception {

// Given
String courierId = "123e4567-e89b-12d3-a456-426614174000";
String courierId = UUID.randomUUID().toString();
double totalDistance = 0.097; // Example distance in kilometers
String formattedDistance = String.format("%.2f km", totalDistance);

Expand Down
Loading

0 comments on commit bc3c350

Please sign in to comment.