Skip to content
Snippets Groups Projects
Commit cad28fdc authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4814 Removed all label references from MongoEnvironmentRepository

parent 684b3b3d
No related branches found
No related tags found
No related merge requests found
...@@ -2,10 +2,11 @@ package de.ozgcloud.admin; ...@@ -2,10 +2,11 @@ package de.ozgcloud.admin;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import de.ozgcloud.admin.environment.EnableMongoConfigServer;
@SpringBootApplication @SpringBootApplication
@EnableConfigServer @EnableMongoConfigServer
public class AdministrationApplication { public class AdministrationApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -5,13 +5,12 @@ import java.lang.annotation.Retention; ...@@ -5,13 +5,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Import(MongoEnvironmentRepositoryConfiguration.class) @Import(MongoEnvironmentRepositoryConfiguration.class)
@EnableConfigServer @EnableMongoConfigServer
public @interface EnableMongoConfigServer { public @interface EnableMongoConfigServer {
} }
...@@ -9,13 +9,12 @@ import org.springframework.cloud.config.server.environment.EnvironmentRepository ...@@ -9,13 +9,12 @@ import org.springframework.cloud.config.server.environment.EnvironmentRepository
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
@Component // @Component
public class MongoEnvironmentRepository implements EnvironmentRepository { public class MongoEnvironmentRepository implements EnvironmentRepository {
private static final String LABEL = "label"; public static final String COLLECTION_NAME = "configuration_properties";
private static final String APPLICATION = "application";
private static final String PROFILE = "profile"; private static final String PROFILE = "profile";
private static final String DEFAULT = "default";
private MongoOperations mongoOperations; private MongoOperations mongoOperations;
...@@ -25,30 +24,29 @@ public class MongoEnvironmentRepository implements EnvironmentRepository { ...@@ -25,30 +24,29 @@ public class MongoEnvironmentRepository implements EnvironmentRepository {
@Override @Override
public Environment findOne(String application, String profile, String label) { public Environment findOne(String application, String profile, String label) {
String[] profiles = splitInputProfileIntoArrayAndAddDefaultProfile(profile); String[] profiles = splitInputProfileIntoArray(profile);
Query query = buildQuery(profiles, label); Query query = buildMongoQuery(application, profiles);
List<MongoPropertySource> sources = findMatchingEntriesInMongoDB(query, application); List<MongoPropertySource> matchingPropertyEntries = findMatchingEntriesInMongoDB(query, COLLECTION_NAME);
Environment environment = new Environment(application, profiles, label == null ? DEFAULT : label, null, null); Environment environment = new Environment(application, profiles, null, null, null);
environment.addAll(sources.stream().map(this::transformMonPropertySourceToPropertySource).collect(Collectors.toList())); environment.addAll(matchingPropertyEntries.stream().map(this::transformMonPropertySourceToPropertySource).collect(Collectors.toList()));
return environment; return environment;
} }
private String[] splitInputProfileIntoArrayAndAddDefaultProfile(String profile) { private String[] splitInputProfileIntoArray(String profile) {
return (profile + "," + DEFAULT).split(","); return (profile).split(",");
} }
private Query buildQuery(String[] profiles, String label) { private Query buildMongoQuery(String application, String[] profiles) {
Query query = new Query(); Query query = new Query();
query.addCriteria(Criteria.where(LABEL).in(label, null)); query.addCriteria(Criteria.where(APPLICATION).in(application));
query.addCriteria(Criteria.where(PROFILE).in(profiles)); query.addCriteria(Criteria.where(PROFILE).in((Object[]) profiles));
return query; return query;
} }
private PropertySource transformMonPropertySourceToPropertySource(MongoPropertySource mongoPropertySource) { private PropertySource transformMonPropertySourceToPropertySource(MongoPropertySource mongoPropertySource) {
String sourceName = ((mongoPropertySource.getProfile() == null) ? DEFAULT : mongoPropertySource.getProfile()) + "-" String sourceName = mongoPropertySource.getApplication() + '-' + mongoPropertySource.getProfile();
+ ((mongoPropertySource.getLabel() == null) ? DEFAULT : mongoPropertySource.getLabel());
return new PropertySource(sourceName, mongoPropertySource.getSource()); return new PropertySource(sourceName, mongoPropertySource.getSource());
} }
......
package de.ozgcloud.admin.environment; package de.ozgcloud.admin.environment;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
...@@ -12,8 +13,12 @@ public class MongoEnvironmentRepositoryConfiguration { ...@@ -12,8 +13,12 @@ public class MongoEnvironmentRepositoryConfiguration {
private MongoOperations mongoOperations; private MongoOperations mongoOperations;
@Bean @Bean
public MongoEnvironmentRepository mongoEnvironmentRepository() { public EnvironmentRepository environmentRepository() {
return new MongoEnvironmentRepository(mongoOperations); return new MongoEnvironmentRepository(mongoOperations);
} }
// @Bean
// public MongoEnvironmentRepository mongoEnvironmentRepository() {
// return new MongoEnvironmentRepository(mongoOperations);
// }
} }
...@@ -9,7 +9,7 @@ import lombok.Setter; ...@@ -9,7 +9,7 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class MongoPropertySource { public class MongoPropertySource {
private String profile = "default"; private String application;
private String label; private String profile;
private Map<String, Object> source = new LinkedHashMap<>(); private Map<String, Object> source = new LinkedHashMap<>();
} }
...@@ -9,16 +9,15 @@ import org.junit.jupiter.api.AfterEach; ...@@ -9,16 +9,15 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import de.ozgcloud.common.test.DataITCase; @DataMongoTest
@DataITCase
class MongoEnvironmentRepositoryTest { class MongoEnvironmentRepositoryTest {
private final String APPNAME = "testapp"; private final String APPNAME = "testapp";
private final String PROFILE = "testprofile"; private final String PROFILE = "testprofile";
private final String LABEL = "testlabel";
private final String PROPERTY1 = "property1"; private final String PROPERTY1 = "property1";
private final String VALUE1 = "value1"; private final String VALUE1 = "value1";
private final String DEFAULT = "default"; private final String DEFAULT = "default";
...@@ -26,7 +25,7 @@ class MongoEnvironmentRepositoryTest { ...@@ -26,7 +25,7 @@ class MongoEnvironmentRepositoryTest {
@Autowired @Autowired
private MongoOperations mongoOperations; private MongoOperations mongoOperations;
@Autowired @Autowired
private MongoEnvironmentRepository repository; private EnvironmentRepository repository;
private MongoPropertySource mongoPropertySource; private MongoPropertySource mongoPropertySource;
...@@ -41,70 +40,34 @@ class MongoEnvironmentRepositoryTest { ...@@ -41,70 +40,34 @@ class MongoEnvironmentRepositoryTest {
@AfterEach @AfterEach
public void clearData() { public void clearData() {
mongoOperations.dropCollection(APPNAME); mongoOperations.dropCollection(MongoEnvironmentRepository.COLLECTION_NAME);
} }
@Test @Test
void shouldFindCorrectEnvironment() { void shouldFindCorrectEnvironment() {
mongoPropertySource.setProfile(PROFILE); mongoPropertySource.setProfile(PROFILE);
mongoPropertySource.setLabel(LABEL); mongoPropertySource.setApplication(APPNAME);
mongoOperations.save(mongoPropertySource, APPNAME); mongoOperations.save(mongoPropertySource, MongoEnvironmentRepository.COLLECTION_NAME);
Environment environment = repository.findOne(APPNAME, PROFILE, LABEL); Environment environment = repository.findOne(APPNAME, PROFILE, null);
assertEquals(APPNAME, environment.getName()); assertEquals(APPNAME, environment.getName());
assertEquals(LABEL, environment.getLabel());
assertEquals(PROFILE, environment.getProfiles()[0]); assertEquals(PROFILE, environment.getProfiles()[0]);
assertEquals(1, environment.getPropertySources().size()); assertEquals(1, environment.getPropertySources().size());
assertEquals(PROFILE + "-" + LABEL, environment.getPropertySources().getLast().getName()); assertEquals(APPNAME + '-' + PROFILE, environment.getPropertySources().getLast().getName());
assertEquals(mongoPropertySource.getSource(), environment.getPropertySources().getLast().getSource()); assertEquals(mongoPropertySource.getSource(), environment.getPropertySources().getLast().getSource());
} }
@Test
void shouldAlwaysFindEntriesWithoutLabel() {
mongoPropertySource.setProfile(PROFILE);
mongoOperations.save(mongoPropertySource, APPNAME);
Environment environment = repository.findOne(APPNAME, PROFILE, LABEL);
assertEquals(1, environment.getPropertySources().size());
assertEquals(PROFILE + "-" + DEFAULT, environment.getPropertySources().getLast().getName());
}
@Test
void shouldInterpretNullLabelAsDefaultLabel() {
mongoPropertySource.setProfile(PROFILE);
mongoOperations.save(mongoPropertySource, APPNAME);
Environment environment = repository.findOne(APPNAME, PROFILE, null);
assertEquals(1, environment.getPropertySources().size());
assertEquals(DEFAULT, environment.getLabel());
assertEquals(PROFILE + "-" + DEFAULT, environment.getPropertySources().getLast().getName());
}
@Test @Test
void shouldTakemultipleProfilesSeperatedByComma() { void shouldTakemultipleProfilesSeperatedByComma() {
mongoPropertySource.setProfile(PROFILE); mongoPropertySource.setProfile(PROFILE);
mongoPropertySource.setLabel(LABEL); mongoPropertySource.setApplication(APPNAME);
mongoOperations.save(mongoPropertySource, APPNAME); mongoOperations.save(mongoPropertySource, MongoEnvironmentRepository.COLLECTION_NAME);
Environment environment = repository.findOne(APPNAME, PROFILE + "," + "SecondProfile", LABEL);
assertEquals(1, environment.getPropertySources().size());
assertEquals(PROFILE, environment.getProfiles()[0]);
assertEquals(PROFILE + "-" + LABEL, environment.getPropertySources().getLast().getName());
}
@Test
void shouldAlwaysFindEntrieswithNullProfile() {
mongoPropertySource.setLabel(LABEL);
mongoOperations.save(mongoPropertySource, APPNAME);
Environment environment = repository.findOne(APPNAME, PROFILE, LABEL); Environment environment = repository.findOne(APPNAME, PROFILE + "," + "SecondProfile", null);
assertEquals(1, environment.getPropertySources().size()); assertEquals(1, environment.getPropertySources().size());
assertEquals(PROFILE, environment.getProfiles()[0]); assertEquals(PROFILE, environment.getProfiles()[0]);
assertEquals(DEFAULT + "-" + LABEL, environment.getPropertySources().getLast().getName()); assertEquals(APPNAME + '-' + PROFILE, environment.getPropertySources().getLast().getName());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment