📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -29,8 +29,7 @@ import com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersi
|
||||
import com.iluwatar.featuretoggle.user.User;
|
||||
import com.iluwatar.featuretoggle.user.UserGroup;
|
||||
import java.util.Properties;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The Feature Toggle pattern allows for complete code executions to be turned on or off with ease.
|
||||
@ -45,10 +44,9 @@ import org.slf4j.LoggerFactory;
|
||||
* <p>Note that this pattern can easily introduce code complexity, and if not kept in check can
|
||||
* result in redundant unmaintained code within the codebase.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Block 1 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties}
|
||||
* setting the feature toggle to enabled.
|
||||
|
@ -26,6 +26,7 @@ package com.iluwatar.featuretoggle.pattern.propertiesversion;
|
||||
import com.iluwatar.featuretoggle.pattern.Service;
|
||||
import com.iluwatar.featuretoggle.user.User;
|
||||
import java.util.Properties;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* This example of the Feature Toogle pattern is less dynamic version than {@link
|
||||
@ -40,9 +41,15 @@ import java.util.Properties;
|
||||
* @see com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion
|
||||
* @see User
|
||||
*/
|
||||
@Getter
|
||||
public class PropertiesFeatureToggleVersion implements Service {
|
||||
|
||||
private final boolean isEnhanced;
|
||||
/**
|
||||
* True if the welcome message to be returned is the enhanced venison or not. For
|
||||
* this service it will see the value of the boolean that was set in the constructor {@link
|
||||
* PropertiesFeatureToggleVersion#PropertiesFeatureToggleVersion(Properties)}
|
||||
*/
|
||||
private final boolean enhanced;
|
||||
|
||||
/**
|
||||
* Creates an instance of {@link PropertiesFeatureToggleVersion} using the passed {@link
|
||||
@ -59,7 +66,7 @@ public class PropertiesFeatureToggleVersion implements Service {
|
||||
throw new IllegalArgumentException("No Properties Provided.");
|
||||
} else {
|
||||
try {
|
||||
isEnhanced = (boolean) properties.get("enhancedWelcome");
|
||||
enhanced = (boolean) properties.get("enhancedWelcome");
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Invalid Enhancement Settings Provided.");
|
||||
}
|
||||
@ -87,16 +94,4 @@ public class PropertiesFeatureToggleVersion implements Service {
|
||||
|
||||
return "Welcome to the application.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that checks if the welcome message to be returned is the enhanced venison or not. For
|
||||
* this service it will see the value of the boolean that was set in the constructor {@link
|
||||
* PropertiesFeatureToggleVersion#PropertiesFeatureToggleVersion(Properties)}
|
||||
*
|
||||
* @return Boolean value {@code true} if enhanced.
|
||||
*/
|
||||
@Override
|
||||
public boolean isEnhanced() {
|
||||
return isEnhanced;
|
||||
}
|
||||
}
|
||||
|
@ -23,23 +23,17 @@
|
||||
|
||||
package com.iluwatar.featuretoggle.user;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Used to demonstrate the purpose of the feature toggle. This class actually has nothing to do with
|
||||
* the pattern.
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Default Constructor setting the username.
|
||||
*
|
||||
* @param name {@link String} to represent the name of the user.
|
||||
*/
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -38,7 +38,6 @@ public class UserGroup {
|
||||
private static final List<User> freeGroup = new ArrayList<>();
|
||||
private static final List<User> paidGroup = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* Add the passed {@link User} to the free user group list.
|
||||
*
|
||||
|
@ -35,17 +35,17 @@ import org.junit.jupiter.api.Test;
|
||||
/**
|
||||
* Test Properties Toggle
|
||||
*/
|
||||
public class PropertiesFeatureToggleVersionTest {
|
||||
class PropertiesFeatureToggleVersionTest {
|
||||
|
||||
@Test
|
||||
public void testNullPropertiesPassed() {
|
||||
void testNullPropertiesPassed() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new PropertiesFeatureToggleVersion(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonBooleanProperty() {
|
||||
void testNonBooleanProperty() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
final var properties = new Properties();
|
||||
properties.setProperty("enhancedWelcome", "Something");
|
||||
@ -54,7 +54,7 @@ public class PropertiesFeatureToggleVersionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFeatureTurnedOn() {
|
||||
void testFeatureTurnedOn() {
|
||||
final var properties = new Properties();
|
||||
properties.put("enhancedWelcome", true);
|
||||
var service = new PropertiesFeatureToggleVersion(properties);
|
||||
@ -64,7 +64,7 @@ public class PropertiesFeatureToggleVersionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFeatureTurnedOff() {
|
||||
void testFeatureTurnedOff() {
|
||||
final var properties = new Properties();
|
||||
properties.put("enhancedWelcome", false);
|
||||
var service = new PropertiesFeatureToggleVersion(properties);
|
||||
|
@ -35,34 +35,34 @@ import org.junit.jupiter.api.Test;
|
||||
/**
|
||||
* Test Tiered Feature Toggle
|
||||
*/
|
||||
public class TieredFeatureToggleVersionTest {
|
||||
class TieredFeatureToggleVersionTest {
|
||||
|
||||
final User paidUser = new User("Jamie Coder");
|
||||
final User freeUser = new User("Alan Defect");
|
||||
final Service service = new TieredFeatureToggleVersion();
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
void setUp() {
|
||||
UserGroup.addUserToPaidGroup(paidUser);
|
||||
UserGroup.addUserToFreeGroup(freeUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWelcomeMessageForPaidUser() {
|
||||
void testGetWelcomeMessageForPaidUser() {
|
||||
final var welcomeMessage = service.getWelcomeMessage(paidUser);
|
||||
final var expected = "You're amazing Jamie Coder. Thanks for paying for this awesome software.";
|
||||
assertEquals(expected, welcomeMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWelcomeMessageForFreeUser() {
|
||||
void testGetWelcomeMessageForFreeUser() {
|
||||
final var welcomeMessage = service.getWelcomeMessage(freeUser);
|
||||
final var expected = "I suppose you can use this software.";
|
||||
assertEquals(expected, welcomeMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEnhancedAlwaysTrueAsTiered() {
|
||||
void testIsEnhancedAlwaysTrueAsTiered() {
|
||||
assertTrue(service.isEnhanced());
|
||||
}
|
||||
}
|
||||
|
@ -32,24 +32,24 @@ import org.junit.jupiter.api.Test;
|
||||
/**
|
||||
* Test User Group specific feature
|
||||
*/
|
||||
public class UserGroupTest {
|
||||
class UserGroupTest {
|
||||
|
||||
@Test
|
||||
public void testAddUserToFreeGroup() {
|
||||
void testAddUserToFreeGroup() {
|
||||
var user = new User("Free User");
|
||||
UserGroup.addUserToFreeGroup(user);
|
||||
assertFalse(UserGroup.isPaid(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddUserToPaidGroup() {
|
||||
void testAddUserToPaidGroup() {
|
||||
var user = new User("Paid User");
|
||||
UserGroup.addUserToPaidGroup(user);
|
||||
assertTrue(UserGroup.isPaid(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddUserToPaidWhenOnFree() {
|
||||
void testAddUserToPaidWhenOnFree() {
|
||||
var user = new User("Paid User");
|
||||
UserGroup.addUserToFreeGroup(user);
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
@ -58,7 +58,7 @@ public class UserGroupTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddUserToFreeWhenOnPaid() {
|
||||
void testAddUserToFreeWhenOnPaid() {
|
||||
var user = new User("Free User");
|
||||
UserGroup.addUserToPaidGroup(user);
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
|
Reference in New Issue
Block a user