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

Feature/check if values needs to be encoded #47

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.app.5gla</groupId>
<artifactId>fiware-integration-layer</artifactId>
<version>11.0.0</version>
<version>11.1.0</version>

<name>5gLa FIWARE integration layer</name>
<url>https://github.com/vitrum-connect/5gla-fiware-integration-layer</url>
Expand Down 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());
}

}
Loading