|
| 1 | +package com.bobocode; |
| 2 | + |
| 3 | +import com.bobocode.config.RootConfig; |
| 4 | +import com.bobocode.config.WebConfig; |
| 5 | +import com.bobocode.web.controller.WelcomeController; |
| 6 | +import org.junit.jupiter.api.*; |
| 7 | +import org.springframework.context.annotation.ComponentScan; |
| 8 | +import org.springframework.context.annotation.ComponentScan.Filter; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.context.annotation.FilterType; |
| 11 | +import org.springframework.stereotype.Controller; |
| 12 | +import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 14 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | + |
| 23 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 24 | +class WebAppConfigurationTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + @Order(1) |
| 28 | + @DisplayName("RootConfig class is marked as @Configuration") |
| 29 | + void rootConfigClassIsMarkedAsConfiguration() { |
| 30 | + Configuration configuration = RootConfig.class.getAnnotation(Configuration.class); |
| 31 | + |
| 32 | + assertNotNull(configuration); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @Order(2) |
| 37 | + @DisplayName("RootConfig class enables @ComponentScan") |
| 38 | + void rootConfigClassEnablesComponentScan() { |
| 39 | + ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
| 40 | + |
| 41 | + assertNotNull(componentScan); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @Order(3) |
| 46 | + @DisplayName("RootConfig @ComponentScan contains base packages") |
| 47 | + void rootConfigComponentScanPackages() { |
| 48 | + ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
| 49 | + String[] packages = componentScan.basePackages(); |
| 50 | + if (packages.length == 0) { |
| 51 | + packages = componentScan.value(); |
| 52 | + } |
| 53 | + |
| 54 | + assertThat(packages).contains("com.bobocode"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @Order(4) |
| 59 | + @DisplayName("RootConfig @ComponentScan contains web scan filters") |
| 60 | + void rootConfigComponentScanFilters() { |
| 61 | + ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
| 62 | + Filter[] filters = componentScan.excludeFilters(); |
| 63 | + List<Class> filteredClasses = getFilteredClasses(filters); |
| 64 | + |
| 65 | + assertThat(filters.length).isEqualTo(2); |
| 66 | + assertThat(filters[0].type()).isEqualTo(FilterType.ANNOTATION); |
| 67 | + assertThat(filters[1].type()).isEqualTo(FilterType.ANNOTATION); |
| 68 | + assertThat(filteredClasses.toArray()).containsExactlyInAnyOrder(EnableWebMvc.class, Controller.class); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @Order(5) |
| 73 | + @DisplayName("WebConfig is marked as @Configuration") |
| 74 | + void webConfigIsMarkedAsConfiguration() { |
| 75 | + Configuration configuration = WebConfig.class.getAnnotation(Configuration.class); |
| 76 | + |
| 77 | + assertNotNull(configuration); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @Order(6) |
| 82 | + @DisplayName("WebConfig enables @ComponentScan") |
| 83 | + void webConfigEnablesComponentScan() { |
| 84 | + ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class); |
| 85 | + |
| 86 | + assertNotNull(componentScan); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @Order(7) |
| 91 | + @DisplayName("WebConfig @ComponentScan contains web packages") |
| 92 | + void webConfigComponentScanPackages() { |
| 93 | + ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class); |
| 94 | + |
| 95 | + assertThat(componentScan.basePackages()).contains("com.bobocode.web"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @Order(8) |
| 100 | + @DisplayName("WebConfig is marked with @EnableWebMvc") |
| 101 | + void webConfigEnablesWebMvc() { |
| 102 | + EnableWebMvc enableWebMvc = WebConfig.class.getAnnotation(EnableWebMvc.class); |
| 103 | + |
| 104 | + assertNotNull(enableWebMvc); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Order(9) |
| 109 | + @DisplayName("Initializer method is overridden with RootConfig class") |
| 110 | + void initializerRootConfigClasses() { |
| 111 | + WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
| 112 | + |
| 113 | + assertThat(webAppInitializerWrapper.getRootConfigClasses()).contains(RootConfig.class); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + @Order(10) |
| 118 | + @DisplayName("Initializer method is overridden with WebConfig class") |
| 119 | + void initializerWebConfigClasses() { |
| 120 | + WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
| 121 | + |
| 122 | + assertThat(webAppInitializerWrapper.getServletConfigClasses()).contains(WebConfig.class); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @Order(11) |
| 127 | + @DisplayName("Dispatcher Servlet mapping is \"/\"") |
| 128 | + void dispatcherServletMapping() { |
| 129 | + WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
| 130 | + |
| 131 | + assertThat(webAppInitializerWrapper.getServletMappings()).contains("/"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @Order(12) |
| 136 | + @DisplayName("WelcomeController is marked as @Controller") |
| 137 | + void welcomeControllerIsMarkedAsController() { |
| 138 | + Controller controller = WelcomeController.class.getAnnotation(Controller.class); |
| 139 | + |
| 140 | + assertNotNull(controller); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + @Order(13) |
| 145 | + @DisplayName("WelcomeController method is marked as Get method") |
| 146 | + void welcomeControllerMethodIsMarkedAsGetMethod() throws NoSuchMethodException { |
| 147 | + GetMapping getMapping = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(GetMapping.class); |
| 148 | + |
| 149 | + assertNotNull(getMapping); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + @Order(14) |
| 154 | + @DisplayName("WelcomeController Get method is marked properly") |
| 155 | + void welcomeControllerMethodMapping() throws NoSuchMethodException { |
| 156 | + GetMapping getMapping = WelcomeController.class |
| 157 | + .getDeclaredMethod("welcome").getAnnotation(GetMapping.class); |
| 158 | + |
| 159 | + assertThat(getMapping.value()).contains("/welcome"); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + @Order(15) |
| 164 | + @DisplayName("WelcomeController Get method is marked as @ResponseBody") |
| 165 | + void welcomeControllerMethodIsMarkedAsResponseBody() throws NoSuchMethodException { |
| 166 | + ResponseBody responseBody = WelcomeController.class |
| 167 | + .getDeclaredMethod("welcome").getAnnotation(ResponseBody.class); |
| 168 | + |
| 169 | + assertNotNull(responseBody); |
| 170 | + } |
| 171 | + |
| 172 | + private List<Class> getFilteredClasses(Filter[] filters) { |
| 173 | + return Stream.of(filters).flatMap(filter -> Stream.of(filter.value())).collect(Collectors.toList()); |
| 174 | + } |
| 175 | +} |
0 commit comments