Skip to content

Commit

Permalink
Merge pull request #55 from f-lab-edu/feature/54-testdata
Browse files Browse the repository at this point in the history
[#54] 테스트용 데이터 생성
  • Loading branch information
hoa0217 authored Mar 20, 2024
2 parents 498a288 + 4641bf1 commit 0555fc9
Show file tree
Hide file tree
Showing 22 changed files with 634 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public ResponseEntity<String> handleNotFoundEntityException(NotFoundEntityExcept
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(EmptyResponseException.class)
public ResponseEntity<String> handleEmptyResponseException(EmptyResponseException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(InvalidTimeRangeException.class)
public ResponseEntity<String> handleInvalidTimeRangeException(InvalidTimeRangeException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.modoospace.common.exception;

public class EmptyResponseException extends RuntimeException {
public EmptyResponseException(String response, String identifier) {
super(response+"("+identifier+")응답 값이 비었습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests(request -> request
.antMatchers(HttpMethod.GET, "/", "/error", "/api/v1/spaces/**",
"/api/v1/spaces/*/facilities/**",
"/api/v1/test/**",
"/api/v1/facilities/*/schedules/**",
"/api/v1/visitors/reservations/facilities/*/availability/**").permitAll()
.antMatchers(HttpMethod.POST, "/api/v1/alarms/send/**").permitAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.modoospace.mockData.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.modoospace.common.exception.EmptyResponseException;
import com.modoospace.config.auth.LoginEmail;
import com.modoospace.mockData.controller.dto.MockAddressResponse;
import com.modoospace.mockData.controller.dto.MockSpaceResponse;
import com.modoospace.mockData.service.MockDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/mock-data")
@Slf4j
public class MockDataController {

private final ObjectMapper objectMapper = new ObjectMapper();
private final HttpClient client = HttpClient.newHttpClient();

private final MockDataService mockDataService;

@Value("${spring.kakao.apikey}")
private String key;

@GetMapping("/space/{spaceId}")
public ResponseEntity<MockSpaceResponse> getSpace(@PathVariable String spaceId) throws IOException, InterruptedException {
return ResponseEntity.ok(getMockSpace(spaceId));
}

private MockSpaceResponse getMockSpace(String spaceId) throws IOException, InterruptedException {
HttpRequest spaceRequest = HttpRequest.newBuilder()
.uri(URI.create("https://new-api.spacecloud.kr/spaces/" + spaceId))
.build();
HttpResponse<String> httpSpaceResponse = client.send(spaceRequest, HttpResponse.BodyHandlers.ofString());
if (isResponseSpaceEmpty(httpSpaceResponse.body())) {
throw new EmptyResponseException("Space", spaceId);
}
return objectMapper.readValue(httpSpaceResponse.body(), MockSpaceResponse.class);
}

private boolean isResponseSpaceEmpty(String responseBody) {
return "{}".equals(responseBody.trim());
}

@GetMapping("/address")
public ResponseEntity<MockAddressResponse> getAddress(String address) throws IOException, InterruptedException {
return ResponseEntity.ok(getMockAddress(address));
}

private MockAddressResponse getMockAddress(String address) throws IOException, InterruptedException {
String encodedAddress = URLEncoder.encode(address, StandardCharsets.UTF_8);
HttpRequest addressRequest = HttpRequest.newBuilder()
.uri(URI.create("https://dapi.kakao.com/v2/local/search/address?query=" + encodedAddress))
.header("Authorization", "KakaoAK " + key)
.build();
HttpResponse<String> httpAddressResponse = client.send(addressRequest, HttpResponse.BodyHandlers.ofString());
MockAddressResponse addressResponse = objectMapper.readValue(httpAddressResponse.body(), MockAddressResponse.class);
if (addressResponse.getDocuments().isEmpty()) {
throw new EmptyResponseException("Address", address);
}
return addressResponse;
}

@PostMapping("/space/{spaceId}")
public ResponseEntity<Void> saveSpace(@PathVariable String spaceId, @LoginEmail String email) throws IOException, InterruptedException {
MockSpaceResponse spaceResponse = getMockSpace(spaceId);
MockAddressResponse addressResponse = getMockAddress(spaceResponse.getLocation().getAddress());
Long entitySpaceId = mockDataService.saveEntity(spaceResponse, addressResponse, email);
return ResponseEntity.created(URI.create("/api/v1/spaces/" + entitySpaceId)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.modoospace.mockData.controller.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.modoospace.mockData.controller.dto.address.Document;
import com.modoospace.space.domain.Address;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class MockAddressResponse {

List<Document> documents = new ArrayList<>();

public Address toAddress(String detailAddress) {
Document document = documents.get(0);
return Address.builder()
.depthFirst(document.getAddress().getDepthFirst())
.depthSecond(document.getAddress().getDepthSecond())
.depthThird(document.getAddress().getDepthThird())
.addressNo(document.getAddress().getFullAddressNo())
.roadName(document.getRoadAddress().getRoadName())
.buildingNo(document.getRoadAddress().getFullBuildingNo())
.detailAddress(detailAddress)
.x(document.getX())
.y(document.getY())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.modoospace.mockData.controller.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.modoospace.member.domain.Member;
import com.modoospace.mockData.controller.dto.space.*;
import com.modoospace.space.domain.*;
import lombok.Getter;
import lombok.Setter;

import java.time.DayOfWeek;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class MockSpaceResponse {

@JsonProperty("products")
private List<FacilityResponse> facilityResponses = new ArrayList<>();

@JsonProperty("info")
private SpaceInfo spaceInfo;

private Location location;

@JsonProperty("break_days")
private List<BreakDay> breakDays = new ArrayList<>();

@JsonProperty("break_holidays")
private List<BreakHoliday> breakHolidays = new ArrayList<>();

@JsonProperty("break_times")
private List<BreakTime> breakTimes = new ArrayList<>();

public String getCategoryName() {
return facilityResponses.stream()
.findFirst()
.flatMap(p -> p.getCategories().stream().findFirst())
.map(FacilityCategory::getName).orElse("스터디룸");
}

public Space toSpace(Address address, Category category, Member member) {
return Space.builder()
.name(spaceInfo.getName())
.description(spaceInfo.getDescription())
.address(address)
.category(category)
.host(member)
.build();
}

public WeekdaySettings getWeekdaySettings() {
List<WeekdaySetting> weekdaySettings = new ArrayList<>(Arrays.asList(
new WeekdaySetting(DayOfWeek.MONDAY),
new WeekdaySetting(DayOfWeek.TUESDAY),
new WeekdaySetting(DayOfWeek.WEDNESDAY),
new WeekdaySetting(DayOfWeek.THURSDAY),
new WeekdaySetting(DayOfWeek.FRIDAY))
);

if (breakDays.isEmpty() && breakHolidays.isEmpty()) {
weekdaySettings.add(new WeekdaySetting(DayOfWeek.SATURDAY));
weekdaySettings.add(new WeekdaySetting(DayOfWeek.SUNDAY));
}

return new WeekdaySettings(weekdaySettings);
}

public TimeSettings getTimeSettings() {
List<TimeSetting> timeSettings = breakTimes.stream()
.flatMap(breakTime -> makeTimeRangeFromBreakTime(breakTime).stream())
.map(TimeSetting::new)
.collect(Collectors.toList());

return new TimeSettings(timeSettings);
}

// end 10 ~ start 2
private static List<TimeRange> makeTimeRangeFromBreakTime(BreakTime breakTime) {
List<TimeRange> list = new ArrayList<>();
if (breakTime.getEndHour() > breakTime.getStartHour()) {
list.add(new TimeRange(breakTime.getEndHour(), 24));
list.add(new TimeRange(0, breakTime.getStartHour()));
} else {
list.add(new TimeRange(breakTime.getEndHour(), breakTime.getStartHour()));
}
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.modoospace.mockData.controller.dto.address;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Document {

JibunAddress address;

@JsonProperty("road_address")
RoadAddress roadAddress;

String x;
String y;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.modoospace.mockData.controller.dto.address;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class JibunAddress {

@JsonProperty("region_1depth_name")
String depthFirst;

@JsonProperty("region_2depth_name")
String depthSecond;

@JsonProperty("region_3depth_name")
String depthThird;

@JsonProperty("main_address_no")
String addressNo;

@JsonProperty("sub_address_no")
String subAddressNo;

public String getFullAddressNo() {
return subAddressNo == null || subAddressNo.isBlank() ? addressNo : addressNo + "-" + subAddressNo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.modoospace.mockData.controller.dto.address;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class RoadAddress {

@JsonProperty("road_name")
String roadName;

@JsonProperty("main_building_no")
String buildingNo;

@JsonProperty("sub_building_no")
String subBuildingNo;

public String getFullBuildingNo() {
return subBuildingNo == null || subBuildingNo.isBlank() ? buildingNo : buildingNo + "-" + subBuildingNo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.modoospace.mockData.controller.dto.space;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class BreakDay {

@JsonProperty("BRK_DAY_TP_CD")
String breakDayCd;

@JsonProperty("DAYW_CD")
String dayCd;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.modoospace.mockData.controller.dto.space;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class BreakHoliday {

@JsonProperty("BRK_DAY_TP_CD")
String breakDayCd;

@JsonProperty("DAYW_CD")
String dayCd;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.modoospace.mockData.controller.dto.space;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class BreakTime {

@JsonProperty("start_time")
Integer startHour;

@JsonProperty("end_time")
Integer endHour;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.modoospace.mockData.controller.dto.space;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class FacilityCategory {

String name;
}
Loading

0 comments on commit 0555fc9

Please sign in to comment.