From 6dbe40c4991228caaf1cbc416da1920e405ba1a7 Mon Sep 17 00:00:00 2001 From: "DESKTOP-FSO9NHB\\User" Date: Sun, 6 Dec 2020 18:51:46 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=94=84=EB=A7=81=20=EB=B6=80?= =?UTF-8?q?=ED=8A=B8=EC=99=80=20AWS=EB=A1=9C=20=ED=98=BC=EC=9E=90=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=98=EB=8A=94=20=EC=9B=B9=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20Chapter10=20=EC=A7=84=ED=96=89=EC=A4=91=20?= =?UTF-8?q?ProfileController=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EB=B3=80?= =?UTF-8?q?=EB=8F=99=EC=82=AC=ED=95=AD=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/auth/SecurityConfig.java | 2 +- .../springboot/web/ProfileController.java | 29 +++++++++ .../springboot/web/ProfileControllerTest.java | 34 +++++++++++ .../web/ProfileControllerUnitTest.java | 60 +++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java create mode 100644 WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java create mode 100644 WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java diff --git a/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java index db07b49..adc7031 100644 --- a/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java +++ b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java @@ -17,7 +17,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.csrf().disable().headers().frameOptions().disable() .and() .authorizeRequests() - .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**").permitAll() + .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**", "/profile").permitAll() .antMatchers("/api/v1/**").hasRole(Role.USER.name()) .anyRequest().authenticated() .and() diff --git a/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java new file mode 100644 index 0000000..f54969d --- /dev/null +++ b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java @@ -0,0 +1,29 @@ +package com.banjjoknim.book.springboot.web; + +import lombok.RequiredArgsConstructor; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RequiredArgsConstructor +@RestController +public class ProfileController { + private final Environment env; + + @GetMapping("/profile") + public String profile() { + List profiles = Arrays.asList(env.getActiveProfiles()); + + List realProfiles = Arrays.asList("real", "real1", "real2"); + + String defaultProfile = profiles.isEmpty() ? "default" : profiles.get(0); + + return profiles.stream() + .filter(realProfiles::contains) + .findAny() + .orElse(defaultProfile); + } +} diff --git a/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java new file mode 100644 index 0000000..9a38607 --- /dev/null +++ b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java @@ -0,0 +1,34 @@ +package com.banjjoknim.book.springboot.web; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ProfileControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void profile은_인증없이_호출된다() { + String expected = "default"; + + ResponseEntity response = restTemplate.getForEntity("/profile", String.class); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java new file mode 100644 index 0000000..f31dcc5 --- /dev/null +++ b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java @@ -0,0 +1,60 @@ +package com.banjjoknim.book.springboot.web; + +import org.junit.Test; +import org.springframework.mock.env.MockEnvironment; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ProfileControllerUnitTest { + + @Test + public void real_profile이_조회된다() { + //given + String expectedProfile = "real"; + MockEnvironment env = new MockEnvironment(); + env.addActiveProfile(expectedProfile); + env.addActiveProfile("oauth"); + env.addActiveProfile("real-db"); + + ProfileController controller = new ProfileController(env); + + //when + String profile = controller.profile(); + + //then + assertThat(profile).isEqualTo(expectedProfile); + } + + @Test + public void real_profile이_없으면_첫_번째가_조회된다() { + //given + String expectedProfile = "oauth"; + MockEnvironment env = new MockEnvironment(); + env.addActiveProfile(expectedProfile); + env.addActiveProfile("real-db"); + + ProfileController controller = new ProfileController(env); + + //when + String profile = controller.profile(); + + //then + assertThat(profile).isEqualTo(expectedProfile); + } + + @Test + public void active_profile이_없으면_default가_조회된다() { + //given + String expectedProfile = "default"; + MockEnvironment env = new MockEnvironment(); + + ProfileController controller = new ProfileController(env); + + //when + String profile = controller.profile(); + + //then + assertThat(profile).isEqualTo(expectedProfile); + } + +} \ No newline at end of file