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