Search in sources :

Example 1 with Chain

use of com.pushtorefresh.storio3.Interceptor.Chain in project storio by pushtorefresh.

the class ChainImpl method proceed.

// can be null on PreparedGetObject
@Nullable
@Override
public <Result, WrappedResult, Data> Result proceed(@NonNull PreparedOperation<Result, WrappedResult, Data> operation) {
    if (!interceptors.hasNext()) {
        throw new IllegalStateException("proceed was called on empty iterator");
    }
    calls++;
    // Confirm that this is the only call to chain.proceed().
    if (calls > 1) {
        throw new IllegalStateException("nextInterceptor " + interceptors.previous() + " must call proceed() exactly once");
    }
    // Call the nextChain nextInterceptor in the chain.
    final Interceptor nextInterceptor = interceptors.next();
    final ChainImpl nextChain = new ChainImpl(interceptors);
    return nextInterceptor.intercept(operation, nextChain);
}
Also used : Interceptor(com.pushtorefresh.storio3.Interceptor) Nullable(android.support.annotation.Nullable)

Example 2 with Chain

use of com.pushtorefresh.storio3.Interceptor.Chain in project storio by pushtorefresh.

the class ChainImplTest method buildChain_shouldThrowIfRealInterceptorNull.

@Test
public void buildChain_shouldThrowIfRealInterceptorNull() {
    try {
        final List<Interceptor> interceptors = Collections.singletonList(mock(Interceptor.class));
        // noinspection ConstantConditions
        final Chain chain = ChainImpl.buildChain(interceptors, null);
        chain.proceed(mock(PreparedOperation.class));
        failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Interceptor should not be null");
    }
}
Also used : Chain(com.pushtorefresh.storio3.Interceptor.Chain) PreparedOperation(com.pushtorefresh.storio3.operations.PreparedOperation) Interceptor(com.pushtorefresh.storio3.Interceptor) Test(org.junit.Test)

Example 3 with Chain

use of com.pushtorefresh.storio3.Interceptor.Chain in project storio by pushtorefresh.

the class LoggingInterceptorTest method interceptShouldLogToLogger.

@Test
public void interceptShouldLogToLogger() {
    final String result = "some result";
    final String data = "some data";
    final Interceptor.Chain chain = new TestChain(result);
    final PreparedOperation operation = new TestOperation(data);
    loggingInterceptor.intercept(operation, chain);
    // TODO how to test timings?
    assertThat(resultBuilder.toString()).startsWith("TestOperation\n=> data: some data\n<= result: some result\ntook ");
}
Also used : PreparedOperation(com.pushtorefresh.storio3.operations.PreparedOperation) Test(org.junit.Test)

Example 4 with Chain

use of com.pushtorefresh.storio3.Interceptor.Chain in project storio by pushtorefresh.

the class LoggingInterceptorTest method defaultLoggerShouldLogToAndroidLog.

@Config(shadows = { ShadowLog.class })
@Test
public void defaultLoggerShouldLogToAndroidLog() {
    loggingInterceptor = LoggingInterceptor.defaultLogger();
    final String result = "some result";
    final String data = "some data";
    final Interceptor.Chain chain = new TestChain(result);
    final PreparedOperation operation = new TestOperation(data);
    loggingInterceptor.intercept(operation, chain);
    // TODO how to test timings?
    assertThat(androidLogBuilder.toString()).startsWith("StorIO:TestOperation\n=> data: some data\n<= result: some result\ntook ");
}
Also used : PreparedOperation(com.pushtorefresh.storio3.operations.PreparedOperation) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 5 with Chain

use of com.pushtorefresh.storio3.Interceptor.Chain in project storio by pushtorefresh.

the class ChainImplTest method proceed_shouldThrowIfIteratorEmpty.

@Test
public void proceed_shouldThrowIfIteratorEmpty() {
    try {
        final List<Interceptor> empty = Collections.emptyList();
        final Chain chain = new ChainImpl(empty.listIterator());
        chain.proceed(mock(PreparedOperation.class));
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("proceed was called on empty iterator");
    }
}
Also used : Chain(com.pushtorefresh.storio3.Interceptor.Chain) PreparedOperation(com.pushtorefresh.storio3.operations.PreparedOperation) Interceptor(com.pushtorefresh.storio3.Interceptor) Test(org.junit.Test)

Aggregations

PreparedOperation (com.pushtorefresh.storio3.operations.PreparedOperation)7 Test (org.junit.Test)7 Interceptor (com.pushtorefresh.storio3.Interceptor)6 Chain (com.pushtorefresh.storio3.Interceptor.Chain)5 Nullable (android.support.annotation.Nullable)1 InOrder (org.mockito.InOrder)1 Config (org.robolectric.annotation.Config)1