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
This commit is contained in:
Dhawal Kapil 2022-08-05 00:59:43 +05:30 committed by GitHub
parent d24f73002a
commit 0dc36fdb43
10 changed files with 231 additions and 3 deletions

View File

@ -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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>fauna</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fauna</name>
<description>Blogging Service built with FaunaDB</description>
<description>Code snippets for FaunaDB articles</description>
<parent>
<groupId>com.baeldung</groupId>
@ -41,6 +39,13 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -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<String, String> faunaConnections = new HashMap<>();
private final Map<String, String> 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<String, String> getFaunaConnections() {
return faunaConnections;
}
public Map<String, String> getFaunaSecrets() {
return faunaSecrets;
}
}

View File

@ -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);
}
}

View File

@ -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) {
}

View File

@ -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";
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.healthapp.service;
public interface GeoLocationService {
String getRegion(double latitude, double longitude);
}

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}
}