Search in sources :

Example 66 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryIntegrationTests method findOne_FileAddedToRepo_FindOneSuccess.

@Test
public void findOne_FileAddedToRepo_FindOneSuccess() throws Exception {
    ConfigServerTestUtils.prepareLocalRepo();
    String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
    this.context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).run("--spring.cloud.config.server.git.uri=" + uri, "--spring.cloud.config.server.git.cloneOnStart=true");
    EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
    repository.findOne("bar", "staging", "master");
    Environment environment = repository.findOne("bar", "staging", "master");
    assertEquals("bar", environment.getPropertySources().get(0).getSource().get("foo"));
    Git git = Git.open(ResourceUtils.getFile(uri).getAbsoluteFile());
    git.checkout().setName("master").call();
    StreamUtils.copy("foo: foo", Charset.defaultCharset(), new FileOutputStream(ResourceUtils.getFile(uri + "/bar.properties")));
    git.add().addFilepattern("bar.properties").call();
    git.commit().setMessage("Updated for pull").call();
    environment = repository.findOne("bar", "staging", "master");
    assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo"));
}
Also used : Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 67 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryIntegrationTests method testShouldReturnEnvironmentFromLocalBranchInCaseRemoteDeleted.

@Test
public void testShouldReturnEnvironmentFromLocalBranchInCaseRemoteDeleted() throws Exception {
    JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
    String branchToDelete = "branchToDelete";
    testData.getServerGit().getGit().branchCreate().setName(branchToDelete).call();
    Environment environment = testData.getRepository().findOne("bar", "staging", "branchToDelete");
    assertNotNull(environment);
    testData.getServerGit().getGit().branchDelete().setBranchNames(branchToDelete).call();
    testData.getRepository().findOne("bar", "staging", "branchToDelete");
    assertNotNull(environment);
}
Also used : Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 68 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryIntegrationTests method findOne_NestedSearchPath_FindOneSuccess.

@Test
public void findOne_NestedSearchPath_FindOneSuccess() throws IOException {
    String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
    this.context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).run("--spring.cloud.config.server.git.uri=" + uri, "--spring.cloud.config.server.git.searchPaths=sub", "--spring.cloud.config.server.git.cloneOnStart=true");
    EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
    repository.findOne("bar", "staging", "master");
    Environment environment = repository.findOne("bar", "staging", "master");
    assertEquals(2, environment.getPropertySources().size());
}
Also used : SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 69 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryIntegrationTests method pull.

@Test
public void pull() throws Exception {
    ConfigServerTestUtils.prepareLocalRepo();
    String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
    this.context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).run("--spring.cloud.config.server.git.uri=" + uri);
    EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
    repository.findOne("bar", "staging", "master");
    Environment environment = repository.findOne("bar", "staging", "master");
    assertEquals("bar", environment.getPropertySources().get(0).getSource().get("foo"));
    Git git = Git.open(ResourceUtils.getFile(uri).getAbsoluteFile());
    git.checkout().setName("master").call();
    StreamUtils.copy("foo: foo", Charset.defaultCharset(), new FileOutputStream(ResourceUtils.getFile(uri + "/bar.properties")));
    git.add().addFilepattern("bar.properties").call();
    git.commit().setMessage("Updated for pull").call();
    environment = repository.findOne("bar", "staging", "master");
    assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo"));
}
Also used : Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 70 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryIntegrationTests method testNewRemoteTag.

@Test
public void testNewRemoteTag() throws Exception {
    JGitConfigServerTestData testData = JGitConfigServerTestData.prepareClonedGitRepository(TestConfiguration.class);
    Git serverGit = testData.getServerGit().getGit();
    Environment environment = testData.getRepository().findOne("bar", "staging", "master");
    Object fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
    assertEquals(fooProperty, "bar");
    serverGit.checkout().setName("master").call();
    // create a new tag
    serverGit.tag().setName("testTag").setMessage("Testing a tag").call();
    // update the remote repo
    FileOutputStream out = new FileOutputStream(new File(testData.getServerGit().getGitWorkingDirectory(), "/bar.properties"));
    StreamUtils.copy("foo: testAfterTag", Charset.defaultCharset(), out);
    testData.getServerGit().getGit().add().addFilepattern("bar.properties").call();
    testData.getServerGit().getGit().commit().setMessage("Updated for branch test").call();
    environment = testData.getRepository().findOne("bar", "staging", "master");
    fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
    assertEquals(fooProperty, "testAfterTag");
    environment = testData.getRepository().findOne("bar", "staging", "testTag");
    fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
    assertEquals(fooProperty, "bar");
    // now move the tag and test again
    serverGit.tag().setName("testTag").setForceUpdate(true).setMessage("Testing a moved tag").call();
    environment = testData.getRepository().findOne("bar", "staging", "testTag");
    fooProperty = ConfigServerTestUtils.getProperty(environment, "bar.properties", "foo");
    assertEquals(fooProperty, "testAfterTag");
}
Also used : Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) Environment(org.springframework.cloud.config.environment.Environment) File(java.io.File) Test(org.junit.Test)

Aggregations

Environment (org.springframework.cloud.config.environment.Environment)118 Test (org.junit.Test)104 StandardEnvironment (org.springframework.core.env.StandardEnvironment)37 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)20 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)19 PropertySource (org.springframework.cloud.config.environment.PropertySource)12 LinkedHashMap (java.util.LinkedHashMap)9 File (java.io.File)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)7 FileOutputStream (java.io.FileOutputStream)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 EnvironmentPropertySource.prepareEnvironment (org.springframework.cloud.config.server.support.EnvironmentPropertySource.prepareEnvironment)4 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)4 HttpEntity (org.springframework.http.HttpEntity)4 RestTemplate (org.springframework.web.client.RestTemplate)4 Git (org.eclipse.jgit.api.Git)3 Matchers.anyString (org.mockito.Matchers.anyString)3