Search in sources :

Example 26 with FlippingExecutionContext

use of org.ff4j.core.FlippingExecutionContext in project ff4j by ff4j.

the class ClientFilterStrategyTest method testInitialisationProgram.

@Test
public void testInitialisationProgram() {
    FlippingStrategy fs = new ClientFilterStrategy("Pierre, Paul, Jacques");
    fs.init("f1", null);
    fs.init("f1", new HashMap<String, String>());
    new WhiteListStrategy();
    new WhiteListStrategy("Pierre");
    // Working
    new BlackListStrategy();
    FlippingStrategy bl2 = new BlackListStrategy("Pierre");
    FlippingExecutionContext context = new FlippingExecutionContext();
    context.putString("clientHostName", "localhost");
    Assert.assertTrue(bl2.evaluate("f1", new InMemoryFeatureStore(), context));
    context.putString("clientHostName", "Pierre");
    Assert.assertFalse(bl2.evaluate("f1", new InMemoryFeatureStore(), context));
}
Also used : ClientFilterStrategy(org.ff4j.strategy.ClientFilterStrategy) BlackListStrategy(org.ff4j.strategy.BlackListStrategy) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FlippingStrategy(org.ff4j.core.FlippingStrategy) WhiteListStrategy(org.ff4j.strategy.WhiteListStrategy) FlippingExecutionContext(org.ff4j.core.FlippingExecutionContext) Test(org.junit.Test) AbstractFf4jTest(org.ff4j.test.AbstractFf4jTest)

Example 27 with FlippingExecutionContext

use of org.ff4j.core.FlippingExecutionContext in project ff4j by ff4j.

the class ServerFilterStrategyTest method testFilterOK.

@Test
public void testFilterOK() throws ParseException {
    // Given
    Feature f1 = ff4j.getFeature(F1);
    Assert.assertNotNull(f1.getFlippingStrategy());
    ServerFilterStrategy cStra = (ServerFilterStrategy) f1.getFlippingStrategy();
    Assert.assertNotNull(cStra.getInitParams());
    Assert.assertEquals(1, cStra.getInitParams().size());
    Assert.assertTrue(f1.isEnable());
    // When (add correct client name)
    FlippingExecutionContext fex = new FlippingExecutionContext();
    fex.addValue(ServerFilterStrategy.SERVER_HOSTNAME, "dev01");
    // Then
    Assert.assertTrue(ff4j.check(F1, fex));
}
Also used : ServerFilterStrategy(org.ff4j.strategy.ServerFilterStrategy) FlippingExecutionContext(org.ff4j.core.FlippingExecutionContext) Feature(org.ff4j.core.Feature) Test(org.junit.Test) AbstractFf4jTest(org.ff4j.test.AbstractFf4jTest)

Example 28 with FlippingExecutionContext

use of org.ff4j.core.FlippingExecutionContext in project ff4j by ff4j.

the class FF4jResource method checkPOST.

/**
 * Check if feature if flipped
 *
 * @param formParams
 *      target custom params
 * @return
 *      boolean if feature if flipped
 */
@POST
@Path("/" + OPERATION_CHECK + "/{uid}")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@ApiOperation(value = "<b>Advanced check</b> feature toggle (parameterized)", response = Boolean.class)
@ApiResponses({ @ApiResponse(code = 200, message = "if feature is flipped"), @ApiResponse(code = 400, message = "Invalid parameter") })
public Response checkPOST(@Context HttpHeaders headers, @PathParam("uid") String uid, MultivaluedMap<String, String> formParams) {
    // HoldSecurity Context
    FF4JSecurityContextHolder.save(securityContext);
    if (!ff4j.getFeatureStore().exist(uid)) {
        String errMsg = new FeatureNotFoundException(uid).getMessage();
        return Response.status(Response.Status.NOT_FOUND).entity(errMsg).build();
    }
    // Flipping Strategy may expected some dedicated parameters if not present, will return 400
    FlippingExecutionContext flipExecCtx = new FlippingExecutionContext();
    for (String key : formParams.keySet()) {
        flipExecCtx.putString(key, formParams.getFirst(key));
    }
    try {
        boolean flipped = ff4j.check(uid, flipExecCtx);
        return Response.ok(String.valueOf(flipped)).build();
    } catch (IllegalArgumentException iae) {
        String errMsg = "Invalid parameter " + iae.getMessage();
        return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build();
    }
}
Also used : FlippingExecutionContext(org.ff4j.core.FlippingExecutionContext) FeatureNotFoundException(org.ff4j.exception.FeatureNotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

FlippingExecutionContext (org.ff4j.core.FlippingExecutionContext)28 Test (org.junit.Test)21 AbstractFf4jTest (org.ff4j.test.AbstractFf4jTest)8 HashMap (java.util.HashMap)6 Feature (org.ff4j.core.Feature)6 PropertyString (org.ff4j.property.PropertyString)6 Map (java.util.Map)4 FlippingStrategy (org.ff4j.core.FlippingStrategy)4 ClientFilterStrategy (org.ff4j.strategy.ClientFilterStrategy)4 InMemoryFeatureStore (org.ff4j.store.InMemoryFeatureStore)3 ServerFilterStrategy (org.ff4j.strategy.ServerFilterStrategy)3 Date (java.util.Date)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Calendar (java.util.Calendar)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 FF4j (org.ff4j.FF4j)1