Skip to content
Snippets Groups Projects
Commit 82355a87 authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-4717 Use Properties class instead of @Value annotation

parent 8d12e275
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.admin.environment;
import org.springframework.beans.factory.annotation.Value;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import de.ozgcloud.admin.RootController;
import lombok.RequiredArgsConstructor;
@RestController
@RequiredArgsConstructor
@RequestMapping(FrontendEnvironmentController.BASE_PATH)
public class FrontendEnvironmentController {
static final String BASE_PATH = "/api/environment"; // NOSONAR
@Value("${ozgcloud.stage.production:false}")
private boolean production;
private final FrontendEnvironmentProperties environmentProperties;
@GetMapping
public FrontendEnvironment getEnvironment() {
return FrontendEnvironment.builder()
.production(production)
.remoteHost(RootController.PATH)
.production(environmentProperties.isProduction())
.remoteHost(linkTo(RootController.class).toUri().toString())
.build();
}
}
/*
* Copyright (c) 2024. Das Land Schleswig-Holstein vertreten durch das Ministerium für Energiewende, Klimaschutz, Umwelt und Natur
* Zentrales IT-Management
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.admin.environment;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Configuration
@ConfigurationProperties("ozgcloud.stage")
public class FrontendEnvironmentProperties {
/**
* True if the angular frontend (admin client) should build in production mode.
*/
private boolean production;
}
package de.ozgcloud.admin.environment;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
......@@ -7,9 +8,12 @@ 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;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
......@@ -18,17 +22,19 @@ import de.ozgcloud.admin.RootController;
import lombok.SneakyThrows;
@ExtendWith(MockitoExtension.class)
public class FrontendEnvironmentControllerTest {
class FrontendEnvironmentControllerTest {
@Spy
@InjectMocks
private FrontendEnvironmentController controller;
@Value("${ozgcloud.stage.production}")
private boolean production;
@Mock
private FrontendEnvironmentProperties environmentProperties;
private MockMvc mockMvc;
@BeforeEach
void initTest() {
void mock() {
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
......@@ -37,6 +43,8 @@ public class FrontendEnvironmentControllerTest {
@SneakyThrows
@Test
void shouldReturnOk() {
when(environmentProperties.isProduction()).thenReturn(true);
var response = doRequest();
response.andExpect(status().isOk());
......@@ -44,12 +52,16 @@ public class FrontendEnvironmentControllerTest {
@Nested
class TestBody {
@Test
@ParameterizedTest
@ValueSource(booleans = { true, false })
@SneakyThrows
void shouldContainProduction() {
void shouldContainProduction(boolean production) {
when(environmentProperties.isProduction()).thenReturn(production);
var response = doRequest();
response.andExpect(jsonPath("$.production").value(false));
response.andExpect(jsonPath("$.production").value(production));
}
}
......@@ -58,13 +70,13 @@ public class FrontendEnvironmentControllerTest {
void shouldContainURLToRoot() {
var response = doRequest();
response.andExpect(jsonPath("$.remoteHost").value(RootController.PATH));
response.andExpect(jsonPath("$.remoteHost").value("http://localhost" + RootController.PATH));
}
}
@SneakyThrows
public ResultActions doRequest() {
private ResultActions doRequest() {
return mockMvc.perform(get(FrontendEnvironmentController.BASE_PATH));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment