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

OZG-4814 Makes administration to a spring cloud config server on an mongodb

parent 8831cb30
Branches
Tags
No related merge requests found
...@@ -31,6 +31,11 @@ ...@@ -31,6 +31,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>4.1.0</version>
</dependency>
<!-- Dev --> <!-- Dev -->
<dependency> <dependency>
......
...@@ -3,7 +3,10 @@ package de.ozgcloud.admin; ...@@ -3,7 +3,10 @@ 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 de.ozgcloud.admin.environment.EnableMongoConfigServer;
@SpringBootApplication @SpringBootApplication
@EnableMongoConfigServer
public class AdministrationApplication { public class AdministrationApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package de.ozgcloud.admin.environment;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Import(MongoEnvironmentRepositoryConfiguration.class)
@EnableConfigServer
public @interface EnableMongoConfigServer {
}
package de.ozgcloud.admin.environment;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
public class MongoEnvironmentRepository implements EnvironmentRepository {
private static final String LABEL = "label";
private static final String PROFILE = "profile";
private static final String DEFAULT = "default";
private MongoTemplate mongoTemplate;
public MongoEnvironmentRepository(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@Override
public Environment findOne(String application, String profile, String label) {
Query query = buildQuery(profile, label);
List<MongoPropertySource> sources = mongoTemplate.find(query, MongoPropertySource.class, application);
Environment environment = new Environment(application, new String[] { profile }, label == null ? DEFAULT : label, null, null);
System.out.println(label);
environment.addAll(sources.stream().map(this::transformMonPropertySourceToPropertySource).collect(Collectors.toList()));
return environment;
}
private Query buildQuery(String profile, String label) {
Query query = new Query();
query.addCriteria(Criteria.where(LABEL).in(new String[] { label, null }));
query.addCriteria(Criteria.where(PROFILE).in(new String[] { profile, null }));
return query;
}
private PropertySource transformMonPropertySourceToPropertySource(MongoPropertySource mongoPropertySource) {
String sourceName = mongoPropertySource.getProfile() + "-"
+ ((mongoPropertySource.getLabel() == null) ? DEFAULT : mongoPropertySource.getLabel());
return new PropertySource(sourceName, mongoPropertySource.getSource());
}
}
package de.ozgcloud.admin.environment;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import lombok.AllArgsConstructor;
@Configuration
@AllArgsConstructor
public class MongoEnvironmentRepositoryConfiguration {
private MongoTemplate mongoTemplate;
@Bean
public EnvironmentRepository environmentRepository() {
return new MongoEnvironmentRepository(mongoTemplate);
}
}
package de.ozgcloud.admin.environment;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MongoPropertySource {
private String profile;
private String label;
private Map<String, Object> source = new LinkedHashMap<>();
}
spring:
data:
mongodb:
uri: mongodb://localhost/config-db
server:
port: 8888
\ No newline at end of file
package de.ozgcloud.admin.environment;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import de.ozgcloud.admin.AdministrationApplication;
@SpringBootTest(classes = AdministrationApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
class MongoConfigServerTest {
private final String APPNAME = "testapp";
private final String PROFILE = "testprofile";
private final String LABEL = "testlabel";
@LocalServerPort
private int port;
@Test
void shouldHaveHttpEndpoint() {
ResponseEntity<Environment> response = new TestRestTemplate().getForEntity("http://localhost:"
+ port + "/" + APPNAME + "/" + PROFILE + "/" + LABEL, Environment.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}
package de.ozgcloud.admin.environment;
import static org.junit.Assert.*;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.server.environment.EnvironmentRepository;
import org.springframework.data.mongodb.core.MongoTemplate;
@DataMongoTest
class MongoEnvironmentRepositoryTest {
private final String APPNAME = "testapp";
private final String PROFILE = "testprofile";
private final String LABEL = "testlabel";
private final String PROPERTY1 = "property1";
private final String VALUE1 = "value1";
private final String DEFAULT = "default";
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private EnvironmentRepository repository;
private MongoPropertySource mongoPropertySource;
@BeforeEach
public void prepareExampleData() {
mongoPropertySource = new MongoPropertySource();
Map<String, Object> properties = new LinkedHashMap<>();
properties.put(PROPERTY1, VALUE1);
mongoPropertySource.setSource(properties);
}
@AfterEach
public void clearData() {
mongoTemplate.dropCollection(APPNAME);
}
@Test
void shouldFindCorrectEnvironment() {
mongoPropertySource.setProfile(PROFILE);
mongoPropertySource.setLabel(LABEL);
mongoTemplate.save(mongoPropertySource, APPNAME);
Environment environment = repository.findOne(APPNAME, PROFILE, LABEL);
assertEquals(APPNAME, environment.getName());
assertEquals(LABEL, environment.getLabel());
assertEquals(PROFILE, environment.getProfiles()[0]);
assertEquals(1, environment.getPropertySources().size());
assertEquals(PROFILE + "-" + LABEL, environment.getPropertySources().getLast().getName());
assertEquals(mongoPropertySource.getSource(), environment.getPropertySources().getLast().getSource());
}
@Test
void shouldAlwaysFindEntriesWithoutLabel() {
mongoPropertySource.setProfile(PROFILE);
mongoTemplate.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);
mongoTemplate.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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment