|
| 1 | +/** |
| 2 | + * Copyright 2016 Yurii Rashkovskii |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + */ |
| 15 | +package graphql.servlet |
| 16 | + |
| 17 | +import graphql.annotations.GraphQLAnnotations |
| 18 | +import graphql.annotations.GraphQLField |
| 19 | +import graphql.annotations.GraphQLName |
| 20 | +import graphql.schema.GraphQLFieldDefinition |
| 21 | +import graphql.schema.GraphQLObjectType |
| 22 | +import lombok.SneakyThrows |
| 23 | +import spock.lang.Specification |
| 24 | + |
| 25 | +import static graphql.Scalars.GraphQLInt |
| 26 | +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition |
| 27 | + |
| 28 | +class OsgiGraphQLServletSpec extends Specification { |
| 29 | + |
| 30 | + static class TestQueryProvider implements GraphQLQueryProvider { |
| 31 | + |
| 32 | + @GraphQLName("query") |
| 33 | + static class Query { |
| 34 | + @GraphQLField |
| 35 | + public String field; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + @SneakyThrows |
| 40 | + GraphQLObjectType getQuery() { |
| 41 | + return GraphQLAnnotations.object(Query.class); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + Object context() { |
| 46 | + return new Query(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + def "query provider adds query objects"() { |
| 51 | + setup: |
| 52 | + OsgiGraphQLServlet servlet = new OsgiGraphQLServlet() |
| 53 | + TestQueryProvider queryProvider = new TestQueryProvider() |
| 54 | + servlet.bindQueryProvider(queryProvider) |
| 55 | + GraphQLFieldDefinition query |
| 56 | + |
| 57 | + when: |
| 58 | + query = servlet.getSchema().getQueryType().getFieldDefinition("query") |
| 59 | + then: |
| 60 | + query.getType().getName() == "query" |
| 61 | + |
| 62 | + when: |
| 63 | + query = servlet.getReadOnlySchema().getQueryType().getFieldDefinition("query") |
| 64 | + then: |
| 65 | + query.getType().getName() == "query" |
| 66 | + |
| 67 | + when: |
| 68 | + servlet.unbindQueryProvider(queryProvider) |
| 69 | + then: |
| 70 | + servlet.getSchema().getQueryType().getFieldDefinitions().isEmpty() |
| 71 | + servlet.getReadOnlySchema().getQueryType().getFieldDefinitions().isEmpty() |
| 72 | + } |
| 73 | + |
| 74 | + static class TestMutationProvider implements GraphQLMutationProvider { |
| 75 | + @Override |
| 76 | + Collection<GraphQLFieldDefinition> getMutations() { |
| 77 | + return Collections.singletonList(newFieldDefinition().name("int").type(GraphQLInt).staticValue(1).build()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + def "mutation provider adds mutation objects"() { |
| 82 | + setup: |
| 83 | + OsgiGraphQLServlet servlet = new OsgiGraphQLServlet(); |
| 84 | + TestMutationProvider mutationProvider = new TestMutationProvider(); |
| 85 | + |
| 86 | + when: |
| 87 | + servlet.bindMutationProvider(mutationProvider) |
| 88 | + then: |
| 89 | + servlet.getSchema().getMutationType().getFieldDefinition("int").getType() == GraphQLInt |
| 90 | + servlet.getReadOnlySchema().getMutationType() == null |
| 91 | + |
| 92 | + when: |
| 93 | + servlet.unbindMutationProvider(mutationProvider) |
| 94 | + then: |
| 95 | + servlet.getSchema().getMutationType() == null |
| 96 | + } |
| 97 | + |
| 98 | +// @Test |
| 99 | +// @SneakyThrows |
| 100 | +// public void schema() { |
| 101 | +// OsgiGraphQLServlet servlet = new OsgiGraphQLServlet(); |
| 102 | +// |
| 103 | +// HttpServletRequest req = mock(HttpServletRequest.class); |
| 104 | +// when(req.getPathInfo()).thenReturn("/schema.json"); |
| 105 | +// HttpServletResponse resp = mock(HttpServletResponse.class); |
| 106 | +// |
| 107 | +// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 108 | +// PrintWriter writer = new PrintWriter(outputStream, true); |
| 109 | +// when(resp.getWriter()).thenReturn(writer); |
| 110 | +// |
| 111 | +// verify(resp, times(0)).setStatus(anyInt()); |
| 112 | +// |
| 113 | +// servlet.bindQueryProvider(new TestQueryProvider()); |
| 114 | +// servlet.doGet(req, resp); |
| 115 | +// |
| 116 | +// writer.flush(); |
| 117 | +// |
| 118 | +// Map<String, Object> response = new ObjectMapper().readValue(outputStream.toByteArray(), new TypeReference<Map<String, Object>>() { |
| 119 | +// }); |
| 120 | +// assertTrue(response.containsKey("data")); |
| 121 | +// assertFalse(response.containsKey("errors")); |
| 122 | +// } |
| 123 | + |
| 124 | +} |
0 commit comments