Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Add jackson-core dependency and refactor JSON handling
Browse files Browse the repository at this point in the history
The jackson-core dependency was added in `pom.xml` to enhance JSON handling in the application. The `DeviceMeasurement` class was refactored to handle null fields when generating JSON output. Helper methods were introduced for managing `externalDataReference`, `dateCreated`, and `location` fields. This change improves output cleanliness and standardizes data for users and systems.
  • Loading branch information
saschadoemer committed Mar 2, 2024
1 parent 92d180b commit 3a35e31
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<artifactId>slf4j-nop</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
43 changes: 31 additions & 12 deletions src/main/java/de/app/fivegla/fiware/model/DeviceMeasurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,46 @@ public String asJson() {
" \"name\":\"" + name + "\"," +
" \"type\":\"" + type + "\"," +
" \"value\":" + value + "," +
" \"externalDataLink\":" + (StringUtils.isNotBlank(externalDataReference) ? externalDataReference : " ") + "\"," +
" \"externalDataLink\":" + externalDataReferenceAsJson() + "," +
" \"dateCreated\":" + dateCreatedAsJson() + "," +
" \"location\":" + locationAsJson() +
"}";
}

private String externalDataReferenceAsJson() {
if (StringUtils.isBlank(externalDataReference)) {
return "{}";
} else {
return "{" +
" \"type\":\"" + FiwareType.TEXT.getKey() + "\"," +
" \"value\":\"" + externalDataReference + "\"" +
"}";
}
}

private String dateCreatedAsJson() {
return "{" +
" \"type\":\"" + FiwareType.DATE_TIME.getKey() + "\"," +
" \"value\":\"" + formatter.format(dateCreated) + "\"" +
"}";
if (null == dateCreated) {
return "{}";
} else {
return "{" +
" \"type\":\"" + FiwareType.DATE_TIME.getKey() + "\"," +
" \"value\":\"" + formatter.format(dateCreated) + "\"" +
"}";
}
}

private String locationAsJson() {
return "{" +
" \"type\":\"" + FiwareType.GEO_POINT.getKey() + "\"," +
" \"value\": {" +
" \"type\":\"Point\"," +
" \"coordinates\": [" + longitude + "," + latitude + "]" +
" }" +
"}";
if (latitude == 0.0 && longitude == 0.0) {
return "{}";
} else {
return "{" +
" \"type\":\"" + FiwareType.GEO_POINT.getKey() + "\"," +
" \"value\": {" +
" \"type\":\"Point\"," +
" \"coordinates\": [" + longitude + "," + latitude + "]" +
" }" +
"}";
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.app.fivegla.fiware.api;


import com.fasterxml.jackson.databind.ObjectMapper;
import de.app.fivegla.fiware.model.builder.DeviceMeasurementBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.UUID;

class DevxiceMeasurementValidJsonTest {

@Test
void givenDeviceMeasurement_WhenCreatingJson_ThenTheResultShouldBeValidJson() throws Exception {
var deviceMeasurementAsJson = new DeviceMeasurementBuilder()
.withId(UUID.randomUUID().toString())
.withType(FiwareType.TEXT.name())
.withMeasurement("value", FiwareType.TEXT, 2.0, Instant.now(), 4.0, 8.0)
.build()
.asJson();
var objectMapper = new ObjectMapper();
var jsonNode = objectMapper.readTree(deviceMeasurementAsJson);
Assertions.assertEquals(FiwareType.TEXT.getKey(), jsonNode.get("type").asText());
Assertions.assertEquals(2.0, jsonNode.get("value").asDouble());
}

@Test
void givenDeviceMeasurementWithExternalDataReference_WhenCreatingJson_ThenTheResultShouldBeValidJson() throws Exception {
var deviceMeasurementAsJson = new DeviceMeasurementBuilder()
.withId(UUID.randomUUID().toString())
.withType(FiwareType.TEXT.name())
.withMeasurement("value", FiwareType.TEXT, 2.0, Instant.now(), 4.0, 8.0)
.withExternalDataReference("http://example.com")
.build()
.asJson();
var objectMapper = new ObjectMapper();
var jsonNode = objectMapper.readTree(deviceMeasurementAsJson);
Assertions.assertEquals(FiwareType.TEXT.getKey(), jsonNode.get("type").asText());
Assertions.assertEquals(2.0, jsonNode.get("value").asDouble());
}

}

0 comments on commit 3a35e31

Please sign in to comment.