Skip to content

Commit 67e9c14

Browse files
committed
Improve test coverage and change to using loggers as far as possible
1 parent ad63402 commit 67e9c14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+544
-381
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ _site/
2424
docs/_site/
2525

2626
src/test/resources/TestServices/output/*.ttl
27+
out/
28+
*.class

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ dependencies {
4040
testRuntime("org.junit.platform:junit-platform-launcher:1.0.0")
4141

4242
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.11.0'
43+
44+
testCompile group: 'org.powermock', name: 'powermock-core', version: '1.7.3'
45+
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '1.7.3'
4346
}
4447

4548
tasks.withType(FindBugs) {

src/main/java/org/hypergraphql/Controller.java

+22-22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import graphql.GraphQLError;
1111
import org.hypergraphql.config.system.HGQLConfig;
1212
import org.hypergraphql.services.HGQLQueryService;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1315
import spark.ModelAndView;
1416
import spark.Service;
1517
import spark.template.velocity.VelocityTemplateEngine;
@@ -19,6 +21,10 @@
1921
*/
2022
public class Controller {
2123

24+
private final static Logger LOGGER = LoggerFactory.getLogger(Controller.class);
25+
26+
private Service hgqlService;
27+
2228
private static final Map<String, String> MIME_MAP = new HashMap<String, String>() {{
2329
put("application/json+rdf+xml", "RDF/XML");
2430
put("application/json+turtle", "TTL");
@@ -54,8 +60,7 @@ public void start(HGQLConfig config) {
5460
System.out.println("GraphQL server started at: http://localhost:" + config.getGraphqlConfig().port() + config.getGraphqlConfig().graphqlPath());
5561
System.out.println("GraphiQL UI available at: http://localhost:" + config.getGraphqlConfig().port() + config.getGraphqlConfig().graphiqlPath());
5662

57-
Service hgqlService = Service.ignite().port(config.getGraphqlConfig().port());
58-
63+
hgqlService = Service.ignite().port(config.getGraphqlConfig().port());
5964

6065
// get method for accessing the GraphiQL UI
6166

@@ -70,22 +75,20 @@ public void start(HGQLConfig config) {
7075
);
7176
});
7277

73-
74-
// post method for accessing the GraphQL getSetvice
75-
78+
// post method for accessing the GraphQL getService
7679
hgqlService.post(config.getGraphqlConfig().graphqlPath(), (req, res) -> {
7780
ObjectMapper mapper = new ObjectMapper();
7881
HGQLQueryService service = new HGQLQueryService(config);
7982

80-
JsonNode requestObject = mapper.readTree(req.body().toString());
83+
JsonNode requestObject = mapper.readTree(req.body());
8184

8285
String query = requestObject.get("query").asText();
8386

8487
String acceptType = req.headers("accept");
8588

86-
String mime = MIME_MAP.containsKey(acceptType) ? MIME_MAP.get(acceptType) : null;
89+
String mime = MIME_MAP.getOrDefault(acceptType, null);
8790
String contentType = MIME_MAP.containsKey(acceptType) ? acceptType : "application/json";
88-
Boolean graphQLcompatible = (GRAPHQL_COMPATIBLE_TYPE.containsKey(acceptType)) ? GRAPHQL_COMPATIBLE_TYPE.get(acceptType) : true;
91+
Boolean graphQLCompatible = GRAPHQL_COMPATIBLE_TYPE.getOrDefault(acceptType, true);
8992

9093
res.type(contentType);
9194

@@ -96,26 +99,16 @@ public void start(HGQLConfig config) {
9699
res.status(400);
97100
}
98101

99-
if (graphQLcompatible) {
100-
101-
JsonNode resultJson = mapper.readTree(new ObjectMapper().writeValueAsString(result));
102-
return resultJson;
103-
102+
if (graphQLCompatible) {
103+
return mapper.readTree(new ObjectMapper().writeValueAsString(result));
104104
} else {
105-
String resultString;
106-
107105
if (result.containsKey("data")) {
108-
resultString = result.get("data").toString();
106+
return result.get("data").toString();
109107
} else {
110-
111108
JsonNode errorsJson = mapper.readTree(new ObjectMapper().writeValueAsString(errors));
112-
resultString = errorsJson.toString();
113-
109+
return errorsJson.toString();
114110
}
115-
116-
return resultString;
117111
}
118-
119112
});
120113

121114
//Return the internal HGQL schema representation as rdf.
@@ -134,6 +127,13 @@ public void start(HGQLConfig config) {
134127
});
135128
}
136129

130+
public void stop() {
131+
132+
if(hgqlService != null) {
133+
134+
hgqlService.stop();
135+
}
136+
}
137137

138138

139139
}

src/main/java/org/hypergraphql/config/schema/HGQLVocabulary.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import graphql.schema.GraphQLOutputType;
44

5+
import java.util.Collections;
56
import java.util.HashMap;
67
import java.util.Map;
78

8-
import static graphql.Scalars.*;
9+
import static graphql.Scalars.GraphQLBoolean;
10+
import static graphql.Scalars.GraphQLID;
11+
import static graphql.Scalars.GraphQLInt;
12+
import static graphql.Scalars.GraphQLString;
913

1014
public class HGQLVocabulary {
1115

@@ -44,25 +48,23 @@ public class HGQLVocabulary {
4448
public static final String RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
4549

4650

47-
public static final Map<String, String> SCALAR_TYPES = new HashMap<String, String>() {{
51+
public static final Map<String, String> SCALAR_TYPES = Collections.unmodifiableMap(new HashMap<String, String>() {{
4852
put("String", HGQL_STRING);
4953
put("Int", HGQL_Int);
5054
put("Boolean", HGQL_Boolean);
5155
put("ID", HGQL_ID);
52-
}};
56+
}});
5357

54-
public static final Map<String, GraphQLOutputType> SCALAR_TYPES_TO_GRAPHQL_OUTPUT = new HashMap<String, GraphQLOutputType>() {{
58+
public static final Map<String, GraphQLOutputType> SCALAR_TYPES_TO_GRAPHQL_OUTPUT =
59+
Collections.unmodifiableMap(new HashMap<String, GraphQLOutputType>() {{
5560
put(HGQL_STRING, GraphQLString);
5661
put(HGQL_Int, GraphQLInt);
5762
put(HGQL_Boolean, GraphQLBoolean);
5863
put(HGQL_ID, GraphQLID);
59-
}};
64+
}});
6065

61-
public static final Map<String, String> JSONLD = new HashMap<String, String>() {{
66+
public static final Map<String, String> JSONLD = Collections.unmodifiableMap(new HashMap<String, String>() {{
6267
put("_id", "@id");
6368
put("_type", "@type");
64-
}};
65-
66-
67-
69+
}});
6870
}

src/main/java/org/hypergraphql/config/system/HGQLConfig.java

+7-26
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,17 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5-
import com.fasterxml.jackson.core.type.TypeReference;
6-
import com.fasterxml.jackson.databind.JsonNode;
75
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.fasterxml.jackson.databind.node.ObjectNode;
9-
import graphql.ExecutionInput;
10-
import graphql.ExecutionResult;
11-
import graphql.GraphQL;
12-
import graphql.language.TypeDefinition;
13-
import graphql.schema.*;
14-
import graphql.schema.idl.RuntimeWiring;
15-
import graphql.schema.idl.SchemaGenerator;
6+
import graphql.schema.GraphQLSchema;
167
import graphql.schema.idl.SchemaParser;
178
import graphql.schema.idl.TypeDefinitionRegistry;
18-
19-
import java.io.File;
20-
import java.io.IOException;
21-
import java.util.*;
22-
239
import org.apache.log4j.Logger;
24-
25-
import org.hypergraphql.datafetching.services.Service;
26-
import org.hypergraphql.config.schema.FieldConfig;
27-
import org.hypergraphql.config.schema.QueryFieldConfig;
28-
import org.hypergraphql.config.schema.TypeConfig;
2910
import org.hypergraphql.datamodel.HGQLSchema;
3011
import org.hypergraphql.datamodel.HGQLSchemaWiring;
3112

32-
import static graphql.Scalars.*;
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.util.List;
3316

3417
/**
3518
* Created by szymon on 05/09/2017.
@@ -110,11 +93,6 @@ public HGQLConfig(String propertyFilepath) {
11093

11194
}
11295

113-
114-
public List<ServiceConfig> getServiceConfigs() {
115-
return serviceConfigs;
116-
}
117-
11896
public GraphqlConfig getGraphqlConfig() {
11997
return graphqlConfig;
12098
}
@@ -128,6 +106,9 @@ public TypeDefinitionRegistry getRegistry() {
128106
return registry;
129107
}
130108

109+
public String getSchemaFile() {
110+
return schemaFile;
111+
}
131112
}
132113

133114

src/main/java/org/hypergraphql/datafetching/ExecutionTreeNode.java

+48-16
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.node.ArrayNode;
66
import com.fasterxml.jackson.databind.node.ObjectNode;
7-
import graphql.language.*;
7+
import graphql.language.Argument;
8+
import graphql.language.BooleanValue;
9+
import graphql.language.Field;
10+
import graphql.language.IntValue;
11+
import graphql.language.Node;
12+
import graphql.language.Selection;
13+
import graphql.language.SelectionSet;
14+
import graphql.language.StringValue;
15+
import graphql.language.Value;
816
import org.apache.jena.rdf.model.Model;
917
import org.apache.log4j.Logger;
1018
import org.hypergraphql.config.schema.FieldOfTypeConfig;
11-
import org.hypergraphql.datamodel.HGQLSchema;
1219
import org.hypergraphql.config.schema.HGQLVocabulary;
1320
import org.hypergraphql.datafetching.services.Service;
14-
15-
import java.util.*;
21+
import org.hypergraphql.datamodel.HGQLSchema;
22+
import org.hypergraphql.exception.HGQLConfigurationException;
23+
24+
import java.util.Collection;
25+
import java.util.HashMap;
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import java.util.UUID;
1631
import java.util.concurrent.ExecutionException;
1732
import java.util.concurrent.ExecutorService;
1833
import java.util.concurrent.Executors;
@@ -58,7 +73,6 @@ public String getExecutionId() {
5873
return executionId;
5974
}
6075

61-
6276
public Map<String, String> getFullLdContext() {
6377

6478
Map<String, String> result = new HashMap<>(ldContext);
@@ -77,7 +91,11 @@ public Map<String, String> getFullLdContext() {
7791

7892
public ExecutionTreeNode(Field field, String nodeId , HGQLSchema schema ) {
7993

80-
this.service = schema.getQueryFields().get(field.getName()).service();
94+
if(schema.getQueryFields().containsKey(field.getName())) {
95+
this.service = schema.getQueryFields().get(field.getName()).service();
96+
} else {
97+
throw new HGQLConfigurationException("Field '" + field.getName() + "' not found in schema");
98+
}
8199
this.executionId = createId();
82100
this.childrenNodes = new HashMap<>();
83101
this.ldContext = new HashMap<>();
@@ -113,14 +131,14 @@ public String toString(int i) {
113131
.append(space).append("Query: ").append(this.query.toString()).append("\n")
114132
.append(space).append("Root type: ").append(this.rootType).append("\n")
115133
.append(space).append("LD context: ").append(this.ldContext.toString()).append("\n");
116-
Set<String> children = this.childrenNodes.keySet();
134+
Set<Map.Entry<String, ExecutionForest>> children = this.childrenNodes.entrySet();
117135
if (!children.isEmpty()) {
118136
result.append(space).append("Children nodes: \n");
119-
for (String child : children) {
137+
for (Map.Entry<String, ExecutionForest> child : children) {
120138
result.append(space).append("\tParent marker: ")
121-
.append(child).append("\n")
139+
.append(child.getKey()).append("\n")
122140
.append(space).append("\tChildren execution nodes: \n")
123-
.append(this.childrenNodes.get(child).toString(i+1)).append("\n");
141+
.append(child.getValue().toString(i+1)).append("\n");
124142
}
125143
}
126144

@@ -189,7 +207,7 @@ private String getContextLdValue(String contextLdKey) {
189207
private JsonNode traverse(Field field, String parentId, String parentType) {
190208

191209
SelectionSet subFields = field.getSelectionSet();
192-
if (subFields!=null) {
210+
if (subFields != null) {
193211

194212
FieldOfTypeConfig fieldConfig = hgqlSchema.getTypes().get(parentType).getField(field.getName());
195213
String targetName = fieldConfig.getTargetName();
@@ -198,10 +216,15 @@ private JsonNode traverse(Field field, String parentId, String parentType) {
198216

199217
Set<Service> serviceCalls = splitFields.keySet();
200218

201-
202-
for (Service serviceCall : serviceCalls) {
203-
if (serviceCall != this.service) {
204-
ExecutionTreeNode childNode = new ExecutionTreeNode(serviceCall, splitFields.get(serviceCall), parentId, targetName, hgqlSchema);
219+
for (Map.Entry<Service, Set<Field>> entry : splitFields.entrySet()) {
220+
if (!entry.getKey().equals(this.service)) {
221+
ExecutionTreeNode childNode = new ExecutionTreeNode(
222+
entry.getKey(),
223+
entry.getValue(),
224+
parentId,
225+
targetName,
226+
hgqlSchema
227+
);
205228

206229
if (this.childrenNodes.containsKey(parentId)) {
207230
try {
@@ -291,8 +314,17 @@ private Map<Service, Set<Field>> getPartitionedFields(String parentType, Selecti
291314

292315
Service serviceConfig;
293316

294-
serviceConfig = hgqlSchema.getTypes().get(parentType).getFields().get(field.getName()).getService();
317+
if(hgqlSchema.getTypes().containsKey(parentType)) {
295318

319+
if(hgqlSchema.getTypes().get(parentType).getFields().containsKey(field.getName())) {
320+
serviceConfig = hgqlSchema.getTypes().get(parentType).getFields().get(field.getName()).getService();
321+
} else {
322+
throw new HGQLConfigurationException("Schema is missing field '"
323+
+ parentType + "::" + field.getName() + "'");
324+
}
325+
} else {
326+
throw new HGQLConfigurationException("Schema is missing type '" + parentType + "'");
327+
}
296328

297329
if (result.containsKey(serviceConfig)) {
298330

src/main/java/org/hypergraphql/datafetching/FetchingExecution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public FetchingExecution(Set<String> inputValues, ExecutionTreeNode node) {
2525
}
2626

2727
@Override
28-
public Model call() throws Exception {
28+
public Model call() {
2929
return node.generateTreeModel(inputValues);
3030
}
3131
}

src/main/java/org/hypergraphql/datafetching/LocalSPARQLExecution.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.hypergraphql.datafetching;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4-
import org.apache.jena.query.*;
4+
import org.apache.jena.query.Query;
5+
import org.apache.jena.query.QueryExecution;
6+
import org.apache.jena.query.QueryExecutionFactory;
7+
import org.apache.jena.query.QueryFactory;
8+
import org.apache.jena.query.QuerySolution;
9+
import org.apache.jena.query.ResultSet;
510
import org.apache.jena.rdf.model.Model;
611
import org.apache.jena.rdf.model.ModelFactory;
712
import org.hypergraphql.datafetching.services.SPARQLEndpointService;
@@ -24,7 +29,7 @@ public LocalSPARQLExecution(JsonNode query, Set<String> inputSubset, Set<String>
2429
}
2530

2631
@Override
27-
public SPARQLExecutionResult call() throws Exception {
32+
public SPARQLExecutionResult call() {
2833
Map<String, Set<String>> resultSet = new HashMap<>();
2934
for (String marker : markers) {
3035
resultSet.put(marker, new HashSet<>());
@@ -39,7 +44,7 @@ public SPARQLExecutionResult call() throws Exception {
3944
Query jenaQuery = QueryFactory.create(sparqlQuery);
4045

4146
QueryExecution qexec = QueryExecutionFactory.create(jenaQuery, model);
42-
ResultSet results = qexec.execSelect();
47+
ResultSet results = qexec.execSelect();
4348

4449

4550
while (results.hasNext()) {
@@ -62,4 +67,4 @@ public SPARQLExecutionResult call() throws Exception {
6267
return sparqlExecutionResult;
6368
}
6469

65-
}
70+
}

0 commit comments

Comments
 (0)