-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from makevook/issue/60
chore: meilisearch testcontainer 추가
- Loading branch information
Showing
9 changed files
with
192 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
api/src/main/java/vook/server/api/devhelper/TestTermsLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package vook.server.api.devhelper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.stereotype.Component; | ||
import vook.server.api.model.demo.DemoTerm; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class TestTermsLoader { | ||
|
||
private final ResourceLoader resourceLoader; | ||
|
||
public List<DemoTerm> getTerms(String location) { | ||
try { | ||
// file로 바로 접근 할 경우, IDE에서는 접근 가능하나, jar로 패키징 후 실행 시에는 접근 불가능 | ||
// ref) https://velog.io/@haron/트러블슈팅-Spring-IDE-에서-되는데-배포하면-안-돼요 | ||
InputStream tsvFileInputStream = resourceLoader.getResource(location).getInputStream(); | ||
CsvReader tsvReader = new CsvReader("\t"); | ||
List<RawTerm> rawTerms = tsvReader.readValue(tsvFileInputStream, RawTerm.class); | ||
return toTerms(rawTerms); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
static List<DemoTerm> toTerms(List<RawTerm> rawTerms) { | ||
return rawTerms.stream() | ||
.map(RawTerm::toTerm) | ||
.toList(); | ||
} | ||
|
||
static class RawTerm { | ||
private String term; | ||
private String synonyms; | ||
private String meaning; | ||
|
||
public DemoTerm toTerm() { | ||
DemoTerm term = DemoTerm.forCreateOf(this.term, this.meaning); | ||
String[] synonymArray = this.synonyms.split("//n"); | ||
Arrays.stream(synonymArray) | ||
.map(String::trim) | ||
.forEach(term::addSynonym); | ||
return term; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
api/src/main/java/vook/server/api/outbound/search/MeilisearchProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package vook.server.api.outbound.search; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Setter | ||
@Getter | ||
@Component | ||
@ConfigurationProperties(prefix = "service.meilisearch") | ||
public class MeilisearchProperties { | ||
private String host; | ||
private String apiKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
api/src/test/java/vook/server/api/testhelper/MeilisearchContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package vook.server.api.testhelper; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
public class MeilisearchContainer extends GenericContainer<MeilisearchContainer> { | ||
|
||
private static final String DEFAULT_IMAGE_NAME = "getmeili/meilisearch:latest"; | ||
|
||
public MeilisearchContainer() { | ||
this(DEFAULT_IMAGE_NAME); | ||
} | ||
|
||
public MeilisearchContainer(String dockerImageName) { | ||
this(DockerImageName.parse(dockerImageName)); | ||
} | ||
|
||
public MeilisearchContainer(DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
this.addExposedPort(7700); | ||
this.addEnv("MEILI_MASTER_KEY", "aMasterKey"); | ||
} | ||
|
||
public String getMasterKey() { | ||
return this.getEnvMap().get("MEILI_MASTER_KEY"); | ||
} | ||
|
||
/** | ||
* Meilisearch에 접속하기 위한 URL을 반환한다. | ||
*/ | ||
public String getHostUrl() { | ||
return "http://" + getHost() + ":" + getMappedPort(7700); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
api/src/test/java/vook/server/api/web/routes/demo/DemoWebServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package vook.server.api.web.routes.demo; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import vook.server.api.app.demo.repo.DemoTermRepository; | ||
import vook.server.api.devhelper.TestTermsLoader; | ||
import vook.server.api.model.demo.DemoTerm; | ||
import vook.server.api.outbound.search.DemoTermSearchService; | ||
import vook.server.api.testhelper.ApiTest; | ||
import vook.server.api.web.routes.demo.reqres.SearchTermRequest; | ||
import vook.server.api.web.routes.demo.reqres.SearchTermResponse; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Transactional | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class DemoWebServiceTest extends ApiTest { | ||
|
||
@Autowired | ||
private DemoWebService demoWebService; | ||
|
||
@Autowired | ||
private TestTermsLoader testTermsLoader; | ||
@Autowired | ||
private DemoTermRepository demoTermRepository; | ||
@Autowired | ||
private DemoTermSearchService demoTermSearchService; | ||
|
||
@BeforeAll | ||
void beforeAll() { | ||
List<DemoTerm> terms = testTermsLoader.getTerms("classpath:init/개발.tsv"); | ||
demoTermRepository.saveAll(terms); | ||
demoTermSearchService.init(); | ||
demoTermSearchService.addTerms(terms); | ||
} | ||
|
||
@AfterAll | ||
void afterAll() { | ||
demoTermSearchService.clearAll(); | ||
} | ||
|
||
@Test | ||
void searchTerm() { | ||
SearchTermRequest searchTermRequest = new SearchTermRequest(); | ||
searchTermRequest.setQuery("하이브리드앱"); | ||
searchTermRequest.setWithFormat(false); | ||
searchTermRequest.setHighlightPreTag("<em>"); | ||
searchTermRequest.setHighlightPostTag("</em>"); | ||
|
||
SearchTermResponse searchTermResponse = demoWebService.searchTerm(searchTermRequest); | ||
|
||
assertThat(searchTermResponse).isNotNull(); | ||
assertThat(searchTermResponse.getQuery()).isEqualTo("하이브리드앱"); | ||
assertThat(searchTermResponse.getHits()).isNotEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters