Skip to content

Commit

Permalink
feat: w3w open api component (#5)
Browse files Browse the repository at this point in the history
* feat: w3w open api component

- coordinates, locale -> w3wWords
- w3wWords -> coordinates

* fix: test 코드 일시적으로 비활성화

- 환경변수 설정이 필요함
- 추후 github action workflow 수정 예정

* code-review: w3w 컴포넌트 인터페이스 리턴 타입 변경

- W3wWordsResponse
- W3wCoordinatesResponse
  • Loading branch information
rolroralra authored Jan 31, 2024
1 parent 3837936 commit 19ac696
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 4 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ buildscript {

guavaVersion = '31.1-jre'
apacheCommonsLang3Version = '3.12.0'

javaFakerVersion = '1.0.2'
}

repositories {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rootProject.name = 'java-sprintboot-subprojects'

include 'api'
include 'w3w'

rootProject.children.forEach { project ->
project.projectDir = file("subprojects/${project.name}")
Expand Down
4 changes: 4 additions & 0 deletions subprojects/api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.h2database:h2'



compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.poemfoot.api;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApiApplicationTests {

@Test
Expand Down
17 changes: 17 additions & 0 deletions subprojects/w3w/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ext {
w3wJavaWrapperVersion = '3.1.19'
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation "com.what3words:w3w-java-wrapper:${w3wJavaWrapperVersion}"

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}
11 changes: 11 additions & 0 deletions subprojects/w3w/src/main/java/com/poemfoot/w3w/W3wApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.poemfoot.w3w;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class W3wApplication {

public static void main(String[] args) {
// Do Nothing
}
}
11 changes: 11 additions & 0 deletions subprojects/w3w/src/main/java/com/poemfoot/w3w/W3wProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.poemfoot.w3w;

import com.poemfoot.w3w.dto.W3wCoordinatesResponse;
import com.poemfoot.w3w.dto.W3wWordsResponse;
import java.util.Locale;

public interface W3wProvider {
W3wWordsResponse getWords(double latitude, double longitude, Locale locale);

W3wCoordinatesResponse getCoordinates(String word1, String word2, String word3);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.poemfoot.w3w.config;

import com.what3words.javawrapper.What3WordsV3;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class W3wConfig {
@Value("${w3w.api.key}")
private String apiKey;

@Bean
public What3WordsV3 what3WordsV3() {
return new What3WordsV3(apiKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.poemfoot.w3w.dto;

import com.what3words.javawrapper.response.ConvertToCoordinates;
import com.what3words.javawrapper.response.Coordinates;
import com.what3words.javawrapper.response.Square;

public record W3wCoordinatesResponse(
String country,
Square square,
String nearestPlace,
Coordinates coordinates,
W3wWords words,
String language,
String locale,
String map
){
public static W3wCoordinatesResponse of(ConvertToCoordinates convertToCoordinates) {
return new W3wCoordinatesResponse(
convertToCoordinates.getCountry(),
convertToCoordinates.getSquare(),
convertToCoordinates.getNearestPlace(),
convertToCoordinates.getCoordinates(),
W3wWords.of(convertToCoordinates.getWords()),
convertToCoordinates.getLanguage(),
convertToCoordinates.getLocale(),
convertToCoordinates.getMap()
);
}
}
22 changes: 22 additions & 0 deletions subprojects/w3w/src/main/java/com/poemfoot/w3w/dto/W3wWords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.poemfoot.w3w.dto;

public record W3wWords (
String word1,
String word2,
String word3
){
public static W3wWords of(String compositeWords) {
String[] words = compositeWords.split("\\.");

if (words.length != 3) {
throw new IllegalArgumentException("Invalid composite words: " + compositeWords);
}

return new W3wWords(words[0], words[1], words[2]);
}

@Override
public String toString() {
return String.join(".", word1, word2, word3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.poemfoot.w3w.dto;

import com.what3words.javawrapper.response.ConvertTo3WA;
import com.what3words.javawrapper.response.Coordinates;
import com.what3words.javawrapper.response.Square;

public record W3wWordsResponse(
String country,
Square square,
String nearestPlace,
Coordinates coordinates,
W3wWords words,
String language,
String locale,
String map
){
public static W3wWordsResponse of(ConvertTo3WA convertTo3WA) {
return new W3wWordsResponse(
convertTo3WA.getCountry(),
convertTo3WA.getSquare(),
convertTo3WA.getNearestPlace(),
convertTo3WA.getCoordinates(),
W3wWords.of(convertTo3WA.getWords()),
convertTo3WA.getLanguage(),
convertTo3WA.getLocale(),
convertTo3WA.getMap()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.poemfoot.w3w.impl;

import com.poemfoot.w3w.W3wProvider;
import com.poemfoot.w3w.dto.W3wCoordinatesResponse;
import com.poemfoot.w3w.dto.W3wWordsResponse;
import com.what3words.javawrapper.What3WordsV3;
import com.what3words.javawrapper.request.Coordinates;
import com.what3words.javawrapper.response.ConvertTo3WA;
import com.what3words.javawrapper.response.ConvertToCoordinates;
import java.util.Locale;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class W3wProviderImpl implements W3wProvider {

private final What3WordsV3 w3wApi;

@Override
public W3wWordsResponse getWords(double latitude, double longitude, Locale locale) {
ConvertTo3WA convertTo3WA = w3wApi.convertTo3wa(new Coordinates(latitude, longitude))
.language(locale.getLanguage())
.execute();

return W3wWordsResponse.of(convertTo3WA);
}

@Override
public W3wCoordinatesResponse getCoordinates(String word1, String word2, String word3) {
String w3wWords = String.join(".", word1, word2, word3);

ConvertToCoordinates convertToCoordinates = w3wApi.convertToCoordinates(w3wWords)
.execute();

return W3wCoordinatesResponse.of(convertToCoordinates);
}
}
3 changes: 3 additions & 0 deletions subprojects/w3w/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
w3w:
api:
key: ${W3W_API_KEY}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.poemfoot.w3w;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import com.poemfoot.w3w.dto.W3wCoordinatesResponse;
import com.poemfoot.w3w.dto.W3wWords;
import com.poemfoot.w3w.dto.W3wWordsResponse;
import java.util.Locale;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@Disabled
@SpringBootTest
class W3wProviderTest {
@Autowired
private W3wProvider w3wProvider;

@ParameterizedTest(name = "getWords({0}, {1}, {2})")
@CsvSource(value = {"37.2,127.5,ko"}, delimiter = ',')
void getWords(double latitude, double longitude, Locale locale) {
// When
W3wWordsResponse result = w3wProvider.getWords(latitude, longitude, locale);

// Then
assertAll(
() -> assertThat(result).isNotNull(),
() -> assertThat(result).hasFieldOrProperty("words")
);

W3wWords w3wWords = result.words();

assertAll(
() -> assertThat(w3wWords).isNotNull(),
() -> assertThat(w3wWords).hasNoNullFieldsOrProperties()
);

assertAll(
() -> assertThat(w3wWords.word1()).isNotBlank(),
() -> assertThat(w3wWords.word2()).isNotBlank(),
() -> assertThat(w3wWords.word3()).isNotBlank()
);
}

@ParameterizedTest
@CsvSource(value = {"좋겠다.휴게실.이성"}, delimiter = '.')
void getCoordinates(String word1, String word2, String word3) {
W3wCoordinatesResponse result = w3wProvider.getCoordinates(word1, word2, word3);

assertAll(
() -> assertThat(result).isNotNull(),
() -> assertThat(result).hasFieldOrProperty("coordinates")
);

assertAll(
() -> assertThat(result.coordinates()).isNotNull(),
() -> assertThat(result.coordinates()).hasFieldOrProperty("lat"),
() -> assertThat(result.coordinates()).hasFieldOrProperty("lng")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.poemfoot.w3w.dto;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class W3wWordsTest {

@ParameterizedTest
@CsvSource(value = {"word1,word2,word3"}, delimiter = ',')
void testToString(String word1, String word2, String word3) {
// Given
W3wWords w3wWords = new W3wWords(word1, word2, word3);

// Expect
assertAll(
() -> assertThat(w3wWords).isNotNull(),
() -> assertThat(w3wWords.toString()).isEqualTo(String.join(".", word1, word2, word3))
);
}
}

0 comments on commit 19ac696

Please sign in to comment.