Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
feat(StringUtils): clean up and increase test coverage
Browse files Browse the repository at this point in the history
Remove redundant "null"-string checks now covered in value parsing
Increase test coverage above jacoco threshold (88->100%)
  • Loading branch information
mkalen committed Nov 1, 2018
1 parent 27501b2 commit 56ee26e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
7 changes: 5 additions & 2 deletions nodes/src/main/java/io/aexp/nodes/graphql/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

abstract class StringUtil {

private StringUtil() {
}

/**
* Joins and array to a delimited string
*
Expand Down Expand Up @@ -46,7 +49,7 @@ static <T> String formatGraphQLParameter(String key, T value) {
StringBuilder stringBuilder = new StringBuilder();
Pattern pattern = Pattern.compile("^\\$");
Matcher matcher = pattern.matcher("" + value);
if (value instanceof String && !"null".equalsIgnoreCase((String) value) && !matcher.find()) {
if (value instanceof String && !matcher.find()) {
stringBuilder.append(key).append(":\"").append(value).append("\"");
} else if (value instanceof List) {
stringBuilder.append(key).append(":").append(formatGraphQLArgumentList((List) value));
Expand All @@ -71,7 +74,7 @@ private static String formatGraphQLArgumentList(List values) {
for (Object value: values) {
if (stringBuilder.length() != 1) stringBuilder.append(",");
Matcher m = p.matcher("" + value);
if (value instanceof String && !"null".equalsIgnoreCase((String) value) && !m.find()) {
if (value instanceof String && !m.find()) {
stringBuilder.append("\"").append(value).append("\"");
} else if (value instanceof InputObject) {
stringBuilder.append(((InputObject) value).getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package io.aexp.nodes.graphql;

import org.junit.Before;
import org.junit.Test;
import io.aexp.nodes.graphql.models.TestModel;
import io.aexp.nodes.graphql.models.TestModelEnum;
Expand All @@ -21,7 +22,9 @@

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -32,7 +35,15 @@ enum STATUS {
active
}

private String EXAMPLE_URL = "https://graphql.example.com";
private static String EXAMPLE_URL = "https://graphql.example.com";
private List<String> idList;

@Before
public void setUp() {
idList = new ArrayList<String>();
idList.add("47");
idList.add("$id");
}

@Test
public void requestWithoutUrl() {
Expand Down Expand Up @@ -63,23 +74,29 @@ public void requestWithOptionalParameter() throws MalformedURLException {
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.url(EXAMPLE_URL)
.request(TestModelOptionalArguments.class)
.arguments(new Arguments("test.nested", new Argument<Integer>("first", 10)))
.arguments(
new Arguments("test.nested",
new Argument<List<String>>("ids", idList),
new Argument<Integer>("first", 10)))
.build();
requestEntity.setRequestMethod(GraphQLTemplate.GraphQLMethod.QUERY);
assertEquals(EXAMPLE_URL, requestEntity.getUrl().toString());
assertEquals("GraphQLRequestEntity{request='query { test { nested (first:10) { string } } } ', url='"+EXAMPLE_URL+"'}", requestEntity.toString());
assertEquals("GraphQLRequestEntity{request='query { test { nested (ids:[\"47\",$id],first:10) { string } } } ', url='"+EXAMPLE_URL+"'}", requestEntity.toString());
}

@Test
public void requestWithOtherOptionalParameter() throws MalformedURLException {
GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.url(EXAMPLE_URL)
.request(TestModelOptionalArguments.class)
.arguments(new Arguments("test.nested", new Argument<Integer>("last", 5)))
.arguments(
new Arguments("test.nested",
new Argument<List<String>>("ids", idList),
new Argument<Integer>("last", 5)))
.build();
requestEntity.setRequestMethod(GraphQLTemplate.GraphQLMethod.QUERY);
assertEquals(EXAMPLE_URL, requestEntity.getUrl().toString());
assertEquals("GraphQLRequestEntity{request='query { test { nested (last:5) { string } } } ', url='"+EXAMPLE_URL+"'}", requestEntity.toString());
assertEquals("GraphQLRequestEntity{request='query { test { nested (ids:[\"47\",$id],last:5) { string } } } ', url='"+EXAMPLE_URL+"'}", requestEntity.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@GraphQLProperty(name = "test")
public class TestModelOptionalArguments {
@GraphQLArguments({
@GraphQLArgument(name = "ids"),
@GraphQLArgument(name = "first", type = "Integer", optional = true),
@GraphQLArgument(name = "last", type = "Integer", optional = true)
})
Expand Down

0 comments on commit 56ee26e

Please sign in to comment.