Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
#129 Feat 라이더 위치정보 조회기능 (#186)
Browse files Browse the repository at this point in the history
* Feat : 위치정보 조회 기능

* Test : add more tests
  • Loading branch information
JooHyukKim authored Feb 3, 2022
1 parent eb93391 commit bf00596
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 31 deletions.
3 changes: 0 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@
- (= Microservices && interconnection)


- **트래픽 최저점시간 대비 100배까지 증가하는 점심, 저녁 피크타임 트래픽 스파이크를 어떻게 해결할까요?**
- (= Scaling && 테스트 시나리오)

- **트래픽 최저점시간 대비 100배까지 증가하는 점심, 저녁 피크타임 트래픽 스파이크를 어떻게 해결할까요?**
- (= Scaling && 테스트 시나리오)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.inbobwetrust.domain;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDateTime;
import java.util.Objects;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -25,17 +17,27 @@
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.util.UriComponentsBuilder;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import reactor.test.StepVerifier;

import java.time.LocalDateTime;
import java.util.Objects;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@AutoConfigureWireMock(port = 0)
@Testcontainers
public class RiderLocationWriteTest {

@Autowired WebTestClient testClient;

@SpyBean RiderLocationRepository locationRepository;
Expand Down Expand Up @@ -94,7 +96,7 @@ void updateLocation_test() throws JsonProcessingException {
.expectBody(Boolean.class)
.isEqualTo(false);
// then
verify(locationService, times(1)).setIfPresent(any(RiderLocation.class));
verify(locationService, times(1)).tryPutOperation(any(RiderLocation.class));
verify(locationRepository, times(1)).setIfPresent(riderLocation);
verify(deliveryRepository, times(1)).isPickedUp(riderLocation.getDeliveryId());
}
Expand Down Expand Up @@ -125,7 +127,7 @@ void updateLocation_test_success() throws JsonProcessingException {
// then
var cache = locationRepository.findAll().collectList().block();
System.out.println(cache);
verify(locationService, times(1)).setIfPresent(any(RiderLocation.class));
verify(locationService, times(1)).tryPutOperation(any(RiderLocation.class));
verify(locationRepository, times(1)).setIfPresent(riderLocation);
verify(deliveryRepository, times(1)).isPickedUp(riderLocation.getDeliveryId());
}
Expand All @@ -152,8 +154,36 @@ void updateLocation_test_success2() throws JsonProcessingException {

// then
StepVerifier.create(locationRepository.findAll()).expectNext(riderLocation).verifyComplete();
verify(locationService, times(1)).setIfPresent(any(RiderLocation.class));
verify(locationService, times(1)).tryPutOperation(any(RiderLocation.class));
verify(locationRepository, times(1)).setIfPresent(riderLocation);
verify(deliveryRepository, times(0)).isPickedUp(any());
}

@Test
@DisplayName("라이더 위치 조회 테스트")
void getLocationTest() {
// given
var deliveryId = LocalDateTime.now().toString();
var riderLocation = new RiderLocation(deliveryId, deliveryId, 180.0f, 180f);
var saved = locationRepository.setIfAbsent(riderLocation).block();
assertEquals(true, saved);
var uri =
UriComponentsBuilder.fromUriString("/rider/location")
.queryParam("deliveryId", riderLocation.getDeliveryId())
.buildAndExpand()
.toUri();
// when
var getLocationResult =
testClient
.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk()
.expectBody(RiderLocation.class)
.returnResult()
.getResponseBody();
// then
Assertions.assertEquals(riderLocation, getLocationResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ public class RiderLocationController {

@PutMapping
public Mono<Boolean> updateLocationPost(@RequestBody RiderLocation location) {
return locationService.setIfPresent(location);
return locationService.tryPutOperation(location);
}

@PostMapping
public Mono<Boolean> updateLocationPut(@RequestBody RiderLocation location) {
return locationService.setIfPresent(location);
return locationService.tryPutOperation(location);
}

@GetMapping
public Mono<RiderLocation> getLocation(@RequestParam(name = "deliveryId") String deliveryId){
return locationService.getLocation(deliveryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface RiderLocationRepository {
Flux<RiderLocation> findAll();

Flux<Boolean> deleteAll();

Mono<RiderLocation> getLocation(String deliveryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public Flux<RiderLocation> findAll() {
public Flux<Boolean> deleteAll() {
return locationOperations.keys("*").flatMap(locationOperations.opsForValue()::delete);
}

@Override
public Mono<RiderLocation> getLocation(String deliveryId) {
return locationOperations.opsForValue().get(deliveryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public class RiderLocationService {
private final RiderLocationRepository repository;
private final DeliveryRepository deliveryRepository;

public Mono<Boolean> setIfPresent(RiderLocation location) {
public Mono<Boolean> tryPutOperation(RiderLocation location) {
return repository.setIfPresent(location).flatMap(isSaved -> orElseSetNew(isSaved, location));
}


private Mono<Boolean> orElseSetNew(Boolean isSaved, RiderLocation location) {
return isSaved ? Mono.just(true) : doSetNew(location);
}
Expand All @@ -29,7 +30,12 @@ private Mono<Boolean> setIfPickedUp(Boolean isPickedUp, RiderLocation location)
return isPickedUp ? repository.setIfAbsent(location) : Mono.just(false);
}


public Flux<RiderLocation> findAll() {
return repository.findAll();
}

public Mono<RiderLocation> getLocation(String deliveryId) {
return repository.getLocation(deliveryId);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.inbobwetrust.domain;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@WebFluxTest(RiderLocationController.class)
@AutoConfigureWebTestClient
class RiderLocationControllerTest {
Expand All @@ -27,7 +29,7 @@ void updateLocationPut_test() {
// given
var location = new RiderLocation("rider-1234", "delivery-1234", 23.0f, 190f);
// when
when(riderLocationService.setIfPresent(any(RiderLocation.class)))
when(riderLocationService.tryPutOperation(any(RiderLocation.class)))
.thenReturn(Mono.just(Boolean.TRUE));
var result =
testClient
Expand All @@ -48,7 +50,7 @@ void updateLocationPost_test() {
// given
var location = new RiderLocation("rider-1234", "delivery-1234", 23.0f, 190f);
// when
when(riderLocationService.setIfPresent(any(RiderLocation.class)))
when(riderLocationService.tryPutOperation(any(RiderLocation.class)))
.thenReturn(Mono.just(Boolean.TRUE));
var result =
testClient
Expand All @@ -63,4 +65,31 @@ void updateLocationPost_test() {
// then
StepVerifier.create(result).expectNextMatches(Boolean::booleanValue).verifyComplete();
}

@Test
void getLocation_test() {
// given
var location = new RiderLocation("rider-1234", "delivery-1234", 23.0f, 190f);
var uri =
UriComponentsBuilder.fromUriString(riderLocationMapping)
.queryParam("deliveryId", location.getDeliveryId())
.buildAndExpand()
.toUriString();

// when
when(riderLocationService.getLocation(location.getDeliveryId())).thenReturn(Mono.just(location));

var result =
testClient
.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk()
.expectBody(RiderLocation.class)
.returnResult()
.getResponseBody();
// then
assertEquals(location, result);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.inbobwetrust.domain;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -18,6 +13,12 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.List;
import java.util.stream.Stream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class RiderLocationServiceTest {

Expand Down Expand Up @@ -47,13 +48,12 @@ void setIfPresent_fail_does_not_exist(
if (!canSetIfPresent) {
when(deliveryRepository.isPickedUp(anyString())).thenReturn(Mono.just(isPickedUp));
}

if (isPickedUp && !canSetIfPresent) {
when(locationRepository.setIfAbsent(any(RiderLocation.class)))
.thenReturn(Mono.just(canSetIfAbsent));
}
// then
var resultStream = locationService.setIfPresent(riderLocation);
var resultStream = locationService.tryPutOperation(riderLocation);

StepVerifier.create(resultStream).expectNextMatches(res -> res == finalResult).verifyComplete();

Expand Down Expand Up @@ -87,4 +87,15 @@ void findAllTest() {
.expectNext(locations.get(0), locations.get(1))
.verifyComplete();
}

@Test
void getLocationTest() {
// given
var location = new RiderLocation("rider-12222", "delivery-12222", 23.0f, Float.MIN_VALUE);
// when
when(locationRepository.getLocation(location.getDeliveryId())).thenReturn(Mono.just(location));
var stream = locationService.getLocation(location.getDeliveryId());
// then
StepVerifier.create(stream).expectNext(location).verifyComplete();
}
}

0 comments on commit bf00596

Please sign in to comment.