|
| 1 | +package com.oembedler.moon.graphql.boot; |
| 2 | + |
| 3 | +import graphql.ExecutionResult; |
| 4 | +import graphql.servlet.context.ContextSetting; |
| 5 | +import graphql.servlet.core.GraphQLQueryInvoker; |
| 6 | +import graphql.servlet.input.GraphQLSingleInvocationInput; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.mockito.Mock; |
| 11 | +import org.mockito.junit.MockitoJUnitRunner; |
| 12 | + |
| 13 | +import javax.transaction.Transactional; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.mockito.BDDMockito.given; |
| 19 | +import static org.mockito.BDDMockito.then; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | + |
| 22 | +@RunWith(MockitoJUnitRunner.class) |
| 23 | +public class TransactionQueryInvokerWrapperTest { |
| 24 | + |
| 25 | + @Mock |
| 26 | + private GraphQLQueryInvoker wrappedInvoker; |
| 27 | + |
| 28 | + private TransactionalGraphQLQueryInvokerWrapper wrapper; |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setUp() { |
| 32 | + wrapper = new TransactionalGraphQLQueryInvokerWrapper(wrappedInvoker); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void shouldHaveTransactionalAnnotation() { |
| 37 | + assertThat(TransactionalGraphQLQueryInvokerWrapper.class.getAnnotation(Transactional.class)).isNotNull(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void shouldWrapExistingQueryInvokerWithSingleQuery() { |
| 42 | + //GIVEN |
| 43 | + final GraphQLSingleInvocationInput invocationInput = mock(GraphQLSingleInvocationInput.class); |
| 44 | + final ExecutionResult expectedExecutionResult = mock(ExecutionResult.class); |
| 45 | + given(wrappedInvoker.query(invocationInput)).willReturn(expectedExecutionResult); |
| 46 | + //WHEN |
| 47 | + final ExecutionResult actualExecutionResult = wrapper.query(invocationInput); |
| 48 | + //THEN |
| 49 | + then(wrappedInvoker).should().query(invocationInput); |
| 50 | + then(wrappedInvoker).shouldHaveNoMoreInteractions(); |
| 51 | + assertThat(actualExecutionResult) |
| 52 | + .as("Should call the wrapped invoker, and return the execution result returned by it.") |
| 53 | + .isEqualTo(expectedExecutionResult); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldWrapExistingQueryInvokerWithBatchedQuery() { |
| 58 | + //GIVEN |
| 59 | + final GraphQLSingleInvocationInput invocationInput = mock(GraphQLSingleInvocationInput.class); |
| 60 | + final List<GraphQLSingleInvocationInput> invocationInputList = Collections.singletonList(invocationInput); |
| 61 | + final ContextSetting contextSetting = ContextSetting.PER_QUERY_WITH_INSTRUMENTATION; |
| 62 | + final ExecutionResult expectedExecutionResult = mock(ExecutionResult.class); |
| 63 | + final List<ExecutionResult> expectedExecutionResultList = Collections.singletonList(expectedExecutionResult); |
| 64 | + given(wrappedInvoker.query(invocationInputList, contextSetting)).willReturn(expectedExecutionResultList); |
| 65 | + //WHEN |
| 66 | + final List<ExecutionResult> actualExecutionResultList = wrapper.query(invocationInputList, contextSetting); |
| 67 | + //THEN |
| 68 | + then(wrappedInvoker).should().query(invocationInputList, contextSetting); |
| 69 | + then(wrappedInvoker).shouldHaveNoMoreInteractions(); |
| 70 | + assertThat(actualExecutionResultList) |
| 71 | + .as("Should call the wrapped invoker, and return the execution result list returned by it.") |
| 72 | + .isEqualTo(expectedExecutionResultList); |
| 73 | + } |
| 74 | +} |
0 commit comments