|
| 1 | +package com.itasocialacademy.oitassist.version.controller; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.when; |
| 4 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 7 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 8 | + |
| 9 | +import com.itasocialacademy.oitassist.core.web.AppExceptionHttpStatusMapper; |
| 10 | +import com.itasocialacademy.oitassist.security.jwt.JwtFilter; |
| 11 | +import com.itasocialacademy.oitassist.version.api.VersionResponse; |
| 12 | +import com.itasocialacademy.oitassist.version.service.interfaces.VersionService; |
| 13 | +import java.time.Instant; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.context.TestConfiguration; |
| 17 | +import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; |
| 18 | +import org.springframework.context.annotation.Bean; |
| 19 | +import org.springframework.context.annotation.ComponentScan; |
| 20 | +import org.springframework.context.annotation.FilterType; |
| 21 | +import org.springframework.context.annotation.Import; |
| 22 | +import org.springframework.http.MediaType; |
| 23 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 24 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 25 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
| 26 | +import org.springframework.security.web.SecurityFilterChain; |
| 27 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 28 | +import org.springframework.test.web.servlet.MockMvc; |
| 29 | + |
| 30 | +@WebMvcTest( |
| 31 | + controllers = VersionController.class, |
| 32 | + excludeFilters = @ComponentScan.Filter( |
| 33 | + type = FilterType.ASSIGNABLE_TYPE, |
| 34 | + classes = JwtFilter.class)) |
| 35 | +@Import(VersionControllerTest.SecurityTestConfiguration.class) |
| 36 | +class VersionControllerTest { |
| 37 | + |
| 38 | + private static final String ENDPOINT = "/api/v1/version"; |
| 39 | + private static final String COMMIT_ID = "3534bab24568a602605078b9264711223f218dd2"; |
| 40 | + private static final String SHORT_COMMIT_ID = "3534bab"; |
| 41 | + private static final String COMMIT_TIME = "2026-08-19T08:22:05Z"; |
| 42 | + private static final String BUILD_TIME = "2026-08-19T19:55:29.490Z"; |
| 43 | + |
| 44 | + @Autowired |
| 45 | + private MockMvc mockMvc; |
| 46 | + |
| 47 | + @MockitoBean |
| 48 | + private VersionService versionService; |
| 49 | + |
| 50 | + @MockitoBean |
| 51 | + private AppExceptionHttpStatusMapper appExceptionHttpStatusMapper; |
| 52 | + |
| 53 | + @Test |
| 54 | + void getVersion_ShouldReturnVersion_WhenCallerIsAnonymous() throws Exception { |
| 55 | + when(versionService.getVersion()).thenReturn( |
| 56 | + new VersionResponse( |
| 57 | + new VersionResponse.BackendVersion( |
| 58 | + COMMIT_ID, |
| 59 | + SHORT_COMMIT_ID, |
| 60 | + Instant.parse(COMMIT_TIME), |
| 61 | + "dev", |
| 62 | + "0.0.1-SNAPSHOT"), |
| 63 | + Instant.parse(BUILD_TIME))); |
| 64 | + |
| 65 | + mockMvc.perform(get(ENDPOINT)) |
| 66 | + .andExpect(status().isOk()) |
| 67 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 68 | + .andExpect(jsonPath("$.backend.commitId").value(COMMIT_ID)) |
| 69 | + .andExpect(jsonPath("$.backend.shortCommitId").value(SHORT_COMMIT_ID)) |
| 70 | + .andExpect(jsonPath("$.backend.commitTime").value(COMMIT_TIME)) |
| 71 | + .andExpect(jsonPath("$.backend.branch").value("dev")) |
| 72 | + .andExpect(jsonPath("$.backend.version").value("0.0.1-SNAPSHOT")) |
| 73 | + .andExpect(jsonPath("$.buildTime").value(BUILD_TIME)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void getVersion_ShouldReturnOkWithEmptyValues_WhenBuildMetadataIsMissing() throws Exception { |
| 78 | + when(versionService.getVersion()).thenReturn( |
| 79 | + new VersionResponse( |
| 80 | + new VersionResponse.BackendVersion(null, null, null, null, null), |
| 81 | + null)); |
| 82 | + |
| 83 | + mockMvc.perform(get(ENDPOINT)) |
| 84 | + .andExpect(status().isOk()) |
| 85 | + .andExpect(jsonPath("$.backend").exists()) |
| 86 | + .andExpect(jsonPath("$.backend.commitId").isEmpty()) |
| 87 | + .andExpect(jsonPath("$.backend.commitTime").isEmpty()) |
| 88 | + .andExpect(jsonPath("$.backend.branch").isEmpty()) |
| 89 | + .andExpect(jsonPath("$.buildTime").isEmpty()); |
| 90 | + } |
| 91 | + |
| 92 | + @TestConfiguration(proxyBeanMethods = false) |
| 93 | + @EnableWebSecurity |
| 94 | + static class SecurityTestConfiguration { |
| 95 | + @Bean |
| 96 | + SecurityFilterChain securityFilterChain(HttpSecurity http) { |
| 97 | + return http |
| 98 | + .csrf(AbstractHttpConfigurer::disable) |
| 99 | + .authorizeHttpRequests(authorization -> authorization |
| 100 | + .anyRequest() |
| 101 | + .permitAll()) |
| 102 | + .build(); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments