Skip to content

Commit

Permalink
test: MockDataController ResponseEntity 적용
Browse files Browse the repository at this point in the history
Related: #54
  • Loading branch information
hoa0217 committed Mar 20, 2024
1 parent 5db6820 commit 4641bf1
Showing 1 changed file with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
package com.modoospace.data.controller;
package com.modoospace.mockData.controller;

import com.modoospace.AbstractIntegrationContainerBaseTest;
import com.modoospace.common.exception.EmptyResponseException;
import com.modoospace.data.controller.dto.AddressResponse;
import com.modoospace.data.controller.dto.SpaceResponse;
import com.modoospace.member.domain.Member;
import com.modoospace.member.domain.MemberRepository;
import com.modoospace.member.domain.Role;
import com.modoospace.space.controller.dto.space.SpaceDetailResponse;
import com.modoospace.mockData.controller.dto.MockAddressResponse;
import com.modoospace.mockData.controller.dto.MockSpaceResponse;
import com.modoospace.space.domain.Category;
import com.modoospace.space.domain.CategoryRepository;
import com.modoospace.space.domain.Space;
import com.modoospace.space.domain.SpaceRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.URI;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@Transactional
class DataControllerTest extends AbstractIntegrationContainerBaseTest {
class MockDataControllerTest extends AbstractIntegrationContainerBaseTest {

@Autowired
private DataController dataController;
private MockDataController mockDataController;

@Autowired
private MemberRepository memberRepository;

@Autowired
private SpaceRepository spaceRepository;

@Autowired
private CategoryRepository categoryRepository;

Expand All @@ -50,44 +56,57 @@ public void setUp() {
@DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 Exception을 던진다.")
@Test
public void getSpace_throwException_ifEmptyResponse() {
assertThatThrownBy(() -> dataController.getSpace("1"))
assertThatThrownBy(() -> mockDataController.getSpace("1"))
.isInstanceOf(EmptyResponseException.class);
}

@DisplayName("외부Api로 받아온 데이터를 반환한다.")
@Test
public void getSpace() throws IOException, InterruptedException {
SpaceResponse space = dataController.getSpace("58861");
MockSpaceResponse space = mockDataController.getSpace("58861").getBody();

assertThat(space).isNotNull();
assertThat(space.getSpaceInfo().getName()).isEqualTo("감성공간 아르떼부암 ");
}

@DisplayName("KakaoApi로 받아온 주소데이터가 비어있을 경우 Exception을 던진다.")
@Test
public void getAddress_throwException_ifEmptyResponse() {
assertThatThrownBy(() -> dataController.getAddress("address"))
assertThatThrownBy(() -> mockDataController.getAddress("address"))
.isInstanceOf(EmptyResponseException.class);
}

@DisplayName("KakaoApi로 받아온 데이터를 반환한다.")
@Test
public void getAddress() throws IOException, InterruptedException {
AddressResponse address = dataController.getAddress("서울 종로구 자하문로 254");
MockAddressResponse address = mockDataController.getAddress("서울 종로구 자하문로 254").getBody();

assertThat(address).isNotNull();
assertThat(address.getDocuments().get(0).getX()).isEqualTo("126.963964049851");
assertThat(address.getDocuments().get(0).getY()).isEqualTo("37.5969625614596");
}

@DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 변환하지 않고 Exception을 던진다.")
@Test
public void saveSpace_throwException_ifEmptyResponse() {
assertThatThrownBy(() -> dataController.saveSpace("2", member.getEmail()))
assertThatThrownBy(() -> mockDataController.saveSpace("2", member.getEmail()))
.isInstanceOf(EmptyResponseException.class);
}

@DisplayName("외부Api로 받아온 데이터를 모두스페이스 엔티티로 변환 후 저장하여 반환한다.")
@Test
public void saveSpace() throws IOException, InterruptedException {
SpaceDetailResponse space = dataController.saveSpace("58861", member.getEmail());
assertThat(space.getName()).isEqualTo("감성공간 아르떼부암 ");
assertThat(space.getFacilities()).hasSize(1);
URI location = mockDataController.saveSpace("58861", member.getEmail()).getHeaders().getLocation();
assertThat(location).isNotNull();

Optional<Space> space = spaceRepository.findById(extractId(location));
assertThat(space).isPresent();
assertThat(space.get().getName()).isEqualTo("감성공간 아르떼부암 ");
}

private Long extractId(URI location) {
String path = location.getPath();
String id = path.substring(path.lastIndexOf("/") + 1);
return Long.parseLong(id);
}
}

0 comments on commit 4641bf1

Please sign in to comment.