-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Unit tests to Conditional on Profile
- Ignores order of profile - validates value attribute - Works only with more than one profile
- Loading branch information
Showing
7 changed files
with
207 additions
and
106 deletions.
There are no files selected for viewing
22 changes: 0 additions & 22 deletions
22
src/main/java/io/github/shyamz/conditional/ActiveOnProfilesCondition.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/io/github/shyamz/conditional/ConditionalOnAllProfilesCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.github.shyamz.conditional; | ||
|
||
|
||
import org.springframework.context.annotation.Condition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
public class ConditionalOnAllProfilesCondition implements Condition { | ||
|
||
private List<String> currentProfiles = new ArrayList<>(); | ||
private List<String> activeProfiles = new ArrayList<>(); | ||
|
||
@Override | ||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
Map<String, Object> attributes = metadata | ||
.getAnnotationAttributes(ConditionalOnAllProfiles.class.getName()); | ||
|
||
populateCurrentProfiles((String[]) attributes.get("value")); | ||
populateActiveProfiles(context.getEnvironment().getActiveProfiles()); | ||
|
||
return currentProfiles.stream().allMatch(activeProfiles::contains); | ||
} | ||
|
||
private void populateCurrentProfiles(String[] attributeValues) { | ||
currentProfiles.clear(); | ||
currentProfiles.addAll(Arrays.asList(attributeValues)); | ||
validateCurrentProfiles(); | ||
} | ||
|
||
private void validateCurrentProfiles() { | ||
if (currentProfiles.isEmpty() || currentProfiles.size() == 1) { | ||
throw new RuntimeException("'value' attributes need more than one 'profile' values. For just one profile use '@Profile' annotation"); | ||
} | ||
} | ||
|
||
private void populateActiveProfiles(String[] activeProfileValues) { | ||
activeProfiles.clear(); | ||
activeProfiles.addAll(Arrays.asList(activeProfileValues)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
src/test/java/io/github/shyamz/conditional/ConditionalApplicationTests.java
This file was deleted.
Oops, something went wrong.
137 changes: 137 additions & 0 deletions
137
src/test/java/io/github/shyamz/conditional/ConditionalOnAllProfilesConditionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package io.github.shyamz.conditional; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
|
||
public class ConditionalOnAllProfilesConditionTest { | ||
|
||
private ConditionalOnAllProfilesCondition subject; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
subject = new ConditionalOnAllProfilesCondition(); | ||
} | ||
|
||
@Test | ||
public void throwsExceptionForEmptyProfileList() throws Exception { | ||
assertThatThrownBy( | ||
() -> subject.matches(mockConditionContext(), mockAnnotatedTypeMetaData())) | ||
.isExactlyInstanceOf(RuntimeException.class) | ||
.hasMessage("'value' attributes need more than one 'profile' values. For just one profile use '@Profile' annotation"); | ||
} | ||
|
||
@Test | ||
public void throwsExceptionForOneProfile() throws Exception { | ||
assertThatThrownBy( | ||
() -> subject.matches(mockConditionContext(), mockAnnotatedTypeMetaData("test"))) | ||
.isExactlyInstanceOf(RuntimeException.class) | ||
.hasMessage("'value' attributes need more than one 'profile' values. For just one profile use '@Profile' annotation"); | ||
} | ||
|
||
@Test | ||
public void returnsTrueIfCurrentProfilesAreEqual() throws Exception { | ||
assertThat(subject.matches(mockConditionContext("test", "local"), | ||
mockAnnotatedTypeMetaData("test", "local"))).isTrue(); | ||
} | ||
|
||
@Test | ||
public void returnsTrueIfCurrentProfilesAreEqualButInDifferentOrder() throws Exception { | ||
assertThat(subject.matches(mockConditionContext("test", "local"), | ||
mockAnnotatedTypeMetaData("local", "test"))).isTrue(); | ||
} | ||
|
||
@Test | ||
public void returnsTrueIfCurrentProfilesAreSubsetOfActiveProfiles() throws Exception { | ||
assertThat(subject.matches(mockConditionContext("test", "local", "pact"), | ||
mockAnnotatedTypeMetaData("local", "test"))).isTrue(); | ||
} | ||
|
||
@Test | ||
public void returnsFalseIfCurrentProfilesAreNotSubsetOfActiveProfiles() throws Exception { | ||
assertThat(subject.matches(mockConditionContext("test", "local"), | ||
mockAnnotatedTypeMetaData("local", "test", "pact"))).isFalse(); | ||
} | ||
|
||
@Test | ||
public void returnsFalseIfCurrentProfilesAreDifferent() throws Exception { | ||
assertThat(subject.matches(mockConditionContext("test", "local"), mockAnnotatedTypeMetaData("cloud", "acceptance"))).isFalse(); | ||
} | ||
|
||
private ConditionContext mockConditionContext(String... activeProfiles) { | ||
return new ConditionContext() { | ||
|
||
@Override | ||
public BeanDefinitionRegistry getRegistry() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ConfigurableListableBeanFactory getBeanFactory() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Environment getEnvironment() { | ||
MockEnvironment environment = new MockEnvironment(); | ||
environment.withProperty("spring.profiles.active", String.join(",", activeProfiles)); | ||
return environment; | ||
} | ||
|
||
@Override | ||
public ResourceLoader getResourceLoader() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ClassLoader getClassLoader() { | ||
return null; | ||
} | ||
}; | ||
|
||
} | ||
|
||
private AnnotatedTypeMetadata mockAnnotatedTypeMetaData(String... currentProfiles) { | ||
return new AnnotatedTypeMetadata() { | ||
@Override | ||
public boolean isAnnotated(String annotationName) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAnnotationAttributes(String annotationName) { | ||
return Collections.singletonMap("value", currentProfiles); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters