BAEL-5230 Added code and test for Fauna IoT application article
This commit is contained in:
parent
8db0d7b277
commit
0b81bf5e45
|
@ -41,6 +41,18 @@
|
||||||
<artifactId>spring-security-test</artifactId>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- utils -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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) {
|
||||||
|
}
|
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.baeldung.healthapp.service;
|
||||||
|
|
||||||
|
public interface GeoLocationService {
|
||||||
|
|
||||||
|
String getRegion(double latitude, double longitude);
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -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=fnAEsvJPtDAA0MyJZzrPNfjYGN49aiJe-7QfPdSo
|
||||||
|
|
||||||
|
fauna-connections.US=https://db.us.fauna.com/
|
||||||
|
fauna-secrets.US=fnAEsvKMCyAAQnx8-T_lWbhH91HOH_9374GpD4en
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue