Skip to content

Commit

Permalink
test: updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dankoy committed Dec 24, 2024
1 parent 3bdd298 commit 7f8ea4b
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 55 deletions.
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
Expand All @@ -24,9 +25,9 @@
import ru.dankoy.korvotoanki.core.dao.vocabularybuilder.title.TitleDaoJdbc;
import ru.dankoy.korvotoanki.core.dao.vocabularybuilder.vocabulary.VocabularyDaoJdbc;
import ru.dankoy.korvotoanki.core.fabric.anki.AnkiDataFabricImpl;
import ru.dankoy.korvotoanki.core.service.converter.AnkiConverterServiceImpl;
import ru.dankoy.korvotoanki.core.service.converter.AnkiConverterServiceCompletableFuture;
import ru.dankoy.korvotoanki.core.service.dictionaryapi.DictionaryServiceWebClient;
import ru.dankoy.korvotoanki.core.service.exporter.ExporterServiceAnkiAsync;
import ru.dankoy.korvotoanki.core.service.exporter.ExporterServiceAnkiCompletableFuture;
import ru.dankoy.korvotoanki.core.service.filenameformatter.FileNameFormatterServiceImpl;
import ru.dankoy.korvotoanki.core.service.fileprovider.FileProviderServiceImpl;
import ru.dankoy.korvotoanki.core.service.googletrans.GoogleTranslatorWebClient;
Expand All @@ -45,6 +46,8 @@
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration"
})
@SpringBootTest
@AutoConfigureTestDatabase(
replace = AutoConfigureTestDatabase.Replace.NONE) // use embedded database to run migrations on
class KorvoToAnkiApplicationTests {

@Autowired ApplicationContext context;
Expand All @@ -70,9 +73,9 @@ void contextLoads() {
var titleDaoJdbc = context.getBean(TitleDaoJdbc.class);
var vocabularyDaoJdbc = context.getBean(VocabularyDaoJdbc.class);
var ankiDataFabric = context.getBean(AnkiDataFabricImpl.class);
var ankiConverterService = context.getBean(AnkiConverterServiceImpl.class);
var ankiConverterService = context.getBean(AnkiConverterServiceCompletableFuture.class);
var dictionaryServiceWebClient = context.getBean(DictionaryServiceWebClient.class);
var exporterServiceAnkiAsync = context.getBean(ExporterServiceAnkiAsync.class);
var exporterServiceAnkiAsync = context.getBean(ExporterServiceAnkiCompletableFuture.class);
// var exporterServiceAnki = context.getBean(ExporterServiceAnki.class);
var fileNameFormatterService = context.getBean(FileNameFormatterServiceImpl.class);
var fileProviderService = context.getBean(FileProviderServiceImpl.class);
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.test.context.TestPropertySource;
import ru.dankoy.korvotoanki.config.datasource.StateDataSourceConfig;
import ru.dankoy.korvotoanki.core.domain.state.State;
import ru.dankoy.korvotoanki.core.exceptions.StateDaoException;

@DisplayName("Test StateDaoJdbc ")
@Import({StateDataSourceConfig.class, StateDaoJdbc.class})
@TestPropertySource(
properties = {
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,"
+ " org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration"
@JdbcTest(
excludeAutoConfiguration = {
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration.class,
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.class
})
@JdbcTest
@AutoConfigureTestDatabase(
replace = AutoConfigureTestDatabase.Replace.NONE) // use embedded database
class StateDaoJdbcTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;
import ru.dankoy.korvotoanki.core.domain.Title;
import ru.dankoy.korvotoanki.core.exceptions.TitleDaoException;

@DisplayName("Test TitleDaoJdbc ")
@TestPropertySource(
@JdbcTest(
properties = {
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration"
})
@JdbcTest
@Import(TitleDaoJdbc.class)
@AutoConfigureTestDatabase(
replace = AutoConfigureTestDatabase.Replace.NONE) // use embedded database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.test.context.TestPropertySource;
import ru.dankoy.korvotoanki.core.domain.Title;
import ru.dankoy.korvotoanki.core.domain.Vocabulary;

@DisplayName("Test VocabularyDaoJdbc ")
@TestPropertySource(
@JdbcTest(
properties = {
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration"
})
@JdbcTest
@Import(VocabularyDaoJdbc.class)
@AutoConfigureTestDatabase(
replace = AutoConfigureTestDatabase.Replace.NONE) // use embedded database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
GoogleTranslatorOkHttp.class,
AnkiDataFabricImpl.class,
ExternalApiParams.class
})
},
properties = {"korvo-to-anki.async=false"})
class AnkiConverterServiceImplTest {

@MockBean private DictionaryService dictionaryService;
Expand Down

0 comments on commit 7f8ea4b

Please sign in to comment.