Skip to content

Commit

Permalink
[fix] conflict 해결 (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeyoung22 committed Aug 21, 2023
1 parent 45bd5c9 commit f8db27e
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static dev.tripdraw.exception.member.MemberExceptionType.MEMBER_NOT_FOUND;
import static dev.tripdraw.exception.trip.TripExceptionType.TRIP_NOT_FOUND;

import dev.tripdraw.application.draw.RouteImageGenerator;
import dev.tripdraw.domain.member.Member;
import dev.tripdraw.domain.member.MemberRepository;
import dev.tripdraw.domain.trip.Point;
Expand All @@ -25,6 +26,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static dev.tripdraw.exception.member.MemberExceptionType.MEMBER_NOT_FOUND;
import static dev.tripdraw.exception.trip.TripExceptionType.TRIP_NOT_FOUND;

@RequiredArgsConstructor
@Transactional
@Service
Expand Down
11 changes: 11 additions & 0 deletions backend/src/main/java/dev/tripdraw/domain/trip/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
import jakarta.persistence.Embeddable;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import static dev.tripdraw.exception.trip.TripExceptionType.POINT_NOT_FOUND;
import static dev.tripdraw.exception.trip.TripExceptionType.POINT_NOT_IN_TRIP;
import static jakarta.persistence.CascadeType.PERSIST;
import static jakarta.persistence.CascadeType.REMOVE;
import static jakarta.persistence.FetchType.LAZY;

@Accessors(fluent = true)
@Getter
@NoArgsConstructor(access = PROTECTED)
Expand Down Expand Up @@ -53,4 +60,8 @@ public List<Point> points() {
.filter(point -> !point.isDeleted())
.toList();
}

public RouteLength calculateRouteLength() {
return RouteLength.from(points);
}
}
49 changes: 49 additions & 0 deletions backend/src/main/java/dev/tripdraw/domain/trip/RouteLength.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dev.tripdraw.domain.trip;

import dev.tripdraw.exception.trip.TripException;

import java.util.List;
import java.util.stream.IntStream;

import static dev.tripdraw.exception.trip.TripExceptionType.ONE_OR_NO_POINT;
import static java.lang.Math.*;

public class RouteLength {

public static final double AVERAGE_DISTANCE_FOR_ONE_DEGREE_DIFFERENCE_IN_LATITUDE_ON_KOREA_REGION = 111.1;

private final Double length;

private RouteLength(List<Point> points) {
this.length = calculateLength(points);
}

public static RouteLength from(List<Point> points) {
return new RouteLength(points);
}

private Double calculateLength(List<Point> points) {
if (points.isEmpty() || points.size() == 1) {
throw new TripException(ONE_OR_NO_POINT);
}

return IntStream.range(0, points.size() - 1)
.mapToDouble(i -> distanceBetween(points.get(i), points.get(i + 1)))
.sum();
}

private Double distanceBetween(Point startPoint, Point endPoint) {
double theta = startPoint.longitude() - endPoint.longitude();
Double latitude1 = startPoint.latitude();
Double latitude2 = endPoint.latitude();

double unitDistance = sin(toRadians(latitude1)) * sin(toRadians(latitude2))
+ cos(toRadians(latitude1)) * cos(toRadians(latitude2)) * cos(toRadians(theta));

return toDegrees(acos(unitDistance)) * AVERAGE_DISTANCE_FOR_ONE_DEGREE_DIFFERENCE_IN_LATITUDE_ON_KOREA_REGION;
}

public String lengthInKm() {
return String.format("%.2f" + "km", length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.tripdraw.domain.trip;

import dev.tripdraw.exception.trip.TripException;
import dev.tripdraw.exception.trip.TripExceptionType;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class RouteLengthTest {

@Test
void 경로의_길이를_계산한다() {
// given
List<Point> points = List.of(new Point(1.1, 1.1, LocalDateTime.now()), new Point(2.1, 1.1, LocalDateTime.now()), new Point(2.1, 2.1, LocalDateTime.now()));

// when
RouteLength length = RouteLength.from(points);

// then
Assertions.assertThat(length.lengthInKm()).isEqualTo("222.13km");
}

@ParameterizedTest
@MethodSource("generateData")
void 경로에_위치정보가_하나이거나_없는_경우_예외를_발생시킨다(List<Point> points) {
// expect
Assertions.assertThatThrownBy(() -> RouteLength.from(points))
.isInstanceOf(TripException.class)
.hasMessage(TripExceptionType.ONE_OR_NO_POINT.message());
}

static Stream<List<Point>> generateData() {
return Stream.of(
new ArrayList<>(),
List.of(new Point(1.1, 1.1, LocalDateTime.now()))
);
}


}
51 changes: 44 additions & 7 deletions backend/src/test/java/dev/tripdraw/domain/trip/RouteTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package dev.tripdraw.domain.trip;

import static dev.tripdraw.exception.trip.TripExceptionType.POINT_ALREADY_DELETED;
import static dev.tripdraw.exception.trip.TripExceptionType.POINT_NOT_FOUND;
import static dev.tripdraw.exception.trip.TripExceptionType.POINT_NOT_IN_TRIP;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import dev.tripdraw.exception.trip.TripException;
import java.time.LocalDateTime;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static dev.tripdraw.exception.trip.TripExceptionType.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(ReplaceUnderscores.class)
class RouteTest {
Expand Down Expand Up @@ -118,5 +117,43 @@ class RouteTest {
// then
assertThat(route.points()).containsExactly(point2);
}

@Test
void 경로의_거리를_계산한다() {
// given
Route route = new Route();
route.add(new Point(1.1, 1.1, LocalDateTime.now()));
route.add(new Point(1.1, 2.1, LocalDateTime.now()));
route.add(new Point(2.1, 2.1, LocalDateTime.now()));

// when
RouteLength routeLength = route.calculateRouteLength();

// then
assertThat(routeLength.lengthInKm()).isEqualTo("222.18km");
}

@Test
void 경로에_위치정보가_존재하지_않으면_거리를_계산할_때_예외를_발생시킨다() {
// given
Route route = new Route();

// expect
assertThatThrownBy(route::calculateRouteLength)
.isInstanceOf(TripException.class)
.hasMessage(ONE_OR_NO_POINT.message());
}

@Test
void 경로에_위치정보가_하나만_존재하면_거리를_계산할_때_예외를_발생시킨다() {
// given
Route route = new Route();
route.add(new Point(1.1, 1.1, LocalDateTime.now()));

// expect
assertThatThrownBy(route::calculateRouteLength)
.isInstanceOf(TripException.class)
.hasMessage(ONE_OR_NO_POINT.message());
}
}

0 comments on commit f8db27e

Please sign in to comment.