Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature support customising path in controllers #43

Merged
merged 3 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nrich-form-configuration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
testImplementation "org.glassfish:jakarta.el"
testImplementation "org.hibernate.validator:hibernate-validator"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.junit.jupiter:junit-jupiter-params"
testImplementation "org.mockito:mockito-junit-jupiter"
testImplementation "org.springframework:spring-test"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;

@RequiredArgsConstructor
@RequestMapping("nrich/form/configuration")
@RequestMapping("${nrich.form-configuration.endpoint-path:nrich/form/configuration}")
public class FormConfigurationController {

private final FormConfigurationService formConfigurationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import net.croz.nrich.formconfiguration.api.model.FormConfiguration;
import net.croz.nrich.formconfiguration.api.request.FetchFormConfigurationRequest;
import net.croz.nrich.formconfiguration.api.service.FormConfigurationService;
import org.junit.jupiter.api.BeforeEach;
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.CsvSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -37,23 +37,19 @@ class FormConfigurationControllerTest {
@InjectMocks
private FormConfigurationController formConfigurationController;

private MockMvc mockMvc;

@BeforeEach
void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(formConfigurationController).build();
}

@Test
void shouldReturnFormConfiguration() throws Exception {
@CsvSource({ ",/nrich/form/configuration/fetch", "/api/form/configuration,/api/form/configuration/fetch" })
@ParameterizedTest
void shouldReturnFormConfiguration(String endpointPath, String uri) throws Exception {
// given
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(formConfigurationController).addPlaceholderValue("nrich.form-configuration.endpoint-path", endpointPath).build();

FetchFormConfigurationRequest request = createFetchFormConfigurationRequest();
FormConfiguration formConfiguration = createFormConfiguration();

doReturn(Collections.singletonList(formConfiguration)).when(formConfigurationService).fetchFormConfigurationList(request.getFormIdList());

// when
MockHttpServletResponse response = mockMvc.perform(post("/nrich/form/configuration/fetch")
MockHttpServletResponse response = mockMvc.perform(post(uri)
.content(objectMapper.writeValueAsString(request)).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

@RequiredArgsConstructor
@RequestMapping("${croz.nrich.registry.domain:}/nrich/registry/configuration")
@RequestMapping("${nrich.registry.configuration.endpoint-path:nrich/registry/configuration}")
@ResponseBody
public class RegistryConfigurationController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Map;

@RequiredArgsConstructor
@RequestMapping("${croz.nrich.registry.domain:}/nrich/registry/data")
@RequestMapping("${nrich.registry.data.endpoint-path:nrich/registry/data}")
@ResponseBody
public class RegistryDataController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import javax.validation.Valid;

@RequiredArgsConstructor
@RequestMapping("${croz.nrich.registry.domain:}/nrich/registry/history")
@RequestMapping("${nrich.registry.history.endpoint-path:nrich/registry/history}")
@ResponseBody
public class RegistryHistoryController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

@TestPropertySource(locations = "/application-test.yml")
@TestPropertySource(properties = "nrich.registry.configuration.endpoint-path=api/registry/configuration")
class RegistryConfigurationControllerEndpointTest extends BaseWebTest {

@Test
void shouldFetchRegistryConfiguration() throws Exception {
// when
MockHttpServletResponse response = mockMvc.perform(post("/domain/nrich/registry/configuration/fetch").contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse();
MockHttpServletResponse response = mockMvc.perform(post("/api/registry/configuration/fetch").contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

@TestPropertySource(locations = "/application-test.yml")
@TestPropertySource(properties = "nrich.registry.data.endpoint-path=api/registry/data")
class RegistryDataControllerEndpointTest extends BaseWebTest {

@Test
Expand All @@ -23,7 +23,7 @@ void shouldListRegistryOnCustomDomain() throws Exception {

// when
MockHttpServletResponse response = mockMvc.perform(
post("/domain/nrich/registry/data/list").contentType(MediaType.APPLICATION_JSON)
post("/api/registry/data/list").contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))).andReturn().getResponse();

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

@TestPropertySource(locations = "/application-test.yml")
@TestPropertySource(properties = "nrich.registry.history.endpoint-path=api/registry/history")
class RegistryHistoryControllerEndpointTest extends BaseWebTest {

@Test
Expand All @@ -22,7 +22,7 @@ void shouldFetchRegistryHistoryList() throws Exception {
ListRegistryHistoryRequest request = listRegistryHistoryRequest(RegistryHistoryTestEntity.class.getName(), 1L);

// when
MockHttpServletResponse response = mockMvc.perform(post("/domain/nrich/registry/history/list")
MockHttpServletResponse response = mockMvc.perform(post("/api/registry/history/list")
.contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(request))).andReturn().getResponse();

// then
Expand Down
1 change: 0 additions & 1 deletion nrich-registry/src/test/resources/application-test.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.croz.nrich.security.csrf.configuration;

import net.croz.nrich.security.csrf.api.service.CsrfTokenManagerService;
import net.croz.nrich.security.csrf.core.constants.CsrfConstants;
import net.croz.nrich.security.csrf.core.controller.CsrfPingController;
import net.croz.nrich.security.csrf.core.service.AesCsrfTokenManagerService;
import net.croz.nrich.security.csrf.properties.NrichCsrfProperties;
Expand All @@ -28,7 +27,7 @@ public CsrfTokenManagerService tokenManagerService(NrichCsrfProperties csrfPrope
return new AesCsrfTokenManagerService(csrfProperties.getTokenExpirationInterval(), csrfProperties.getTokenFutureThreshold(), csrfProperties.getCryptoKeyLength());
}

@ConditionalOnProperty(name = "nrich.security.csrf.csrf-ping-url", havingValue = CsrfConstants.CSRF_DEFAULT_PING_URI, matchIfMissing = true)
@ConditionalOnMissingBean
@Bean
public CsrfPingController csrfPingController() {
return new CsrfPingController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CsrfPingController {

private static final String SUCCESS_KEY = "success";

@RequestMapping(CsrfConstants.CSRF_DEFAULT_PING_URI)
@RequestMapping("${nrich.security.csrf.endpoint-path:" + CsrfConstants.CSRF_DEFAULT_PING_URI + "}")
@ResponseBody
public Map<String, Boolean> ping() {
Map<String, Boolean> result = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.croz.nrich.security.csrf.core.controller;

import net.croz.nrich.security.csrf.core.constants.CsrfConstants;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
Expand All @@ -13,12 +13,14 @@

class CsrfPingControllerTest {

private final MockMvc mockMvc = MockMvcBuilders.standaloneSetup(CsrfPingController.class).build();
@CsvSource({ ",/nrich/csrf/ping", "/api/csrf/ping,/api/csrf/ping" })
@ParameterizedTest
void shouldReturnPingRequest(String endpointPath, String uri) throws Exception {
// given
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(CsrfPingController.class).addPlaceholderValue("nrich.security.csrf.endpoint-path", endpointPath).build();

@Test
void shouldReturnPingRequest() throws Exception {
// when
MockHttpServletResponse response = mockMvc.perform(post(CsrfConstants.CSRF_DEFAULT_PING_URI).accept(MediaType.APPLICATION_JSON)
MockHttpServletResponse response = mockMvc.perform(post(uri).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse();

// then
Expand Down