-
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.
- Loading branch information
Showing
7 changed files
with
162 additions
and
55 deletions.
There are no files selected for viewing
92 changes: 73 additions & 19 deletions
92
src/test/java/ru/dankoy/korvotoanki/KorvoToAnkiApplicationExporterSyncTests.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 |
---|---|---|
@@ -1,34 +1,88 @@ | ||
package ru.dankoy.korvotoanki; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import ru.dankoy.korvotoanki.config.appprops.FilesProperties; | ||
import ru.dankoy.korvotoanki.core.service.converter.AnkiConverterService; | ||
import ru.dankoy.korvotoanki.core.service.exporter.ExporterServiceAnki; | ||
import ru.dankoy.korvotoanki.core.service.state.StateService; | ||
import ru.dankoy.korvotoanki.core.service.templatecreator.TemplateCreatorService; | ||
import ru.dankoy.korvotoanki.core.service.vocabulary.VocabularyService; | ||
|
||
@DisplayName("Test sync bean context ") | ||
@SpringBootTest | ||
@TestPropertySource( | ||
properties = { | ||
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration" | ||
}) | ||
@Import(ExporterServiceAnki.class) | ||
@TestPropertySource(properties = "korvo-to-anki.async=false") | ||
@DisplayName("Test sync exporter bean context ") | ||
class KorvoToAnkiApplicationExporterSyncTests { | ||
|
||
@Autowired ApplicationContext context; | ||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withInitializer( | ||
new ConditionEvaluationReportLoggingListener()) // to print out conditional config | ||
// report to log | ||
.withUserConfiguration(TestConfig.class) | ||
.withUserConfiguration(ExporterServiceAnki.class); | ||
|
||
@DisplayName("all sync exporter bean") | ||
@DisplayName("sync exporter bean exists") | ||
@Test | ||
void contextLoads() { | ||
void syncExporterServiceExists() { | ||
|
||
var exporterServiceAnki = context.getBean(ExporterServiceAnki.class); | ||
contextRunner | ||
.withPropertyValues("korvo-to-anki.async=false") | ||
.run( | ||
context -> | ||
assertAll(() -> assertThat(context).hasSingleBean(ExporterServiceAnki.class))); | ||
} | ||
|
||
@DisplayName("sync exporter bean not exists") | ||
@Test | ||
void syncExporterServiceNotExists() { | ||
|
||
contextRunner | ||
.withPropertyValues("korvo-to-anki.async=true") | ||
.run( | ||
context -> | ||
assertAll(() -> assertThat(context).doesNotHaveBean(ExporterServiceAnki.class))); | ||
} | ||
|
||
@Configuration // this annotation is not required here as the class is explicitly mentioned in | ||
// `withUserConfiguration` method | ||
protected static class TestConfig { | ||
@Bean | ||
public VocabularyService vocabularyService() { | ||
return Mockito.mock( | ||
VocabularyService.class); // this bean will be automatically autowired into tested beans | ||
} | ||
|
||
@Bean | ||
public AnkiConverterService ankiConverterService() { | ||
return Mockito.mock( | ||
AnkiConverterService | ||
.class); // this bean will be automatically autowired into tested beans | ||
} | ||
|
||
@Bean | ||
public TemplateCreatorService templateCreatorService() { | ||
return Mockito.mock( | ||
TemplateCreatorService | ||
.class); // this bean will be automatically autowired into tested beans | ||
} | ||
|
||
@Bean | ||
public FilesProperties filesProperties() { | ||
return Mockito.mock( | ||
FilesProperties.class); // this bean will be automatically autowired into tested beans | ||
} | ||
|
||
assertNotNull(exporterServiceAnki); | ||
@Bean | ||
public StateService stateService() { | ||
return Mockito.mock( | ||
StateService.class); // this bean will be automatically autowired into tested beans | ||
} | ||
} | ||
} |
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
93 changes: 74 additions & 19 deletions
93
src/test/java/ru/dankoy/korvotoanki/KorvoToAnkiApplicationWebClientTests.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 |
---|---|---|
@@ -1,37 +1,92 @@ | ||
package ru.dankoy.korvotoanki; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import ru.dankoy.korvotoanki.config.OkHttpConfig; | ||
import ru.dankoy.korvotoanki.config.appprops.AppProperties; | ||
import ru.dankoy.korvotoanki.config.appprops.DictionaryApiProperties; | ||
import ru.dankoy.korvotoanki.config.appprops.GoogleParamsProperties; | ||
import ru.dankoy.korvotoanki.core.service.dictionaryapi.DictionaryServiceOkHttp; | ||
import ru.dankoy.korvotoanki.core.service.googletrans.GoogleTranslatorOkHttp; | ||
import ru.dankoy.korvotoanki.core.service.googletrans.parser.GoogleTranslatorParser; | ||
|
||
@DisplayName("Test okhttp beans context ") | ||
@SpringBootTest | ||
@TestPropertySource( | ||
properties = { | ||
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration" | ||
}) | ||
@Import({GoogleTranslatorOkHttp.class, DictionaryServiceOkHttp.class}) | ||
@TestPropertySource(properties = "korvo-to-anki.http-client=ok-http") | ||
class KorvoToAnkiApplicationWebClientTests { | ||
|
||
@Autowired ApplicationContext context; | ||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withInitializer( | ||
new ConditionEvaluationReportLoggingListener()) // to print out conditional config | ||
// report to log | ||
.withPropertyValues("debug=false") | ||
.withUserConfiguration(OkHttpConfig.class, Config.class) | ||
.withUserConfiguration(GoogleTranslatorOkHttp.class, DictionaryServiceOkHttp.class); | ||
|
||
@DisplayName("all okhttp service beans") | ||
@Test | ||
void contextLoads() { | ||
void contextLoadsOkHttpServiceBeansExpectsToLoad() { | ||
|
||
var googleTranslatorOkHttp = context.getBean(GoogleTranslatorOkHttp.class); | ||
var dictionaryServiceOkHttp = context.getBean(DictionaryServiceOkHttp.class); | ||
contextRunner | ||
// .withUserConfiguration(AppProperties.class) | ||
.withPropertyValues("korvo-to-anki.http-client=ok-http") | ||
.run( | ||
context -> | ||
assertAll( | ||
() -> assertThat(context).hasSingleBean(GoogleTranslatorOkHttp.class), | ||
() -> assertThat(context).hasSingleBean(DictionaryServiceOkHttp.class), | ||
() -> assertThat(context).hasSingleBean(OkHttpConfig.class))); | ||
} | ||
|
||
@DisplayName("all okhttp service beans") | ||
@Test | ||
void contextLoadsOkHttpServiceBeansExpectsToNotLoad() { | ||
|
||
contextRunner | ||
// .withUserConfiguration(AppProperties.class) | ||
.withPropertyValues("korvo-to-anki.http-client=whatever") | ||
.run( | ||
context -> | ||
assertAll( | ||
() -> assertThat(context).doesNotHaveBean(GoogleTranslatorOkHttp.class), | ||
() -> assertThat(context).doesNotHaveBean(DictionaryServiceOkHttp.class), | ||
() -> assertThat(context).doesNotHaveBean(OkHttpConfig.class))); | ||
} | ||
|
||
@Configuration | ||
protected static class Config { | ||
|
||
@Bean | ||
public AppProperties appProperties() { | ||
return Mockito.mock(AppProperties.class); | ||
} | ||
|
||
@Bean | ||
public GoogleParamsProperties googleParamsProperties() { | ||
return Mockito.mock(GoogleParamsProperties.class); | ||
} | ||
|
||
@Bean | ||
public GoogleTranslatorParser googleTranslatorParser() { | ||
return Mockito.mock(GoogleTranslatorParser.class); | ||
} | ||
|
||
@Bean | ||
public DictionaryApiProperties dictionaryApiProperties() { | ||
return Mockito.mock(DictionaryApiProperties.class); | ||
} | ||
|
||
assertNotNull(googleTranslatorOkHttp); | ||
assertNotNull(dictionaryServiceOkHttp); | ||
@Bean | ||
public ObjectMapper objectMapper() { | ||
return Mockito.mock(ObjectMapper.class); | ||
} | ||
} | ||
} |
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
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