Merge branch '3.1.x'

This commit is contained in:
Ryan Baxter
2022-02-15 11:47:40 -05:00
4 changed files with 294 additions and 255 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -103,12 +103,12 @@ public class EnvironmentController {
this.acceptEmpty = acceptEmpty;
}
@GetMapping(path = "/{name}/{profiles:.*[^-].*}", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(path = "/{name}/{profiles:[^-]+}", produces = MediaType.APPLICATION_JSON_VALUE)
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
return getEnvironment(name, profiles, null, false);
}
@GetMapping(path = "/{name}/{profiles:.*[^-].*}", produces = EnvironmentMediaType.V2_JSON)
@GetMapping(path = "/{name}/{profiles:[^-]+}", produces = EnvironmentMediaType.V2_JSON)
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles) {
return getEnvironment(name, profiles, null, true);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,8 +35,6 @@ import org.springframework.cloud.config.server.encryption.KeyStoreTextEncryptorL
import org.springframework.cloud.config.server.environment.AwsParameterStoreEnvironmentRepositoryTests;
import org.springframework.cloud.config.server.environment.AwsS3EnvironmentRepositoryTests;
import org.springframework.cloud.config.server.environment.CompositeEnvironmentRepositoryTests;
import org.springframework.cloud.config.server.environment.EnvironmentControllerIntegrationTests;
import org.springframework.cloud.config.server.environment.EnvironmentControllerTests;
import org.springframework.cloud.config.server.environment.EnvironmentEncryptorEnvironmentRepositoryTests;
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryConcurrencyTests;
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryIntegrationTests;
@@ -67,8 +65,7 @@ import org.springframework.cloud.config.server.ssh.SshUriPropertyProcessorTest;
@SuiteClasses({ NativeConfigServerIntegrationTests.class, GenericResourceRepositoryTests.class,
ResourceControllerTests.class, ResourceControllerIntegrationTests.class, ConfigClientOnIntegrationTests.class,
ConfigServerApplicationTests.class, VanillaConfigServerIntegrationTests.class,
EnvironmentControllerIntegrationTests.class, MultipleJGitEnvironmentRepositoryIntegrationTests.class,
EnvironmentEncryptorEnvironmentRepositoryTests.class, EnvironmentControllerTests.class,
MultipleJGitEnvironmentRepositoryIntegrationTests.class, EnvironmentEncryptorEnvironmentRepositoryTests.class,
SVNKitEnvironmentRepositoryIntegrationTests.class,
MultipleJGitEnvironmentApplicationPlaceholderRepositoryTests.class, JdbcEnvironmentRepositoryTests.class,
CompositeEnvironmentRepositoryTests.class, JGitEnvironmentRepositoryConcurrencyTests.class,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,27 +19,25 @@ package org.springframework.cloud.config.server.environment;
import java.util.HashMap;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.EnvironmentControllerIntegrationTests.ControllerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -48,131 +46,144 @@ import static org.mockito.Mockito.when;
* @author Dave Syer
* @author Roy Clarkson
* @author Ivan Corrales Solera
* @author Henning Pöttker
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ControllerConfiguration.class)
public class EnvironmentControllerIntegrationTests {
class EnvironmentControllerIntegrationTests {
@Autowired
private WebApplicationContext context;
abstract static class TestCases {
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
@Autowired
private EnvironmentRepository repository;
private MockMvc mvc;
private Environment environment = new Environment("foo", "default");
@Autowired
private EnvironmentRepository repository;
private final Environment environment = new Environment("foo", "default");
@BeforeEach
public void init() {
Mockito.reset(this.repository);
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.environment.add(new PropertySource("foo", new HashMap<>()));
}
@Test
public void environmentNoLabel() throws Exception {
when(this.repository.findOne("foo", "default", null, false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", null, false);
}
@Test
public void propertiesNoLabel() throws Exception {
when(this.repository.findOne("foo", "default", null, false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", null, false);
}
@Test
public void propertiesLabel() throws Exception {
when(this.repository.findOne("foo", "default", "label", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", "label", false);
}
@Test
public void propertiesLabelWhenApplicationNameContainsHyphen() throws Exception {
Environment environment = new Environment("foo-bar", "default");
environment.add(new PropertySource("foo", new HashMap<>()));
when(this.repository.findOne("foo-bar", "default", "label", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-bar-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo-bar", "default", "label", false);
}
@Test
public void propertiesLabelWithSlash() throws Exception {
when(this.repository.findOne("foo", "default", "label/spam", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/label(_)spam/foo-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", "label/spam", false);
}
@Test
public void environmentWithLabel() throws Exception {
when(this.repository.findOne("foo", "default", "awesome", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/awesome"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void environmentWithMissingLabel() throws Exception {
when(this.repository.findOne("foo", "default", "missing", false))
.thenThrow(new NoSuchLabelException("Planned"));
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
public void environmentWithMissingRepo() throws Exception {
when(this.repository.findOne("foo", "default", "missing", false))
.thenThrow(new NoSuchRepositoryException("Planned"));
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
public void environmentWithLabelContainingPeriod() throws Exception {
when(this.repository.findOne("foo", "default", "1.0.0", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/1.0.0"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void environmentWithLabelContainingSlash() throws Exception {
when(this.repository.findOne("foo", "default", "feature/puff", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/feature(_)puff"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"propertySources\":")));
}
@Test
public void environmentWithApplicationContainingSlash() throws Exception {
Environment environment = new Environment("foo/app", "default");
environment.add(new PropertySource("foo", new HashMap<>()));
when(this.repository.findOne("foo/app", "default", null, false)).thenReturn(environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo(_)app/default"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"propertySources\":")));
}
@Before
public void init() {
Mockito.reset(this.repository);
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.environment.add(new PropertySource("foo", new HashMap<>()));
}
@Test
public void environmentNoLabel() throws Exception {
when(this.repository.findOne("foo", "default", null, false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default")).andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", null, false);
@SpringBootTest(classes = ControllerConfiguration.class)
static class PathPatternParserTests extends TestCases {
}
@Test
public void propertiesNoLabel() throws Exception {
when(this.repository.findOne("foo", "default", null, false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", null, false);
}
@SpringBootTest(classes = ControllerConfiguration.class)
@TestPropertySource(properties = "spring.mvc.pathmatch.matching-strategy=ant_path_matcher")
static class AntPathMatcherTests extends TestCases {
@Test
public void propertiesLabel() throws Exception {
when(this.repository.findOne("foo", "default", "label", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", "label", false);
}
@Test
public void propertiesLabelWhenApplicationNameContainsHyphen() throws Exception {
Environment environment = new Environment("foo-bar", "default");
environment.add(new PropertySource("foo", new HashMap<>()));
when(this.repository.findOne("foo-bar", "default", "label", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-bar-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo-bar", "default", "label", false);
}
@Test
public void propertiesLabelWithSlash() throws Exception {
when(this.repository.findOne("foo", "default", "label/spam", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/label(_)spam/foo-default.properties"))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(this.repository).findOne("foo", "default", "label/spam", false);
}
@Test
public void environmentWithLabel() throws Exception {
when(this.repository.findOne("foo", "default", "awesome", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/awesome"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void environmentWithMissingLabel() throws Exception {
when(this.repository.findOne("foo", "default", "missing", false))
.thenThrow(new NoSuchLabelException("Planned"));
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
public void environmentWithMissingRepo() throws Exception {
when(this.repository.findOne("foo", "default", "missing", false))
.thenThrow(new NoSuchRepositoryException("Planned"));
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
public void environmentWithLabelContainingPeriod() throws Exception {
when(this.repository.findOne("foo", "default", "1.0.0", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/1.0.0"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void environmentWithLabelContainingSlash() throws Exception {
when(this.repository.findOne("foo", "default", "feature/puff", false)).thenReturn(this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/feature(_)puff"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"propertySources\":")));
}
@Test
public void environmentWithApplicationContainingSlash() throws Exception {
Environment environment = new Environment("foo/app", "default");
environment.add(new PropertySource("foo", new HashMap<>()));
when(this.repository.findOne("foo/app", "default", null, false)).thenReturn(environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo(_)app/default"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"propertySources\":")));
}
@Configuration
@EnableWebMvc
@Import(PropertyPlaceholderAutoConfiguration.class)
public static class ControllerConfiguration {
@Import({ PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class })
static class ControllerConfiguration {
@Bean
public EnvironmentRepository environmentRepository() {
EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);
return repository;
EnvironmentRepository environmentRepository() {
return Mockito.mock(EnvironmentRepository.class);
}
@Bean
public EnvironmentController controller() {
EnvironmentController controller() {
return new EnvironmentController(environmentRepository());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,11 +24,10 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.yaml.snakeyaml.Yaml;
@@ -41,8 +40,11 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@@ -55,25 +57,23 @@ import static org.mockito.Mockito.when;
* @author Ivan Corrales Solera
* @author Daniel Frey
* @author Ian Bondoc
* @author Henning Pöttker
*/
public class EnvironmentControllerTests {
class EnvironmentControllerTests {
@Rule
public ExpectedException expected = ExpectedException.none();
private EnvironmentRepository repository = mock(EnvironmentRepository.class);
private final EnvironmentRepository repository = mock(EnvironmentRepository.class);
private EnvironmentController controller;
private Environment environment = new Environment("foo", "master");
private final Environment environment = new Environment("foo", "master");
@Before
@BeforeEach
public void init() {
this.controller = new EnvironmentController(this.repository);
this.environment.add(new PropertySource("foo", new HashMap<>()));
}
@After
@AfterEach
public void clean() {
System.clearProperty("foo");
}
@@ -222,10 +222,11 @@ public class EnvironmentControllerTests {
}
@Test(expected = EnvironmentNotFoundException.class)
@Test
public void testEnvironmentNotFound() {
this.controller.setAcceptEmpty(false);
this.controller.labelled("foo", "bar", null);
assertThatThrownBy(() -> this.controller.labelled("foo", "bar", null))
.isInstanceOf(EnvironmentNotFoundException.class);
}
@Test
@@ -508,142 +509,172 @@ public class EnvironmentControllerTests {
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
}
@Test
public void mappingForEnvironment() throws Exception {
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo/bar")).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void mappingForLabelledEnvironment() throws Exception {
when(this.repository.findOne("foo", "bar", "other", false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo/bar/other")).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void environmentMissing() throws Exception {
when(this.repository.findOne("foo1", "notfound", null, false))
.thenThrow(new EnvironmentNotFoundException("Missing Environment"));
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo1/notfound"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
public void mappingForYaml() throws Exception {
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo-bar.yml"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN))
.andExpect(MockMvcResultMatchers.content().string("{}\n"));
}
@Test
public void mappingForJson() throws Exception {
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo-bar.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.content().string("{}"));
}
@Test
public void mappingForLabelledYaml() throws Exception {
when(this.repository.findOne("foo", "bar", "other", false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar.yml"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingForLabelledProperties() throws Exception {
when(this.repository.findOne("foo", "bar", "other", false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar.properties"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingForProperties() throws Exception {
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo-bar.properties"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingForLabelledYamlWithHyphen() throws Exception {
when(this.repository.findOne("foo-bar-foo2-bar2", "spam", "other", false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar-foo2-bar2-spam.yml"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingforLabelledJsonProperties() throws Exception {
when(this.repository.findOne("foo", "bar", "other", false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void mappingforJsonProperties() throws Exception {
when(this.repository.findOne("foo", "bar", null, false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/foo-bar.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void mappingForLabelledJsonPropertiesWithHyphen() throws Exception {
when(this.repository.findOne("foo-bar-foo2-bar2", "spam", "other", false)).thenReturn(this.environment);
MockMvc mvc = MockMvcBuilders.standaloneSetup(this.controller).build();
mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar-foo2-bar2-spam.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void handleEnvironmentException() throws Exception {
when(repository.findOne(eq("exception"), eq("bad-syntax.ext"), any(), eq(false))).thenThrow(
new FailedToConstructEnvironmentException("Cannot construct", new RuntimeException("underlier")));
MockMvc mvc = MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(controller).build();
MvcResult result = mvc.perform(MockMvcRequestBuilders.get("/exception/bad-syntax.ext"))
.andExpect(MockMvcResultMatchers.status().is(500)).andReturn();
assertThat(result.getResponse().getErrorMessage()).isEqualTo("Cannot construct");
}
@Test
public void labelWithPreviousDirectory() {
expected.expect(InvalidEnvironmentRequestException.class);
this.controller.labelled("foo", "bar", "..(_)..");
assertThatThrownBy(() -> this.controller.labelled("foo", "bar", "..(_).."))
.isInstanceOf(InvalidEnvironmentRequestException.class);
}
@Test
public void labelWithPreviousDirectoryEncodedParenthesis() {
expected.expect(InvalidEnvironmentRequestException.class);
this.controller.labelled("foo", "bar", "..%28_%29..");
assertThatThrownBy(() -> this.controller.labelled("foo", "bar", "..%28_%29.."))
.isInstanceOf(InvalidEnvironmentRequestException.class);
}
@Test
public void labelWithPreviousDirectoryAllEncoded() {
expected.expect(InvalidEnvironmentRequestException.class);
this.controller.labelled("foo", "bar", "%2E%2E%28%5F%29%2E%2E");
assertThatThrownBy(() -> this.controller.labelled("foo", "bar", "%2E%2E%28%5F%29%2E%2E"))
.isInstanceOf(InvalidEnvironmentRequestException.class);
}
@Test
public void nameWithPound() {
expected.expect(InvalidEnvironmentRequestException.class);
this.controller.labelled("foo#", "bar", "mylabel");
assertThatThrownBy(() -> this.controller.labelled("foo#", "bar", "mylabel"))
.isInstanceOf(InvalidEnvironmentRequestException.class);
}
@Test
public void nameWithPoundEncoded() {
expected.expect(InvalidEnvironmentRequestException.class);
this.controller.labelled("foo%23", "bar", "mylabel");
assertThatThrownBy(() -> this.controller.labelled("foo%23", "bar", "mylabel"))
.isInstanceOf(InvalidEnvironmentRequestException.class);
}
abstract class MockMvcTestCases {
protected MockMvc mvc;
@Test
public void mappingForEnvironment() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", null, false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/bar")).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void mappingForLabelledEnvironment() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", "other", false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo/bar/other"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void environmentMissing() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo1", "notfound", null, false))
.thenThrow(new EnvironmentNotFoundException("Missing Environment"));
this.mvc.perform(MockMvcRequestBuilders.get("/foo1/notfound"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
public void mappingForYaml() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", null, false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo-bar.yml"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN))
.andExpect(MockMvcResultMatchers.content().string("{}\n"));
}
@Test
public void mappingForJson() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", null, false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo-bar.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.content().string("{}"));
}
@Test
public void mappingForLabelledYaml() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", "other", false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar.yml"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingForLabelledProperties() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", "other", false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar.properties"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingForProperties() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", null, false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo-bar.properties"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingForLabelledYamlWithHyphen() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo-bar-foo2-bar2", "spam", "other", false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar-foo2-bar2-spam.yml"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.TEXT_PLAIN));
}
@Test
public void mappingforLabelledJsonProperties() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", "other", false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void mappingforJsonProperties() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo", "bar", null, false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/foo-bar.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void mappingForLabelledJsonPropertiesWithHyphen() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne("foo-bar-foo2-bar2", "spam", "other", false))
.thenReturn(EnvironmentControllerTests.this.environment);
this.mvc.perform(MockMvcRequestBuilders.get("/other/foo-bar-foo2-bar2-spam.json"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void handleEnvironmentException() throws Exception {
when(EnvironmentControllerTests.this.repository.findOne(eq("exception"), eq("bad_syntax.ext"), any(),
eq(false)))
.thenThrow(new FailedToConstructEnvironmentException("Cannot construct",
new RuntimeException("underlier")));
MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/exception/bad_syntax.ext"))
.andExpect(MockMvcResultMatchers.status().is(500)).andReturn();
assertThat(result.getResponse().getErrorMessage()).isEqualTo("Cannot construct");
}
}
@Nested
class PathPatternParserTests extends MockMvcTestCases {
@BeforeEach
public void initMockMvc() {
StandaloneMockMvcBuilder builder = MockMvcBuilders
.standaloneSetup(EnvironmentControllerTests.this.controller);
builder.setPatternParser(new PathPatternParser());
this.mvc = builder.build();
}
}
@Nested
class AntPathMatcherTests extends MockMvcTestCases {
@BeforeEach
public void initMockMvc() {
this.mvc = MockMvcBuilders.standaloneSetup(EnvironmentControllerTests.this.controller).build();
}
}
}