add general boolean method to services for feature status. Change user.toString

This commit is contained in:
Joseph McCarthy 2016-01-26 21:18:47 +00:00
parent ba1d3a0fbf
commit 4a49f82f23
5 changed files with 37 additions and 5 deletions
feature-toggle/src
main/java/com/iluwatar/featuretoggle/pattern
test/java/com/iluwatar/featuretoggle/pattern

@ -7,4 +7,6 @@ public interface Service {
String getWelcomeMessage(User user); String getWelcomeMessage(User user);
boolean isEnhanced();
} }

@ -7,21 +7,36 @@ import java.util.Properties;
public class PropertiesFeatureToggleVersion implements Service { public class PropertiesFeatureToggleVersion implements Service {
private Properties properties; private boolean isEnhanced;
/**
*
* @param properties {@link Properties} used to configure the service and toggle features.
*/
public PropertiesFeatureToggleVersion(final Properties properties) { public PropertiesFeatureToggleVersion(final Properties properties) {
this.properties = properties; if (properties == null) {
throw new IllegalArgumentException("No Properties Provided.");
} else {
try {
isEnhanced = (boolean) properties.get("enhancedWelcome");
} catch (Exception e) {
throw new IllegalArgumentException("Invalid Enhancement Settings Provided.");
}
}
} }
@Override @Override
public String getWelcomeMessage(final User user) { public String getWelcomeMessage(final User user) {
final boolean enhancedWelcome = (boolean) properties.get("enhancedWelcome"); if (isEnhanced()) {
if (enhancedWelcome) {
return "Welcome " + user + ". You're using the enhanced welcome message."; return "Welcome " + user + ". You're using the enhanced welcome message.";
} }
return "Welcome to the application."; return "Welcome to the application.";
} }
@Override
public boolean isEnhanced() {
return isEnhanced;
}
} }

@ -15,4 +15,9 @@ public class TieredFeatureToggleVersion implements Service {
return "I suppose you can use this software."; return "I suppose you can use this software.";
} }
@Override
public boolean isEnhanced() {
return true;
}
} }

@ -8,6 +8,8 @@ import org.junit.Test;
import java.util.Properties; import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PropertiesFeatureToggleVersionTest { public class PropertiesFeatureToggleVersionTest {
@ -16,6 +18,7 @@ public class PropertiesFeatureToggleVersionTest {
final Properties properties = new Properties(); final Properties properties = new Properties();
properties.put("enhancedWelcome",true); properties.put("enhancedWelcome",true);
Service service = new PropertiesFeatureToggleVersion(properties); Service service = new PropertiesFeatureToggleVersion(properties);
assertTrue(service.isEnhanced());
final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
assertEquals("Welcome Jamie No Code. You're using the enhanced welcome message.",welcomeMessage); assertEquals("Welcome Jamie No Code. You're using the enhanced welcome message.",welcomeMessage);
} }
@ -25,6 +28,7 @@ public class PropertiesFeatureToggleVersionTest {
final Properties properties = new Properties(); final Properties properties = new Properties();
properties.put("enhancedWelcome",false); properties.put("enhancedWelcome",false);
Service service = new PropertiesFeatureToggleVersion(properties); Service service = new PropertiesFeatureToggleVersion(properties);
assertFalse(service.isEnhanced());
final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code")); final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
assertEquals("Welcome to the application.",welcomeMessage); assertEquals("Welcome to the application.",welcomeMessage);
} }

@ -7,6 +7,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TieredFeatureToggleVersionTest { public class TieredFeatureToggleVersionTest {
@ -33,4 +34,9 @@ public class TieredFeatureToggleVersionTest {
final String expected = "I suppose you can use this software."; final String expected = "I suppose you can use this software.";
assertEquals(expected, welcomeMessage); assertEquals(expected, welcomeMessage);
} }
@Test
public void testIsEnhancedAlwaysTrueAsTiered() throws Exception {
assertTrue(service.isEnhanced());
}
} }