Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check that a result is an ok playframework

I am trying to make a slightly better @Cached annotation by making it aware of the parameters of the function I call in my controllers.

so I have this Action :

public class ContextualCachedAction extends Action<ContextualCached> {

    @Override
    public Result call(Context ctx) throws Throwable {
        try {
            String key = makeKey(ctx);
            Integer duration = configuration.duration();
            Result result = (Result) Cache.get(key);
            if (result == null) {
                result = delegate.call(ctx);

                //TODO find a way to cache only successful calls

                Cache.set(key, result, duration);
            }
            return result;
        } catch (RuntimeException e) {
            throw e;
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }

    private String makeKey(Context ctx) {
        //makes the key from some parameters in the ctx.request()
    }
}

My question is this : I would like to cache the result of delegate.call() only if it is an Ok(). How can I check for that? Is there a property? a util? or do I need to Ok().getClass().isInstance(result)?

Thanks for any answers and hints.

PS : Why do I want to do that? Because I have some calls that generate few types of different results. Few enough results that caching them could be an option since I don't want to

like image 473
le-doude Avatar asked Feb 27 '13 16:02

le-doude


1 Answers

Less sucky approach:

import org.junit.*;
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.*;

/* do stuff */

Result result = doSomethingWithController();
assertThat(status(result)).isEqualTo(OK);

Works as of 2.2.2.

like image 185
user2029783 Avatar answered Oct 05 '22 16:10

user2029783