Skip to content

Commit

Permalink
test(sandside): #224 add integration test for GQL feedAddAndSubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
Marthym committed Oct 13, 2024
1 parent 57cff88 commit 4e46dca
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import fr.ght1pc9kc.juery.api.Criteria;
import fr.ght1pc9kc.juery.api.PageRequest;
import fr.ght1pc9kc.juery.basic.QueryStringParser;
import graphql.ErrorClassification;
import graphql.GraphqlErrorException;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,6 +24,7 @@
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -111,7 +111,7 @@ public Mono<Map<News, Popularity>> popularity(List<News> news) {
public Mono<Entity<WebFeed>> feedAddAndSubscribe(@Valid @Argument("feed") FeedForm feedForm) {
if (isNull(feedForm)) {
return Mono.error(() -> GraphqlErrorException.newErrorException()
.errorClassification(ErrorClassification.errorClassification("BAD_REQUEST"))
.errorClassification(ErrorType.BAD_REQUEST)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.ght1pc9kc.baywatch.common.infra.adapters.GraphqlExceptionAdapter;
import fr.ght1pc9kc.baywatch.common.infra.config.GraphqlConfiguration;
import fr.ght1pc9kc.baywatch.common.infra.config.jackson.JacksonMappingConfiguration;
import fr.ght1pc9kc.baywatch.techwatch.api.FeedService;
Expand All @@ -18,19 +19,27 @@
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.graphql.test.tester.GraphQlTester;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

@Tag("integration")
@ActiveProfiles("test")
@Import({JacksonMappingConfiguration.class, MockSecurityConfiguration.class, GraphqlConfiguration.class, GraphqlTechwatchConfig.class})
@Import({JacksonMappingConfiguration.class, MockSecurityConfiguration.class, GraphqlConfiguration.class, GraphqlTechwatchConfig.class,
GraphqlExceptionAdapter.class})
@GraphQlTest(GraphQLFeedsController.class)
class GraphQLFeedsControllerTest {

Expand All @@ -53,6 +62,7 @@ void setUp() {
when(mockFeedService.list(any(PageRequest.class)))
.thenReturn(Flux.just(FeedSamples.JEDI));
when(mockFeedService.count(any(PageRequest.class))).thenReturn(Mono.just(1));
doReturn(Flux.just(FeedSamples.JEDI)).when(mockFeedService).addAndSubscribe(anyCollection());
}

@Test
Expand Down Expand Up @@ -89,4 +99,34 @@ void should_call_feeds_search() throws JsonProcessingException {
.isArray().first().isObject()
.containsKeys("_id", "name", "location", "tags");
}

@Test
@WithMockUser(username = "admin", roles = {"USER", "ADMIN"})
void should_call_feedAddAndSubscribe() throws JsonProcessingException {
gqlClient.documentName("feedsServiceTest")
.operationName("FeedAddAndSubscribe")
.variable("feed", null)
.execute().errors().expect(ex -> ex.getErrorType() == ErrorType.INTERNAL_ERROR);

GraphQlTester.Response executed = gqlClient.documentName("feedsServiceTest")
.operationName("FeedAddAndSubscribe")
.variable("feed", Map.of(
"name", "Jedi.com",
"location", "https://jedi.com/atom.xml",
"description", "Jedi News Feed",
"tags", List.of("light", "good")))
.execute();

Object response = executed.path("feedAddAndSubscribe")
.entity(Object.class)
.get();
String actual = objectMapper.writeValueAsString(response);
Assertions.assertThat(actual).isNotBlank();
assertThatJson(actual)
.isObject()
.containsOnly(
Map.entry("_id", FeedSamples.JEDI.id()),
Map.entry("name", FeedSamples.JEDI.self().name())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ query ScrapFeedHeader($link: URI!) {
mutation FeedSubscription($feedId: ID) {
subscribe(id: $feedId) {_id name}
}

mutation FeedAddAndSubscribe($feed: FeedForm) {
feedAddAndSubscribe(feed: $feed) {_id name}
}

0 comments on commit 4e46dca

Please sign in to comment.