Search in sources :

Example 1 with PropertySource

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

the class CompositeEnvironmentRepositoryTests method testOrder.

@Test
public void testOrder() {
    PropertySource p1 = mock(PropertySource.class);
    doReturn("p1").when(p1).getName();
    PropertySource p2 = mock(PropertySource.class);
    doReturn("p2").when(p2).getName();
    PropertySource p3 = mock(PropertySource.class);
    doReturn("p3").when(p3).getName();
    PropertySource p4 = mock(PropertySource.class);
    doReturn("p4").when(p4).getName();
    PropertySource p5 = mock(PropertySource.class);
    doReturn("p5").when(p5).getName();
    String sLoc1 = "loc1";
    String sLoc2 = "loc2";
    String sLoc3 = "loc3";
    String sLoc4 = "loc4";
    String sLoc5 = "loc5";
    Environment e1 = new Environment("app", "dev");
    e1.add(p1);
    e1.add(p5);
    Environment e2 = new Environment("app", "dev");
    e2.add(p2);
    Environment e3 = new Environment("app", "dev");
    e3.add(p3);
    e3.add(p4);
    SearchPathLocator.Locations loc1 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[] { sLoc1 });
    SearchPathLocator.Locations loc2 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[] { sLoc5, sLoc4 });
    SearchPathLocator.Locations loc3 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[] { sLoc3, sLoc2 });
    List<EnvironmentRepository> repos = new ArrayList<EnvironmentRepository>();
    repos.add(new TestOrderedEnvironmentRepository(3, e1, loc1));
    repos.add(new TestOrderedEnvironmentRepository(2, e3, loc2));
    repos.add(new TestOrderedEnvironmentRepository(1, e2, loc3));
    SearchPathCompositeEnvironmentRepository compositeRepo = new SearchPathCompositeEnvironmentRepository(repos);
    Environment compositeEnv = compositeRepo.findOne("foo", "bar", "world");
    List<PropertySource> propertySources = compositeEnv.getPropertySources();
    assertEquals(5, propertySources.size());
    assertEquals("p2", propertySources.get(0).getName());
    assertEquals("p3", propertySources.get(1).getName());
    assertEquals("p4", propertySources.get(2).getName());
    assertEquals("p1", propertySources.get(3).getName());
    assertEquals("p5", propertySources.get(4).getName());
    SearchPathLocator.Locations locations = compositeRepo.getLocations("app", "dev", "label");
    String[] locationStrings = locations.getLocations();
    assertEquals(5, locationStrings.length);
    assertEquals(sLoc3, locationStrings[0]);
    assertEquals(sLoc2, locationStrings[1]);
    assertEquals(sLoc5, locationStrings[2]);
    assertEquals(sLoc4, locationStrings[3]);
    assertEquals(sLoc1, locationStrings[4]);
}
Also used : ArrayList(java.util.ArrayList) PropertySource(org.springframework.cloud.config.environment.PropertySource) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 2 with PropertySource

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

the class CompositeEnvironmentRepositoryTests method testVersion.

@Test
public void testVersion() {
    PropertySource p1 = mock(PropertySource.class);
    doReturn("p1").when(p1).getName();
    PropertySource p2 = mock(PropertySource.class);
    doReturn("p2").when(p2).getName();
    String sLoc1 = "loc1";
    String sLoc2 = "loc2";
    Environment e1 = new Environment("app", "dev");
    e1.add(p1);
    e1.setVersion("1");
    e1.setState("state");
    Environment e2 = new Environment("app", "dev");
    e2.add(p2);
    e2.setVersion("2");
    e2.setState("state2");
    SearchPathLocator.Locations loc1 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[] { sLoc1 });
    SearchPathLocator.Locations loc2 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[] { sLoc1, sLoc2 });
    List<EnvironmentRepository> repos = new ArrayList<EnvironmentRepository>();
    repos.add(new TestOrderedEnvironmentRepository(3, e1, loc1));
    List<EnvironmentRepository> repos2 = new ArrayList<EnvironmentRepository>();
    repos2.add(new TestOrderedEnvironmentRepository(3, e1, loc1));
    repos2.add(new TestOrderedEnvironmentRepository(3, e2, loc2));
    SearchPathCompositeEnvironmentRepository compositeRepo = new SearchPathCompositeEnvironmentRepository(repos);
    SearchPathCompositeEnvironmentRepository multiCompositeRepo = new SearchPathCompositeEnvironmentRepository(repos2);
    Environment env = compositeRepo.findOne("app", "dev", "label");
    assertEquals("1", env.getVersion());
    assertEquals("state", env.getState());
    Environment multiEnv = multiCompositeRepo.findOne("app", "dev", "label");
    assertEquals(null, multiEnv.getVersion());
    assertEquals(null, multiEnv.getState());
}
Also used : ArrayList(java.util.ArrayList) Environment(org.springframework.cloud.config.environment.Environment) PropertySource(org.springframework.cloud.config.environment.PropertySource) Test(org.junit.Test)

Example 3 with PropertySource

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

the class EnvironmentControllerTests method whenPlaceholdersSystemProps.

private void whenPlaceholdersSystemProps() {
    System.setProperty("foo", "bar");
    this.environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}")));
    Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
}
Also used : PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 4 with PropertySource

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

the class EnvironmentControllerTests method arrayInYaml.

@Test
public void arrayInYaml() throws Exception {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("a.b[0]", "c");
    map.put("a.b[1]", "d");
    this.environment.add(new PropertySource("one", map));
    Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
    String yaml = this.controller.yaml("foo", "bar", false).getBody();
    assertEquals("a:\n  b:\n  - c\n  - d\n", yaml);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) PropertySource(org.springframework.cloud.config.environment.PropertySource) Test(org.junit.Test)

Example 5 with PropertySource

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

the class EnvironmentControllerTests method arrayObObjectAtTopLevelInYaml.

@Test
public void arrayObObjectAtTopLevelInYaml() throws Exception {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("document[0].a", "c");
    map.put("document[1].a", "d");
    this.environment.add(new PropertySource("one", map));
    Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
    String yaml = this.controller.yaml("foo", "bar", false).getBody();
    assertEquals("- a: c\n- a: d\n", yaml);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) PropertySource(org.springframework.cloud.config.environment.PropertySource) Test(org.junit.Test)

Aggregations

PropertySource (org.springframework.cloud.config.environment.PropertySource)33 Test (org.junit.Test)20 LinkedHashMap (java.util.LinkedHashMap)19 Environment (org.springframework.cloud.config.environment.Environment)12 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 MapPropertySource (org.springframework.core.env.MapPropertySource)4 CompositePropertySource (org.springframework.core.env.CompositePropertySource)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 YamlPropertiesFactoryBean (org.springframework.beans.factory.config.YamlPropertiesFactoryBean)1 EnvironmentRepository (org.springframework.cloud.config.server.environment.EnvironmentRepository)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1