diff --git a/sandside/src/main/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsController.java b/sandside/src/main/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsController.java index 98f39d89..de288e95 100644 --- a/sandside/src/main/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsController.java +++ b/sandside/src/main/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsController.java @@ -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; @@ -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; @@ -111,7 +111,7 @@ public Mono> popularity(List news) { public Mono> feedAddAndSubscribe(@Valid @Argument("feed") FeedForm feedForm) { if (isNull(feedForm)) { return Mono.error(() -> GraphqlErrorException.newErrorException() - .errorClassification(ErrorClassification.errorClassification("BAD_REQUEST")) + .errorClassification(ErrorType.BAD_REQUEST) .build()); } diff --git a/sandside/src/test/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsControllerTest.java b/sandside/src/test/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsControllerTest.java index 5820048b..520d61ce 100644 --- a/sandside/src/test/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsControllerTest.java +++ b/sandside/src/test/java/fr/ght1pc9kc/baywatch/techwatch/infra/controllers/GraphQLFeedsControllerTest.java @@ -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; @@ -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 { @@ -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 @@ -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()) + ); + } } \ No newline at end of file diff --git a/sandside/src/test/resources/graphql-test/feedsServiceTest.graphql b/sandside/src/test/resources/graphql-test/feedsServiceTest.graphql index 31e3279a..021703ac 100644 --- a/sandside/src/test/resources/graphql-test/feedsServiceTest.graphql +++ b/sandside/src/test/resources/graphql-test/feedsServiceTest.graphql @@ -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} +}