Newer
Older

OZGCloud
committed
package de.ozgcloud.admin.environment;
import static org.mockito.Mockito.*;

OZGCloud
committed
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Jan Zickermann
committed
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;

OZGCloud
committed
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import de.ozgcloud.admin.RootController;
import lombok.SneakyThrows;
@ExtendWith(MockitoExtension.class)
class EnvironmentControllerTest {

OZGCloud
committed
@Spy
private EnvironmentController controller;
private ProductionProperties environmentProperties;
@Mock
private OAuth2Properties oAuth2Properties;

OZGCloud
committed
private MockMvc mockMvc;
@BeforeEach
void mock() {

OZGCloud
committed
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Nested
class TestGetEnvironment {
@SneakyThrows
@Test
void shouldReturnOk() {
when(environmentProperties.isProduction()).thenReturn(true);

OZGCloud
committed
var response = doRequest();
response.andExpect(status().isOk());
}
@Nested
class TestBody {
@Test
@SneakyThrows
void shouldCallIsProduction() {
when(environmentProperties.isProduction()).thenReturn(true);
doRequest();
verify(environmentProperties).isProduction();
}
@ParameterizedTest
@ValueSource(booleans = { true, false })

OZGCloud
committed
@SneakyThrows
void shouldContainProduction(boolean production) {
when(environmentProperties.isProduction()).thenReturn(production);

OZGCloud
committed
var response = doRequest();
response.andExpect(jsonPath("$.production").value(production));

OZGCloud
committed
}
}
@Test
@SneakyThrows
void shouldContainURLToRoot() {
var response = doRequest();
response.andExpect(jsonPath("$.remoteHost").value("http://localhost" + RootController.PATH));

OZGCloud
committed
}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
@SneakyThrows
@Test
void shouldContainAuthServerUrl() {
var oAuthServerUrl = "dummyOAuthUrl";
when(oAuth2Properties.getAuthServerUrl()).thenReturn(oAuthServerUrl);
var response = doRequest();
response.andExpect(jsonPath("$.authServer").value(oAuthServerUrl));
}
@SneakyThrows
@Test
void shouldContainRealm() {
var realm = "dummyRealm";
when(oAuth2Properties.getRealm()).thenReturn(realm);
var response = doRequest();
response.andExpect(jsonPath("$.realm").value(realm));
}
@SneakyThrows
@Test
void shouldContainClientId() {
var clientId = "dummdyClientId";
when(oAuth2Properties.getResource()).thenReturn(clientId);
var response = doRequest();
response.andExpect(jsonPath("$.clientId").value(clientId));
}
@SneakyThrows
private ResultActions doRequest() {
return mockMvc.perform(get(EnvironmentController.PATH));