Skip to content

Commit 3c4aad3

Browse files
authored
Merge pull request #28 from osmlab/dev
Merging dev to master
2 parents fec54e6 + 5ed7cbc commit 3c4aad3

9 files changed

Lines changed: 124 additions & 35 deletions

File tree

README.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,27 @@ For information on the API Library see the [documentation](docs/using.md).
1919

2020
Here is examples of adding the library into a project.
2121

22-
- <img src="https://search.maven.org/assets/images/gradle.png" width="30" height="30"/> Gradle Groovy DSL:
22+
<img src="https://search.maven.org/assets/images/gradle.png" width="30" height="30"/> Gradle Groovy DSL:
23+
2324
```
25+
repositories
26+
{
27+
maven {
28+
name = "GitHubPackages"
29+
url = uri("https://maven.pkg.github.com/osmlab/maproulette-java-client")
30+
credentials {
31+
username = project.findProperty("gpr.user") ?: System.getProperty("USERNAME")
32+
password = project.findProperty("gpr.key") ?: System.getProperty("PASSWORD")
33+
}
34+
}
35+
}
36+
2437
dependencies
2538
{
2639
implementation "org.maproulette:maproulette-java-client:0.3.0"
2740
}
2841
```
2942

30-
- <img src="https://search.maven.org/assets/images/mvn.png" width="30" height="30"/> Maven:
31-
```
32-
<dependency>
33-
<groupId>org.maproulette</groupId>
34-
<artifactId>maproulette-java-client</artifactId>
35-
<version>0.3.0</version>
36-
</dependency>
37-
```
38-
39-
- <img src="https://search.maven.org/assets/images/sbt.svg" width="30" height="30"/> Scala SBT:
40-
```
41-
libraryDependencies += "org.maproulette" % "maproulette-java-client" % "0.3.0"
42-
```
43-
44-
- <img src="https://search.maven.org/assets/images/ivy.png" width="30" height="30"/> Apache Ivy:
45-
```
46-
<dependency org="org.maproulette" name="maproulette-java-client" rev="0.3.0"/>
47-
```
43+
This project is published using Github Package Registry. Unfortunately one of the drawbacks of using Github Package Registry is that to download a package you have to be authenticated and have permissions to read packages. Hopefully this will change in the future, however for now you will have to supply a username and token to access the package.
4844

45+
The "credentials" section above shows that it will look for any properties found in gradle with `gpr.user` for your Github username and if not found it will look at any passed in system environment variables called `USERNAME`. Likewise it will do the same for password, except obviously looking for `gpr.key` and `PASSWORD` respectively.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=org.maproulette.client
2-
version=0.3.0
2+
version=0.3.1
33

44
project_name="MapRoulette Java Client"
55
project_description="Java Client to connect and use MapRoulette API."

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.io.Serializable;
44

5+
import org.maproulette.client.exception.MapRouletteException;
6+
import org.maproulette.client.utilities.Utilities;
7+
58
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
69
import com.fasterxml.jackson.annotation.JsonInclude;
710
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -63,4 +66,9 @@ public class Challenge implements IMapRouletteObject, Serializable
6366
private Integer defaultBasemap;
6467
private String defaultBasemapId;
6568
private String customBasemap;
69+
70+
public static Challenge fromJson(final String json) throws MapRouletteException
71+
{
72+
return Utilities.fromJson(json, Challenge.class);
73+
}
6674
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.io.Serializable;
44

5+
import org.maproulette.client.exception.MapRouletteException;
6+
import org.maproulette.client.utilities.Utilities;
7+
58
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
69
import com.fasterxml.jackson.annotation.JsonInclude;
710
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -39,6 +42,11 @@ public class Project implements IMapRouletteObject, Serializable
3942
@Builder.Default
4043
private boolean enabled = false;
4144

45+
public static Project fromJson(final String json) throws MapRouletteException
46+
{
47+
return Utilities.fromJson(json, Project.class);
48+
}
49+
4250
/**
4351
* Projects have no parents, so will automatically return -1
4452
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.apache.commons.lang.StringUtils;
1010
import org.maproulette.client.exception.MapRouletteException;
1111
import org.maproulette.client.exception.MapRouletteRuntimeException;
12+
import org.maproulette.client.utilities.Utilities;
1213

1314
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1415
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -205,6 +206,11 @@ public static TaskBuilder builder(final long parentIdentifier, final String name
205206
return taskBuilder().parent(parentIdentifier).name(name);
206207
}
207208

209+
public static Task fromJson(final String json) throws MapRouletteException
210+
{
211+
return Utilities.fromJson(json, Task.class);
212+
}
213+
208214
public TaskBuilder toBuilder(final boolean resetGeometry)
209215
{
210216
if (resetGeometry)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.maproulette.client.utilities;
2+
3+
import java.io.IOException;
4+
5+
import org.maproulette.client.exception.MapRouletteException;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
/**
10+
* A generic helper class
11+
*
12+
* @author mcuthbert
13+
*/
14+
public final class Utilities
15+
{
16+
public static <T> T fromJson(final String json, final Class<T> clazz)
17+
throws MapRouletteException
18+
{
19+
try
20+
{
21+
final var mapper = new ObjectMapper();
22+
return mapper.readValue(json, clazz);
23+
}
24+
catch (final IOException e)
25+
{
26+
throw new MapRouletteException(e);
27+
}
28+
}
29+
30+
private Utilities()
31+
{
32+
// Don't need to the constructor as this a utilities class
33+
}
34+
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.Test;
8+
import org.maproulette.client.exception.MapRouletteException;
89
import org.maproulette.client.model.Challenge;
910
import org.maproulette.client.model.ChallengeDifficulty;
1011
import org.maproulette.client.model.ChallengePriority;
@@ -89,11 +90,11 @@ public void defaultSerializationTest() throws IOException
8990
/**
9091
* Tests that a challenge with no defaultPriority specified gets loaded as defaultPriority=LOW.
9192
*
92-
* @throws IOException
93+
* @throws Exception
9394
* any failure when reading the challenge resource file
9495
*/
9596
@Test
96-
public void serializationNoDefaultPrioritySpecifiedTest() throws IOException
97+
public void serializationNoDefaultPrioritySpecifiedTest() throws Exception
9798
{
9899
final var deserializedChallenge = this.getChallenge("challenges/testChallenge4.json");
99100

@@ -112,11 +113,11 @@ public void serializationNoDefaultPrioritySpecifiedTest() throws IOException
112113
* Test if a challange can be deserialized from a test JSON file. The challenge resource json
113114
* contains no MapRoulette priority information.
114115
*
115-
* @throws IOException
116+
* @throws Exception
116117
* any failure when reading the challenge resource file
117118
*/
118119
@Test
119-
public void serializationNoPriorityTest() throws IOException
120+
public void serializationNoPriorityTest() throws Exception
120121
{
121122
// This line will deserialize the challenge, and if it fails we know it didn't work.
122123
final var deserializedChallenge = this.getChallenge("challenges/testChallenge2.json");
@@ -135,11 +136,11 @@ public void serializationNoPriorityTest() throws IOException
135136
* Test if a challenge can be deserialized from a test JSON file. The challenge contains
136137
* MapRoulette priority information for high and medium priority but not for low priority.
137138
*
138-
* @throws IOException
139+
* @throws Exception
139140
* any failure when reading the challenge resource file
140141
*/
141142
@Test
142-
public void serializationTest() throws IOException
143+
public void serializationTest() throws Exception
143144
{
144145
// This line will deserialize the challenge, and if it fails we know it didn't work.
145146
final var deserializedChallenge = this.getChallenge("challenges/testChallenge.json");
@@ -171,12 +172,11 @@ public void serializationTest() throws IOException
171172
* @param resource
172173
* The path to the resource file
173174
* @return A {@link Challenge} object representing the provided resource.
174-
* @throws IOException
175+
* @throws MapRouletteException
175176
* any failure when reading the challenge resource file
176177
*/
177-
private Challenge getChallenge(final String resource) throws IOException
178+
private Challenge getChallenge(final String resource) throws MapRouletteException
178179
{
179-
final var value = SerializerUtilities.getResourceAsString(resource);
180-
return new ObjectMapper().readValue(value, Challenge.class);
180+
return Challenge.fromJson(SerializerUtilities.getResourceAsString(resource));
181181
}
182182
}

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@ public void projectSerializationTest() throws IOException
2424
final var projectJson = mapper.writeValueAsString(project);
2525
final var deserializedProject = mapper.readValue(projectJson, Project.class);
2626

27-
Assertions.assertEquals(project.getName(), deserializedProject.getName());
28-
Assertions.assertEquals(project.getId(), deserializedProject.getId());
29-
Assertions.assertEquals(project.getDescription(), deserializedProject.getDescription());
30-
Assertions.assertEquals(project.getDisplayName(), deserializedProject.getDisplayName());
31-
Assertions.assertEquals(project.isEnabled(), deserializedProject.isEnabled());
27+
this.verifyProjects(project, deserializedProject);
28+
}
29+
30+
@Test
31+
public void fromJsonTest() throws Exception
32+
{
33+
final var mapper = new ObjectMapper();
34+
final var project = Project.builder().name("TestProject").description("TestDescription")
35+
.displayName("TestDisplayName").enabled(true).id(6875L).build();
36+
final var projectJson = mapper.writeValueAsString(project);
37+
final var deserializedProject = Project.fromJson(projectJson);
38+
39+
this.verifyProjects(project, deserializedProject);
40+
}
41+
42+
private void verifyProjects(final Project project1, final Project project2)
43+
{
44+
Assertions.assertEquals(project1.getName(), project2.getName());
45+
Assertions.assertEquals(project1.getId(), project2.getId());
46+
Assertions.assertEquals(project1.getDescription(), project2.getDescription());
47+
Assertions.assertEquals(project1.getDisplayName(), project2.getDisplayName());
48+
Assertions.assertEquals(project1.isEnabled(), project2.isEnabled());
3249
}
3350
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ public void geometriesSerializationTest() throws IOException
5151
this.verifyTask(task, deserializedTask);
5252
}
5353

54+
@Test
55+
public void fromJsonTest() throws Exception
56+
{
57+
final var testFeatureString = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[%s, %s]},\"properties\": {\"name\":\"%s\"}}";
58+
final var mapper = new ObjectMapper();
59+
final var pointList = Arrays.asList(new PointInformation(1.0, 2.0),
60+
new PointInformation(5.4, 8.7));
61+
62+
final var task = Task.builder(343444454, "TestTask").id(12355655)
63+
.instruction("TestInstruction").priority(ChallengePriority.HIGH)
64+
.status(TaskStatus.DELETED).addPoints(pointList)
65+
.addGeojson(String.format(testFeatureString, 1.0, 2.0, "Feature1"))
66+
.addGeojson(String.format(testFeatureString, 5.0, 6.0, "Feature2")).build();
67+
final var taskJson = mapper.writeValueAsString(task);
68+
69+
final var deserializedTask = Task.fromJson(taskJson);
70+
this.verifyTask(task, deserializedTask);
71+
}
72+
5473
private void verifyTask(final Task task1, final Task task2)
5574
{
5675
Assertions.assertEquals(task1.getId(), task2.getId());

0 commit comments

Comments
 (0)