|
| 1 | +package com.graphql.spring.boot.test; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import lombok.AllArgsConstructor; |
| 5 | +import lombok.Data; |
| 6 | +import lombok.NoArgsConstructor; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | + |
| 15 | +import java.math.BigDecimal; |
| 16 | +import java.time.LocalDate; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +@SpringBootTest(classes = TestApplication.class) |
| 24 | +public class GraphQLResponseTest { |
| 25 | + |
| 26 | + private static final String DATA_PATH = "$.data.test"; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private ObjectMapper objectMapper; |
| 30 | + |
| 31 | + private static Stream<Arguments> testGetStringArguments() { |
| 32 | + return Stream.of( |
| 33 | + Arguments.of("{\"data\": {\"test\": 2}}", "2"), |
| 34 | + Arguments.of("{\"data\": {\"test\": \"2\"}}", "2"), |
| 35 | + Arguments.of("{\"data\": {\"test\": \"2020-02-23\"}}", "2020-02-23") |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + private static Stream<Arguments> testGetArguments() { |
| 40 | + return Stream.of( |
| 41 | + Arguments.of("{\"data\": {\"test\": \"2\"}}", Integer.class, 2), |
| 42 | + Arguments.of("{\"data\": {\"test\": \"2\"}}", String.class, "2"), |
| 43 | + Arguments.of("{\"data\": {\"test\": \"2\"}}", BigDecimal.class, new BigDecimal("2")), |
| 44 | + Arguments.of("{\"data\": {\"test\": \"2020-02-23\"}}", LocalDate.class, LocalDate.parse("2020-02-23")), |
| 45 | + Arguments.of("{\"data\": {\"test\": {\"foo\": \"fizzBuzz\", \"bar\": 13.8 }}}", FooBar.class, |
| 46 | + new FooBar("fizzBuzz", new BigDecimal("13.8"))) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + private static Stream<Arguments> testGetListArguments() { |
| 51 | + return Stream.of( |
| 52 | + Arguments.of("{\"data\": {\"test\": [\"2\", \"1\"]}}", Integer.class, Arrays.asList(2, 1)), |
| 53 | + Arguments.of("{\"data\": {\"test\": [\"2\", \"1\"]}}", String.class, Arrays.asList("2", "1")), |
| 54 | + Arguments.of("{\"data\": {\"test\": [\"2\", \"1\"]}}", BigDecimal.class, |
| 55 | + Arrays.asList(new BigDecimal("2"), new BigDecimal("1"))), |
| 56 | + Arguments.of("{\"data\": {\"test\": [\"2020-02-23\", \"2020-02-24\"]}}", LocalDate.class, |
| 57 | + Arrays.asList(LocalDate.parse("2020-02-23"), LocalDate.parse("2020-02-24"))), |
| 58 | + Arguments.of("{\"data\":{\"test\":[{\"foo\":\"fizz\",\"bar\":1.23},{\"foo\":\"buzz\",\"bar\":32.12}]}}", |
| 59 | + FooBar.class, |
| 60 | + Arrays.asList( |
| 61 | + new FooBar("fizz", new BigDecimal("1.23")), |
| 62 | + new FooBar("buzz", new BigDecimal("32.12")) |
| 63 | + ) |
| 64 | + ) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @DisplayName("Should get the JSON node's value as a String.") |
| 69 | + @ParameterizedTest |
| 70 | + @MethodSource("testGetStringArguments") |
| 71 | + public void testGetString( |
| 72 | + final String bodyString, |
| 73 | + final String expected |
| 74 | + ) { |
| 75 | + //GIVEN |
| 76 | + final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper); |
| 77 | + //WHEN |
| 78 | + final String actual = graphQLResponse.get(DATA_PATH); |
| 79 | + //THEN |
| 80 | + assertThat(actual).isEqualTo(expected); |
| 81 | + } |
| 82 | + |
| 83 | + @DisplayName("Should get the JSON node's value as an instance of a specified class.") |
| 84 | + @ParameterizedTest |
| 85 | + @MethodSource("testGetArguments") |
| 86 | + public <T> void testGet( |
| 87 | + final String bodyString, |
| 88 | + final Class<T> clazz, |
| 89 | + final T expected |
| 90 | + ) { |
| 91 | + //GIVEN |
| 92 | + final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper); |
| 93 | + //WHEN |
| 94 | + final T actual = graphQLResponse.get(DATA_PATH, clazz); |
| 95 | + //THEN |
| 96 | + assertThat(actual).isInstanceOf(clazz).isEqualTo(expected); |
| 97 | + } |
| 98 | + |
| 99 | + @DisplayName("Should get the JSON node's value as a List.") |
| 100 | + @ParameterizedTest |
| 101 | + @MethodSource("testGetListArguments") |
| 102 | + public <T> void testGetList( |
| 103 | + final String bodyString, |
| 104 | + final Class<T> clazz, |
| 105 | + final List<T> expected |
| 106 | + ) { |
| 107 | + //GIVEN |
| 108 | + final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper); |
| 109 | + //WHEN |
| 110 | + final List<T> actual = graphQLResponse.getList(DATA_PATH, clazz); |
| 111 | + //THEN |
| 112 | + assertThat(actual).containsExactlyElementsOf(expected); |
| 113 | + } |
| 114 | + |
| 115 | + @Data |
| 116 | + @AllArgsConstructor |
| 117 | + @NoArgsConstructor |
| 118 | + private static class FooBar { |
| 119 | + private String foo; |
| 120 | + private BigDecimal bar; |
| 121 | + } |
| 122 | +} |
0 commit comments