diff --git a/2022-11/spring-05-springBoot/.gitignore b/2022-11/spring-05-springBoot/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-11/spring-05-springBoot/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-11/spring-05-springBoot/pom.xml b/2022-11/spring-05-springBoot/pom.xml new file mode 100644 index 00000000..6299aa46 --- /dev/null +++ b/2022-11/spring-05-springBoot/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + ru.otus + spring-05-springBoot + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + UTF-8 + 11 + 11 + + 3.1.0 + 3.3.9 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-maven + + enforce + + + + + + ${minimal.maven.version} + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + \ No newline at end of file diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java new file mode 100644 index 00000000..996a8eda --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/SpringBootDemo.java @@ -0,0 +1,14 @@ +package ru.otus; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +//http://localhost:8080/actuator/health + +@SpringBootApplication +public class SpringBootDemo { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemo.class, args); + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java new file mode 100644 index 00000000..398ad979 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/commandlinerunners/PreparationDev.java @@ -0,0 +1,20 @@ +package ru.otus.commandlinerunners; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import java.util.Arrays; + + +@Component +public class PreparationDev implements CommandLineRunner { + private static final Logger logger = LoggerFactory.getLogger(PreparationDev.class); + + @Override + public void run(String... args) { + logger.info("DEV mode!!! Что-то настравиваем и подготавливаем, параметры: {} ", Arrays.toString(args)); + //args парметры, котрые могут быть переданы в Main + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java new file mode 100644 index 00000000..3705d682 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/configs/AppProps.java @@ -0,0 +1,28 @@ +package ru.otus.configs; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.Locale; + +@ConfigurationProperties(prefix = "application") +public class AppProps { + + private String message; + private Locale locale; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Locale getLocale() { + return locale; + } + + public void setLocale(Locale locale) { + this.locale = locale; + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java new file mode 100644 index 00000000..975eb40d --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/configs/ApplicationConfig.java @@ -0,0 +1,9 @@ +package ru.otus.configs; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties(AppProps.class) +public class ApplicationConfig { +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/controller/GreetingController.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/controller/GreetingController.java new file mode 100644 index 00000000..7db4c61f --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/controller/GreetingController.java @@ -0,0 +1,29 @@ +package ru.otus.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.otus.configs.AppProps; + + +import java.util.Map; +import ru.otus.service.GreetingService; + +@RestController +public class GreetingController { + private static final Logger logger = LoggerFactory.getLogger(GreetingController.class); + private final GreetingService greetingService; + + public GreetingController(GreetingService greetingService, AppProps props) { + this.greetingService = greetingService; + logger.info("ATTENTION! props.getMessage(): {}", props.getMessage()); + } + + //http://localhost:8080/hello?name=ddd + @GetMapping("/hello") + public Map sayHello(@RequestParam(name="name") String name) { + return this.greetingService.sayHello(name); + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/controller/GreetingControllerRstyle.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/controller/GreetingControllerRstyle.java new file mode 100644 index 00000000..9a2a6225 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/controller/GreetingControllerRstyle.java @@ -0,0 +1,28 @@ +package ru.otus.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; +import ru.otus.service.GreetingService; + +@RestController +public class GreetingControllerRstyle { + private static final Logger logger = LoggerFactory.getLogger(GreetingControllerRstyle.class); + + private final GreetingService greetingService; + + public GreetingControllerRstyle(GreetingService greetingService) { + logger.info("Я НАСТОЯЩИЙ БИН!!!"); + this.greetingService = greetingService; + } + + //http://localhost:8080/hello/jone + @GetMapping(value="/hello/{name}") + public Map sayHello(@PathVariable("name") String name) { + return this.greetingService.sayHello(name); + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java new file mode 100644 index 00000000..c0b48c82 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/GreetingService.java @@ -0,0 +1,7 @@ +package ru.otus.service; + +import java.util.Map; + +public interface GreetingService { + Map sayHello(String name); +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java new file mode 100644 index 00000000..8501ceae --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/GreetingServiceServiceImpl.java @@ -0,0 +1,17 @@ +package ru.otus.service; + +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class GreetingServiceServiceImpl implements GreetingService { + + @Override + public Map sayHello(String name) { + Map map = new HashMap<>(); + map.put(name, "Hello, " + name); + return map; + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java b/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java new file mode 100644 index 00000000..6dddf223 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/ru/otus/service/LocalHello.java @@ -0,0 +1,35 @@ +package ru.otus.service; + +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.MessageSource; +import org.springframework.stereotype.Component; +import ru.otus.configs.AppProps; + + +@Component +public class LocalHello { + private static final Logger logger = LoggerFactory.getLogger(LocalHello.class); + + private final MessageSource messageSource; + private final AppProps props; + private final String message; + + public LocalHello(MessageSource messageSource, AppProps props, @Value("${application.message}") String message) { + this.messageSource = messageSource; + this.props = props; + this.message = message; + } + + //!!! Вообще, PostConstruct - это плохая практика !!! + @PostConstruct + public void printHello() { + var messageLocalized = messageSource.getMessage("hello.user", new String[]{"Ivan"}, props.getLocale()); + logger.info("Localization:{}", messageLocalized); + + logger.info("message from props:{}", props.getMessage()); + logger.info("message:{}", message); + } +} diff --git a/2022-11/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java b/2022-11/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java new file mode 100644 index 00000000..4900dfb6 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/java/wrongPackage/PreparationUnKnown.java @@ -0,0 +1,17 @@ +package wrongPackage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +//Этот компонент не будет найден, т.к. нарушена иерархия пакетов +@Component +public class PreparationUnKnown implements CommandLineRunner { + private static final Logger logger = LoggerFactory.getLogger(PreparationUnKnown.class); + + @Override + public void run(String... args) throws Exception { + logger.info("Это не увидим никогда"); + } +} diff --git a/2022-11/spring-05-springBoot/src/main/resources/application.yml b/2022-11/spring-05-springBoot/src/main/resources/application.yml new file mode 100644 index 00000000..425b46b2 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/resources/application.yml @@ -0,0 +1,17 @@ +spring: + messages: + basename: "i18n/appmessages" + encoding: "UTF-8" + +management: + endpoints: + web: + exposure: + include: info, health, beans + +server: + port: 8080 + +application: + message: "Test Props msg" + locale: ru_RU diff --git a/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages.properties b/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages.properties new file mode 100644 index 00000000..e69de29b diff --git a/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties b/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties new file mode 100644 index 00000000..e3bada4f --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages_en.properties @@ -0,0 +1 @@ +hello.user="Hello, {0}"! \ No newline at end of file diff --git a/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties b/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties new file mode 100644 index 00000000..07b8dad3 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/resources/i18n/appmessages_ru_RU.properties @@ -0,0 +1 @@ +hello.user="Привет, {0}"! \ No newline at end of file diff --git a/2022-11/spring-05-springBoot/src/main/resources/logback.xml b/2022-11/spring-05-springBoot/src/main/resources/logback.xml new file mode 100644 index 00000000..6fb07ce3 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + diff --git a/2022-11/spring-05-springBoot/src/test/java/ru/otus/controller/GreetingControllerTest.java b/2022-11/spring-05-springBoot/src/test/java/ru/otus/controller/GreetingControllerTest.java new file mode 100644 index 00000000..904c5386 --- /dev/null +++ b/2022-11/spring-05-springBoot/src/test/java/ru/otus/controller/GreetingControllerTest.java @@ -0,0 +1,64 @@ +package ru.otus.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.MockMvc; +import ru.otus.service.GreetingService; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + + +@WebMvcTest({GreetingController.class, GreetingControllerRstyle.class}) +class GreetingControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private GreetingService greetingService; + + @Test + void sayHelloTest() throws Exception { + String name = "Mr.Test"; + Map map = new HashMap<>(); + map.put(name, "Hello, " + name); + String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}"; + + when(greetingService.sayHello(name)).thenReturn(map); + MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello?name=%s", name))) + .andReturn() + .getResponse(); + + verify(greetingService, times(1)).sayHello(name); + System.out.println(); + assertThat(result.getContentAsString()).isEqualTo(expectedResult); + } + + @Test + void sayHelloTestMock() throws Exception { + String name = "Mr.Test"; + Map map = new HashMap<>(); + map.put(name, "Hello, " + name); + String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}"; + + when(greetingService.sayHello(name)).thenReturn(map); + MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello/%s", name))) + .andReturn() + .getResponse(); + + verify(greetingService, times(1)).sayHello(name); + System.out.println(); + assertThat(result.getContentAsString()).isEqualTo(expectedResult); + } +} \ No newline at end of file