Skip to content

Commit b4fb0f9

Browse files
author
Michael Cuthbert
authored
Merge pull request #75 from osmlab/dev
Merging to main
2 parents 3968d55 + 2c56b1f commit b4fb0f9

File tree

7 files changed

+155
-113
lines changed

7 files changed

+155
-113
lines changed

.github/workflows/pull-request.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Pull Request Build
2+
on: [pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
# Jdk 17 requires gradle 7.3+ https://docs.gradle.org/current/userguide/compatibility.html
9+
java: [11]
10+
steps:
11+
- uses: actions/checkout@v2
12+
with:
13+
fetch-depth: 0
14+
- name: Set up JDK ${{ matrix.java }}
15+
uses: actions/setup-java@v2
16+
with:
17+
distribution: 'zulu'
18+
java-version: ${{ matrix.java }}
19+
- uses: actions/[email protected]
20+
id: gradle-cache
21+
with:
22+
path: |
23+
~/.gradle/caches
24+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
25+
- uses: actions/[email protected]
26+
id: gradle-wrapper-cache
27+
with:
28+
path: |
29+
~/.gradle/wrapper
30+
key: ${{ runner.os }}-gradlewrapper-${{ hashFiles('gradle/wrapper/*') }}
31+
- name: Build
32+
run: ./gradlew build
33+
validation:
34+
name: "Gradle Validation"
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v2
38+
- uses: gradle/[email protected]

src/integrationTest/java/org/maproulette/client/batch/BatchUploaderIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ private void addDefaultTasks(final BatchUploader uploader, final long parentIden
117117
for (int taskIndex = 0; taskIndex < NUMBER_TASKS; taskIndex++)
118118
{
119119
uploader.addTask(newChallenge, this.getDefaultTask(-1, "Task" + taskIndex));
120-
LOG.debug("Added task name=Task{} parentId={} challengeName={}", taskIndex, parentIdentifier, newChallenge.getName());
120+
LOG.debug("Added task name=Task{} parentId={} challengeName={}", taskIndex,
121+
parentIdentifier, newChallenge.getName());
121122
}
122123
}
123124
}

src/main/java/org/maproulette/client/model/RuleList.java

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package org.maproulette.client.model;
22

3+
import java.io.IOException;
4+
import java.io.Serializable;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.maproulette.client.exception.MapRouletteRuntimeException;
9+
import org.maproulette.client.utilities.ObjectMapperSingleton;
10+
311
import com.fasterxml.jackson.annotation.JsonValue;
412
import com.fasterxml.jackson.core.JsonGenerator;
513
import com.fasterxml.jackson.core.JsonParser;
@@ -11,17 +19,11 @@
1119
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1220
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
1321
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
22+
1423
import lombok.AllArgsConstructor;
1524
import lombok.Builder;
1625
import lombok.Data;
1726
import lombok.NoArgsConstructor;
18-
import org.maproulette.client.exception.MapRouletteRuntimeException;
19-
import org.maproulette.client.utilities.ObjectMapperSingleton;
20-
21-
import java.io.IOException;
22-
import java.io.Serializable;
23-
import java.util.ArrayList;
24-
import java.util.List;
2527

2628
/**
2729
* @author mcuthbert
@@ -53,44 +55,48 @@ public String toJson()
5355
try
5456
{
5557
return ObjectMapperSingleton.getMapper().writeValueAsString(this);
56-
} catch (final JsonProcessingException e)
58+
}
59+
catch (final JsonProcessingException e)
5760
{
5861
throw new MapRouletteRuntimeException(e);
5962
}
6063
}
6164

65+
/**
66+
* Serialize a {@code RuleList}
67+
*/
6268
public static class RuleListSerializer extends StdSerializer<RuleList>
6369
{
64-
6570
public RuleListSerializer()
6671
{
6772
this(null);
6873
}
6974

70-
public RuleListSerializer(Class<RuleList> t)
75+
public RuleListSerializer(final Class<RuleList> clazzRuleList)
7176
{
72-
super(t);
77+
super(clazzRuleList);
7378
}
7479

75-
private static void serializeRuleListHelper(List<RuleList> ruleList, JsonGenerator gen) throws IOException
80+
private static void serializeRuleListHelper(final List<RuleList> ruleListList,
81+
final JsonGenerator gen) throws IOException
7682
{
77-
for (RuleList r : ruleList)
83+
for (final RuleList ruleList : ruleListList)
7884
{
7985
gen.writeStartObject();
80-
gen.writeStringField("condition", r.getCondition());
86+
gen.writeStringField("condition", ruleList.getCondition());
8187
gen.writeArrayFieldStart("rules");
82-
if (r.getRuleList() != null)
88+
if (ruleList.getRuleList() != null)
8389
{
84-
for (RuleList it : r.getRuleList())
90+
for (final RuleList nestedRuleList : ruleList.getRuleList())
8591
{
86-
serializeRuleListHelper(it.getRuleList(), gen);
92+
serializeRuleListHelper(nestedRuleList.getRuleList(), gen);
8793
}
8894
}
89-
if (r.getRules() != null)
95+
if (ruleList.getRules() != null)
9096
{
91-
for (PriorityRule p : r.getRules())
97+
for (final PriorityRule priorityRule : ruleList.getRules())
9298
{
93-
gen.writeObject(p);
99+
gen.writeObject(priorityRule);
94100
}
95101
}
96102
gen.writeEndArray();
@@ -99,7 +105,8 @@ private static void serializeRuleListHelper(List<RuleList> ruleList, JsonGenerat
99105
}
100106

101107
@Override
102-
public void serialize(RuleList value, JsonGenerator gen, SerializerProvider serializers) throws IOException
108+
public void serialize(final RuleList value, final JsonGenerator gen,
109+
final SerializerProvider serializers) throws IOException
103110
{
104111
gen.writeStartObject();
105112
gen.writeStringField("condition", value.getCondition());
@@ -111,55 +118,56 @@ public void serialize(RuleList value, JsonGenerator gen, SerializerProvider seri
111118

112119
if (value.getRules() != null)
113120
{
114-
for (PriorityRule p : value.getRules())
121+
for (final PriorityRule priorityRule : value.getRules())
115122
{
116-
gen.writeObject(p);
123+
gen.writeObject(priorityRule);
117124
}
118125
}
119126
gen.writeEndArray();
120127
gen.writeEndObject();
121128
}
122129
}
123130

131+
/**
132+
* Deserialize a {@code RuleList}
133+
*/
124134
public static class RuleListDeserializer extends StdDeserializer<RuleList>
125135
{
126-
127136
public RuleListDeserializer()
128137
{
129138
this(null);
130139
}
131140

132-
public RuleListDeserializer(final Class<?> vc)
141+
public RuleListDeserializer(final Class<?> valueClass)
133142
{
134-
super(vc);
143+
super(valueClass);
135144
}
136145

137-
private static RuleList buildRuleListHelper(JsonNode node, DeserializationContext ctxt)
146+
private static RuleList buildRuleListHelper(final JsonNode node,
147+
final DeserializationContext ctxt)
138148
{
139149
if (node.get("condition") == null)
140150
{
141151
return null;
142152
}
143-
final RuleList ret = RuleList.builder()
144-
.condition(node.get("condition").asText())
145-
.ruleList(new ArrayList<>())
146-
.rules(new ArrayList<>())
147-
.build();
153+
final RuleList ret = RuleList.builder().condition(node.get("condition").asText())
154+
.ruleList(new ArrayList<>()).rules(new ArrayList<>()).build();
148155

149-
for (JsonNode it : node.withArray("rules"))
156+
for (final JsonNode jsonNode : node.withArray("rules"))
150157
{
151-
if (it.get("condition") != null)
158+
if (jsonNode.get("condition") != null)
152159
{
153-
// If the child is a PriorityRule, do a recursive call to build the rule and add it to the list.
154-
RuleList child = buildRuleListHelper(it, ctxt);
160+
// If the child is a PriorityRule, do a recursive call to build the rule and add
161+
// it to the list.
162+
final RuleList child = buildRuleListHelper(jsonNode, ctxt);
155163
ret.getRuleList().add(child);
156-
} else
164+
}
165+
else
157166
{
158-
PriorityRule priorityRule = PriorityRule.builder()
159-
.type(it.get("type").asText())
160-
.operator(it.get("operator").asText())
161-
.value(it.get("value").asText())
162-
.build();
167+
final PriorityRule priorityRule = PriorityRule.builder()
168+
.type(jsonNode.get("type").asText())
169+
.operator(jsonNode.get("operator").asText())
170+
.value(jsonNode.get("value").asText()).build();
163171
ret.getRules().add(priorityRule);
164172
}
165173
}
@@ -168,9 +176,10 @@ private static RuleList buildRuleListHelper(JsonNode node, DeserializationContex
168176
}
169177

170178
@Override
171-
public RuleList deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
179+
public RuleList deserialize(final JsonParser jsonParser, final DeserializationContext ctxt)
180+
throws IOException
172181
{
173-
JsonNode node = p.getCodec().readTree(p);
182+
final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
174183
return buildRuleListHelper(node, ctxt);
175184
}
176185
}

src/main/java/org/maproulette/client/utilities/ObjectMapperSingleton.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package org.maproulette.client.utilities;
22

3+
import org.maproulette.client.model.RuleList;
4+
35
import com.fasterxml.jackson.databind.ObjectMapper;
46
import com.fasterxml.jackson.databind.module.SimpleModule;
5-
import org.maproulette.client.model.RuleList;
67

7-
public class ObjectMapperSingleton
8+
/**
9+
* ObjectMapperSingleton registers modules and provides a cached mapper.
10+
*
11+
* @author ljdelight
12+
*/
13+
public final class ObjectMapperSingleton
814
{
915
private static volatile ObjectMapper mapper;
1016

@@ -17,7 +23,7 @@ public static ObjectMapper getMapper()
1723
if (mapper == null)
1824
{
1925
mapper = new ObjectMapper();
20-
SimpleModule module = new SimpleModule();
26+
final SimpleModule module = new SimpleModule();
2127
module.addSerializer(RuleList.class, new RuleList.RuleListSerializer());
2228
module.addDeserializer(RuleList.class, new RuleList.RuleListDeserializer());
2329
mapper.registerModule(module);
@@ -27,4 +33,8 @@ public static ObjectMapper getMapper()
2733

2834
return mapper;
2935
}
36+
37+
private ObjectMapperSingleton()
38+
{
39+
}
3040
}

src/main/java/org/maproulette/client/utilities/Utilities.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.maproulette.client.utilities;
22

3-
import org.maproulette.client.exception.MapRouletteException;
4-
53
import java.io.IOException;
64

5+
import org.maproulette.client.exception.MapRouletteException;
6+
77
/**
88
* A generic helper class
99
*

src/test/java/org/maproulette/client/serializer/ChallengeSerializationTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import org.maproulette.client.model.ChallengePriority;
1313
import org.maproulette.client.model.PriorityRule;
1414
import org.maproulette.client.model.RuleList;
15+
import org.maproulette.client.utilities.ObjectMapperSingleton;
1516

1617
import com.fasterxml.jackson.databind.ObjectMapper;
17-
import org.maproulette.client.utilities.ObjectMapperSingleton;
1818

1919
/**
2020
* Tests whether a challenge can be read correctly from resources.
@@ -149,13 +149,11 @@ public void serializationTest() throws Exception
149149
final var highPriority = RuleList.builder().condition("AND")
150150
.rules(Collections.singletonList(PriorityRule.builder().operator("equal")
151151
.type("string").value("priority_pd.3").build()))
152-
.ruleList(new ArrayList<>())
153-
.build();
152+
.ruleList(new ArrayList<>()).build();
154153
final var mediumPriority = RuleList.builder().condition("OR")
155154
.rules(Collections.singletonList(PriorityRule.builder().operator("equal")
156155
.type("string").value("priority_pd.2").build()))
157-
.ruleList(new ArrayList<>())
158-
.build();
156+
.ruleList(new ArrayList<>()).build();
159157

160158
Assertions.assertEquals(DESCRIPTION, deserializedChallenge.getDescription());
161159
Assertions.assertEquals(BLURB, deserializedChallenge.getBlurb());

0 commit comments

Comments
 (0)