diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java index b5f6e53d..f5d20db1 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java @@ -42,6 +42,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Yaml; @@ -223,7 +224,7 @@ public class EnvironmentController { return rootMap; } - @ExceptionHandler(NoSuchLabelException.class) + @ExceptionHandler(RepositoryException.class) public void noSuchLabel(HttpServletResponse response) throws IOException { response.sendError(HttpStatus.NOT_FOUND.value()); } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java index f94cd97e..c28370de 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java @@ -39,6 +39,7 @@ import org.eclipse.jgit.api.TransportCommand; import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.RefNotFoundException; +import org.eclipse.jgit.errors.NoRemoteRepositoryException; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.FetchResult; @@ -208,8 +209,10 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository return git.getRepository().findRef("HEAD").getObjectId().getName(); } catch (RefNotFoundException e) { throw new NoSuchLabelException("No such label: " + label, e); + } catch (NoRemoteRepositoryException e) { + throw new NoSuchRepositoryException("No such repository: " + getUri(), e); } catch (GitAPIException e) { - throw new IllegalStateException("Cannot clone or checkout repository", e); + throw new NoSuchRepositoryException("Cannot clone or checkout repository: " + getUri(), e); } catch (Exception e) { throw new IllegalStateException("Cannot load environment", e); } finally { diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NoSuchRepositoryException.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NoSuchRepositoryException.java new file mode 100644 index 00000000..19aad8b3 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NoSuchRepositoryException.java @@ -0,0 +1,34 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.config.server.environment; + +/** + * @author Dave Syer + * + */ +@SuppressWarnings("serial") +public class NoSuchRepositoryException extends RepositoryException { + + public NoSuchRepositoryException(String string) { + super(string); + } + + public NoSuchRepositoryException(String string, Exception e) { + super(string, e); + } + +} diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java index 6a9e373a..96001840 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java @@ -22,7 +22,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.environment.EnvironmentControllerIntegrationTests.ControllerConfiguration; @@ -60,71 +60,83 @@ public class EnvironmentControllerIntegrationTests { @Test public void environmentNoLabel() throws Exception { - Mockito.when(this.repository.findOne("foo", "default", null)).thenReturn( - new Environment("foo", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/foo/default")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo", "default", null)).thenReturn(new Environment("foo", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/foo/default")).andExpect(MockMvcResultMatchers.status().isOk()); Mockito.verify(this.repository).findOne("foo", "default", null); } @Test public void propertiesNoLabel() throws Exception { - Mockito.when(this.repository.findOne("foo", "default", null)).thenReturn( - new Environment("foo", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/foo-default.properties")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo", "default", null)).thenReturn(new Environment("foo", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/foo-default.properties")) + .andExpect(MockMvcResultMatchers.status().isOk()); Mockito.verify(this.repository).findOne("foo", "default", null); } @Test public void propertiesLabel() throws Exception { - Mockito.when(this.repository.findOne("foo", "default", "label")).thenReturn( - new Environment("foo", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-default.properties")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo", "default", "label")).thenReturn(new Environment("foo", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-default.properties")) + .andExpect(MockMvcResultMatchers.status().isOk()); Mockito.verify(this.repository).findOne("foo", "default", "label"); } @Test public void propertiesLabelWhenApplicationNameContainsHyphen() throws Exception { - Mockito.when(this.repository.findOne("foo-bar", "default", "label")).thenReturn(new Environment("foo-bar", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-bar-default.properties")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo-bar", "default", "label")) + .thenReturn(new Environment("foo-bar", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-bar-default.properties")) + .andExpect(MockMvcResultMatchers.status().isOk()); Mockito.verify(this.repository).findOne("foo-bar", "default", "label"); } @Test public void propertiesLabelWithSlash() throws Exception { - Mockito.when(this.repository.findOne("foo", "default", "label/spam")).thenReturn( - new Environment("foo", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/label(_)spam/foo-default.properties")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo", "default", "label/spam")) + .thenReturn(new Environment("foo", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/label(_)spam/foo-default.properties")) + .andExpect(MockMvcResultMatchers.status().isOk()); Mockito.verify(this.repository).findOne("foo", "default", "label/spam"); } @Test public void environmentWithLabel() throws Exception { - Mockito.when(this.repository.findOne("foo", "default", "awesome")).thenReturn( - new Environment("foo", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/awesome")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo", "default", "awesome")) + .thenReturn(new Environment("foo", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/awesome")) + .andExpect(MockMvcResultMatchers.status().isOk()); + } + + @Test + public void environmentWithMissingLabel() throws Exception { + Mockito.when(this.repository.findOne("foo", "default", "missing")) + .thenThrow(new NoSuchLabelException("Planned")); + this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing")) + .andExpect(MockMvcResultMatchers.status().isNotFound()); + } + + @Test + public void environmentWithMissingRepo() throws Exception { + Mockito.when(this.repository.findOne("foo", "default", "missing")) + .thenThrow(new NoSuchRepositoryException("Planned")); + this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing")) + .andExpect(MockMvcResultMatchers.status().isNotFound()); } @Test public void environmentWithLabelContainingPeriod() throws Exception { - Mockito.when(this.repository.findOne("foo", "default", "1.0.0")).thenReturn( - new Environment("foo", "default")); - this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/1.0.0")).andExpect( - MockMvcResultMatchers.status().isOk()); + Mockito.when(this.repository.findOne("foo", "default", "1.0.0")).thenReturn(new Environment("foo", "default")); + this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/1.0.0")) + .andExpect(MockMvcResultMatchers.status().isOk()); } @Test public void environmentWithLabelContainingSlash() throws Exception { Mockito.when(this.repository.findOne("foo", "default", "feature/puff")) - .thenReturn(new Environment("foo", "default")); + .thenReturn(new Environment("foo", "default")); this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/feature(_)puff")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"propertySources\":"))); + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"propertySources\":"))); } @Configuration