From 0dc36fdb4369424cee2eaced40314ac831db89fa Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Fri, 5 Aug 2022 00:59:43 +0530 Subject: [PATCH] BAEL-5230 Added code and test for Fauna IoT application article (#12554) * BAEL-5230 Added code and test for Fauna IoT application article * BAEL-5230 Removed secrets from the codebase * BAEL-5230 Removed spring-boot-configuration-processor --- persistence-modules/fauna/pom.xml | 11 +++- .../com/baeldung/healthapp/FaunaClients.java | 42 ++++++++++++++ .../healthapp/FaunaHealthApplication.java | 13 +++++ .../baeldung/healthapp/domain/HealthData.java | 17 ++++++ .../service/DefaultGeoLocationService.java | 12 ++++ .../service/DefaultHealthService.java | 58 +++++++++++++++++++ .../healthapp/service/GeoLocationService.java | 6 ++ .../healthapp/service/HealthService.java | 10 ++++ .../src/main/resources/application.properties | 10 ++++ .../DefaultHealthServiceManualTest.java | 55 ++++++++++++++++++ 10 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaClients.java create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaHealthApplication.java create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/domain/HealthData.java create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultGeoLocationService.java create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultHealthService.java create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/GeoLocationService.java create mode 100644 persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/HealthService.java create mode 100644 persistence-modules/fauna/src/test/java/com/baeldung/healthapp/service/DefaultHealthServiceManualTest.java diff --git a/persistence-modules/fauna/pom.xml b/persistence-modules/fauna/pom.xml index 8c985e0b7c..a00dd0eb8d 100644 --- a/persistence-modules/fauna/pom.xml +++ b/persistence-modules/fauna/pom.xml @@ -3,11 +3,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung fauna - 0.0.1-SNAPSHOT fauna - Blogging Service built with FaunaDB + Code snippets for FaunaDB articles com.baeldung @@ -41,6 +39,13 @@ spring-security-test test + + + + org.projectlombok + lombok + provided + diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaClients.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaClients.java new file mode 100644 index 0000000000..c4e1d52b5b --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaClients.java @@ -0,0 +1,42 @@ +package com.baeldung.healthapp; + +import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import com.faunadb.client.FaunaClient; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@ConfigurationProperties +@Component +public class FaunaClients { + + private final Map faunaConnections = new HashMap<>(); + private final Map faunaSecrets = new HashMap<>(); + + public FaunaClient getFaunaClient(String region) throws MalformedURLException { + + String faunaUrl = faunaConnections.get(region); + String faunaSecret = faunaSecrets.get(region); + + log.info("Creating Fauna Client for Region:{} with URL:{}", region, faunaUrl); + + return FaunaClient.builder() + .withEndpoint(faunaUrl) + .withSecret(faunaSecret) + .build(); + } + + public Map getFaunaConnections() { + return faunaConnections; + } + + public Map getFaunaSecrets() { + return faunaSecrets; + } +} diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaHealthApplication.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaHealthApplication.java new file mode 100644 index 0000000000..ac09e4cd36 --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/FaunaHealthApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.healthapp; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class FaunaHealthApplication { + + public static void main(String[] args) { + SpringApplication.run(FaunaHealthApplication.class, args); + } + +} diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/domain/HealthData.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/domain/HealthData.java new file mode 100644 index 0000000000..9ee320c3d5 --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/domain/HealthData.java @@ -0,0 +1,17 @@ +package com.baeldung.healthapp.domain; + +import java.time.ZonedDateTime; + +public record HealthData( + + String userId, + + float temperature, + float pulseRate, + int bpSystolic, + int bpDiastolic, + + double latitude, + double longitude, + ZonedDateTime timestamp) { +} diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultGeoLocationService.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultGeoLocationService.java new file mode 100644 index 0000000000..b643714972 --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultGeoLocationService.java @@ -0,0 +1,12 @@ +package com.baeldung.healthapp.service; + +import org.springframework.stereotype.Component; + +@Component +public class DefaultGeoLocationService implements GeoLocationService { + + @Override + public String getRegion(double latitude, double longitude) { + return "EU"; + } +} diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultHealthService.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultHealthService.java new file mode 100644 index 0000000000..8e44b4b243 --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/DefaultHealthService.java @@ -0,0 +1,58 @@ +package com.baeldung.healthapp.service; + +import static com.faunadb.client.query.Language.Collection; +import static com.faunadb.client.query.Language.Create; +import static com.faunadb.client.query.Language.Now; +import static com.faunadb.client.query.Language.Obj; +import static com.faunadb.client.query.Language.Value; + +import java.net.MalformedURLException; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.healthapp.FaunaClients; +import com.baeldung.healthapp.domain.HealthData; +import com.faunadb.client.FaunaClient; +import com.faunadb.client.types.Value; + +import lombok.extern.slf4j.Slf4j; + +@Component +@Slf4j +public class DefaultHealthService implements HealthService { + + @Autowired + private GeoLocationService geoLocationService; + + @Autowired + private FaunaClients faunaClients; + + @Override + public void process(HealthData healthData) throws MalformedURLException, InterruptedException, ExecutionException { + + String region = geoLocationService.getRegion( // + healthData.latitude(), // + healthData.longitude()); + + FaunaClient faunaClient = faunaClients.getFaunaClient(region); + + Value queryResponse = faunaClient.query( + Create(Collection("healthdata"), + Obj("data", + Obj(Map.of( + "userId", Value(healthData.userId()), + "temperature", Value(healthData.temperature()), + "pulseRate", Value(healthData.pulseRate()), + "bpSystolic", Value(healthData.bpSystolic()), + "bpDiastolic", Value(healthData.bpDiastolic()), + "latitude", Value(healthData.latitude()), + "longitude", Value(healthData.longitude()), + "timestamp", Now())))) + ).get(); + + log.info("Query response received from Fauna: {}", queryResponse); + } +} diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/GeoLocationService.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/GeoLocationService.java new file mode 100644 index 0000000000..43a3204610 --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/GeoLocationService.java @@ -0,0 +1,6 @@ +package com.baeldung.healthapp.service; + +public interface GeoLocationService { + + String getRegion(double latitude, double longitude); +} diff --git a/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/HealthService.java b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/HealthService.java new file mode 100644 index 0000000000..eadf12cc08 --- /dev/null +++ b/persistence-modules/fauna/src/main/java/com/baeldung/healthapp/service/HealthService.java @@ -0,0 +1,10 @@ +package com.baeldung.healthapp.service; + +import java.net.MalformedURLException; +import java.util.concurrent.ExecutionException; + +import com.baeldung.healthapp.domain.HealthData; + +public interface HealthService { + void process(HealthData healthData) throws MalformedURLException, InterruptedException, ExecutionException; +} diff --git a/persistence-modules/fauna/src/main/resources/application.properties b/persistence-modules/fauna/src/main/resources/application.properties index e69de29bb2..0f50dfa8b3 100644 --- a/persistence-modules/fauna/src/main/resources/application.properties +++ b/persistence-modules/fauna/src/main/resources/application.properties @@ -0,0 +1,10 @@ +# Fauna Blog Service +fauna.region=EU +fauna.secret= + +# Fauna Health App +fauna-connections.EU=https://db.eu.fauna.com/ +fauna-secrets.EU=eu-secret + +fauna-connections.US=https://db.us.fauna.com/ +fauna-secrets.US=us-secret \ No newline at end of file diff --git a/persistence-modules/fauna/src/test/java/com/baeldung/healthapp/service/DefaultHealthServiceManualTest.java b/persistence-modules/fauna/src/test/java/com/baeldung/healthapp/service/DefaultHealthServiceManualTest.java new file mode 100644 index 0000000000..f77eb623b6 --- /dev/null +++ b/persistence-modules/fauna/src/test/java/com/baeldung/healthapp/service/DefaultHealthServiceManualTest.java @@ -0,0 +1,55 @@ +package com.baeldung.healthapp.service; + +import static org.mockito.Mockito.when; + +import java.net.MalformedURLException; +import java.time.ZonedDateTime; +import java.util.concurrent.ExecutionException; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import com.baeldung.healthapp.FaunaHealthApplication; +import com.baeldung.healthapp.domain.HealthData; + +@SpringBootTest(classes = FaunaHealthApplication.class) +class DefaultHealthServiceManualTest { + + @Autowired + private DefaultHealthService defaultHealthService; + + @MockBean + private GeoLocationService geoLocationService; + + @Test + void givenEURegion_whenProcess_thenRequestSentToEURegion() throws MalformedURLException, InterruptedException, ExecutionException { + + HealthData healthData = new HealthData("user-1-eu", // + 37.5f, // + 99f, // + 120, 80, // + 51.50, -0.07, // + ZonedDateTime.now()); + + when(geoLocationService.getRegion(51.50, -0.07)).thenReturn("EU"); + + defaultHealthService.process(healthData); + } + + @Test + void givenUSRegion_whenProcess_thenRequestSentToUSRegion() throws MalformedURLException, InterruptedException, ExecutionException { + + HealthData healthData = new HealthData("user-1-us", // + 38.0f, // + 100f, // + 115, 85, // + 40.75, -74.30, // + ZonedDateTime.now()); + + when(geoLocationService.getRegion(40.75, -74.30)).thenReturn("US"); + + defaultHealthService.process(healthData); + } +}