Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from ralf-ueberfuhr-ars/feature/config
Browse files Browse the repository at this point in the history
Configure application using application.yml and configuration class
  • Loading branch information
ralf-ueberfuhr-ars authored Jun 13, 2024
2 parents 32b06a0 + a30cb56 commit ba7dd64
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.sample.schulung.accounts.boundary;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/*
* Lösung lt. Spring MVC
* - Implementiere einen WebMvcConfigurer
* - Packe diese Implementierung (Objekt) in den Context (Eimer)
*/
@Configuration // Spring Boot AutoConfiguration
public class IndexPageConfiguration {

@Bean //Rückgabewert der Methode wird in den Eimer gepackt
WebMvcConfigurer indexPageConfig() {
return new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry
.addViewController("/")
.setViewName("redirect:/index.html");
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package de.sample.schulung.accounts.domain;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
Expand All @@ -11,28 +15,50 @@

@Component
@RequiredArgsConstructor
@Slf4j
public class CustomersInitializer {

/*
* let's use a separate class for the configuration
* - default constructor
* - injected into the CustomersInitializer
* - could be placed in a separate JAVA file too
*/
@Component
@ConfigurationProperties(prefix = "application.customers.initialization")
@Getter
@Setter
public static class CustomerInitialerConfiguration {

private boolean enabled;

}

private final CustomersService service;
private final CustomerInitialerConfiguration config;


@EventListener(ContextRefreshedEvent.class)
public void init() {
service.createCustomer(
new Customer(
null,
"Max",
LocalDate.of(2010, Month.FEBRUARY, 10),
Customer.CustomerState.ACTIVE
)
);
service.createCustomer(
new Customer(
UUID.randomUUID(),
"Julia",
LocalDate.of(2011, Month.APRIL, 2),
Customer.CustomerState.DISABLED
)
);
if(this.config.enabled) {
log.info("Initializing customers");
service.createCustomer(
new Customer(
null,
"Max",
LocalDate.of(2010, Month.FEBRUARY, 10),
Customer.CustomerState.ACTIVE
)
);
service.createCustomer(
new Customer(
UUID.randomUUID(),
"Julia",
LocalDate.of(2011, Month.APRIL, 2),
Customer.CustomerState.DISABLED
)
);
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
application:
customers:
initialization:
enabled: true
9 changes: 9 additions & 0 deletions account-service-provider/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server:
port: 8080
spring:
jackson:
property-naming-strategy: SNAKE_CASE
application:
customers:
initialization:
enabled: ${CUSTOMERS_INITIALIZATION_ENABLED:false}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@SpringBootTest
@AutoConfigureMockMvc
//@ActiveProfiles("local")
class AccountsApiTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.sample.schulung.accounts;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
public class IndexPageTests {

@Autowired
MockMvc mvc;

@Test
void shouldRedirectIndexPage() throws Exception {
var location = mvc.perform(
get("/")
)
.andExpect(status().isFound())
.andExpect(header().exists("Location"))
.andReturn()
.getResponse()
.getHeader("Location");
assertThat(location).endsWith("/index.html");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import de.sample.schulung.accounts.domain.CustomersService;
import de.sample.schulung.accounts.domain.NotFoundException;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -21,7 +19,12 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@SpringBootTest(
// disable initializer for these tests
properties = {
"application.customers.initialization.enabled=false"
}
)
@AutoConfigureMockMvc
public class AccountsBoundaryTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;

@SpringBootTest
@SpringBootTest(
properties = {
"application.customers.initialization.enabled=true"
}
)
public class CustomersInitializerTests {

@MockBean
Expand Down

0 comments on commit ba7dd64

Please sign in to comment.