Skip to content

Instantly share code, notes, and snippets.

@ptaylor
Created June 26, 2018 15:54
Show Gist options
  • Save ptaylor/f848a2b80608b6e59d0bfd78a410c7a3 to your computer and use it in GitHub Desktop.
Save ptaylor/f848a2b80608b6e59d0bfd78a410c7a3 to your computer and use it in GitHub Desktop.
Integration test of Spring Boot Configuration service to validate no errors in configuration served for services and profiles
package org.example.config;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
/**
*
* Test to make sure configuration can be served up for all services and profiles. This catches any syntax errors
* in the confuguration YAML.
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ConfigurationServiceTest {
@Autowired
private TestRestTemplate testRestTemplate;
String[] SERVICES = {
"my-service",
"my-other-service",
"some-thrid-service",
};
String[][] PROFILES = {
{"default"},
{"dev"},
{"dev", "sandbox"},
{"dev", "live"},
{"stage"},
{"stage", "sandbox"},
{"stage", "live"},
{"production"},
{"production", "sandbox"},
{"production", "live"},
};
@Test
public void testServiceConfigurations() {
for (String serviceName: SERVICES) {
for (String[] profileNames: PROFILES) {
testServiceWithProfiles(serviceName, profileNames);
}
}
}
private void testServiceWithProfiles(String serviceName, String[] profileNames) {
String joinedProfiles = String.join(",", profileNames);
String path = "/" + serviceName + "/" + joinedProfiles;
System.out.println("Testing: " + path);
Map response = testRestTemplate.getForObject(path, Map.class);
validateResponse(response, serviceName, joinedProfiles);
}
public void validateResponse(Map response, String serviceName, String profileNames) {
assertNotNull("Failed to get configuration for service:" + serviceName + ", profiles: " + profileNames, response);
String error = (String) response.get("error");
if (error != null) {
System.out.println(" status: " + response.get("status"));
System.out.println(" error: " + response.get("error"));
System.out.println(" message: " + response.get("message"));
fail("Error getting configuration for service: " + serviceName + ", profiles: " + profileNames);
}
System.out.println(" name: " + response.get("name"));
assertEquals(serviceName, response.get("name"));
List profiles = (List) response.get("profiles");
assertEquals(1, profiles.size());
assertEquals(profiles.get(0), profileNames);
List propertySources = (List) response.get("propertySources");
assertTrue(propertySources.size() >= 1);
propertySources.forEach(p -> validatePropertySource((Map) p));
}
private void validatePropertySource(Map propertySource) {
System.out.println(" propertySource: " + propertySource.get("name"));
Map source = (Map) propertySource.get("source");
System.out.println(" config_source: " + source.get("config_source"));
//assertNotNull(source.get("config_source"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment