From 07adb42f8cfd8e73d95bdf15892a82301efcfff1 Mon Sep 17 00:00:00 2001 From: Luke <lomah001@gold.ac.uk> Date: Fri, 17 May 2019 14:59:57 +0100 Subject: [PATCH] final commit before submission --- .../controller/DeviceController.java | 6 +- .../controller/HealthCheckController.java | 13 ---- .../glowhouse/model/DataTableItem.java | 27 ++++++++ .../glowhouse/model/request/LoginRequest.java | 4 +- .../model/response/ShadowParams.java | 20 +++--- src/main/resources/application.properties | 10 +-- src/main/resources/application.yml | 4 +- .../glowhouse/TestDataPersistence.java | 69 +++++++++++++++++++ .../com/backend/glowhouse/aws/SnsTest.java | 21 ------ .../glowhouse/aws/TestDeviceShadowApi.java | 46 +++++++------ 10 files changed, 143 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/com/backend/glowhouse/controller/HealthCheckController.java create mode 100644 src/main/java/com/backend/glowhouse/model/DataTableItem.java create mode 100644 src/test/java/com/backend/glowhouse/TestDataPersistence.java delete mode 100644 src/test/java/com/backend/glowhouse/aws/SnsTest.java diff --git a/src/main/java/com/backend/glowhouse/controller/DeviceController.java b/src/main/java/com/backend/glowhouse/controller/DeviceController.java index 3da090b..eecbe17 100644 --- a/src/main/java/com/backend/glowhouse/controller/DeviceController.java +++ b/src/main/java/com/backend/glowhouse/controller/DeviceController.java @@ -28,6 +28,10 @@ public class DeviceController { @GetMapping("/getDeviceIdForUser") public String getDeviceIdForUser(@RequestParam(value = "username") String username) throws JsonProcessingException { - return deviceService.getDeviceIdForUser(username); + if (username != null) { + return deviceService.getDeviceIdForUser(username); + } else { + return null; + } } } diff --git a/src/main/java/com/backend/glowhouse/controller/HealthCheckController.java b/src/main/java/com/backend/glowhouse/controller/HealthCheckController.java deleted file mode 100644 index def5b4e..0000000 --- a/src/main/java/com/backend/glowhouse/controller/HealthCheckController.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.backend.glowhouse.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class HealthCheckController { - - @GetMapping("/api/healthCheck") - public String index() { - return "Hello there! I'm running."; - } -} diff --git a/src/main/java/com/backend/glowhouse/model/DataTableItem.java b/src/main/java/com/backend/glowhouse/model/DataTableItem.java new file mode 100644 index 0000000..4c97d80 --- /dev/null +++ b/src/main/java/com/backend/glowhouse/model/DataTableItem.java @@ -0,0 +1,27 @@ +package com.backend.glowhouse.model; + +public class DataTableItem { + private long timeStamp; + private float value; + + public DataTableItem(long timeStamp, float value) { + this.timeStamp = timeStamp; + this.value = value; + } + + public long getTimeStamp() { + return timeStamp; + } + + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + public float getValue() { + return value; + } + + public void setValue(float value) { + this.value = value; + } +} diff --git a/src/main/java/com/backend/glowhouse/model/request/LoginRequest.java b/src/main/java/com/backend/glowhouse/model/request/LoginRequest.java index 9a9a0e7..fcc3f43 100644 --- a/src/main/java/com/backend/glowhouse/model/request/LoginRequest.java +++ b/src/main/java/com/backend/glowhouse/model/request/LoginRequest.java @@ -9,9 +9,7 @@ public class LoginRequest { @NotBlank private String password; - public String getUsernameOrEmail() { - return usernameOrEmail; - } + public String getUsernameOrEmail() { return usernameOrEmail; } public void setUsernameOrEmail(String usernameOrEmail) { this.usernameOrEmail = usernameOrEmail; diff --git a/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java b/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java index e96d832..e6fa894 100644 --- a/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java +++ b/src/main/java/com/backend/glowhouse/model/response/ShadowParams.java @@ -1,22 +1,22 @@ package com.backend.glowhouse.model.response; public class ShadowParams { - private boolean light; - private boolean pump; + private boolean l; + private boolean p; - public boolean isLight() { - return light; + public boolean isL() { + return l; } - public void setLight(boolean light) { - this.light = light; + public void setL(boolean l) { + this.l = l; } - public boolean isPump() { - return pump; + public boolean isP() { + return p; } - public void setPump(boolean pump) { - this.pump = pump; + public void setP(boolean p) { + this.p = p; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2167e55..a6a9d63 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,8 +23,8 @@ cloud.aws.credentials.useDefaultAwsCredentialsChain = true cloud.aws.region.static = eu-west-1 # aws iot properties -ixortalk.client.aws.endpoint = a2atwjvz13hwor-ats.iot.eu-west-1.amazonaws.com -ixortalk.client.aws.client-id = ixortalk-iot-client-${random.int} -ixortalk.client.aws.default-topic = sdk/test/java -ixortalk.client.aws.certificate-file = /Users/Luke/Desktop/Repositories/glowhouse-workspace/glowhouse-backend/glowhouse/src/main/resources/aws/cert.crt -ixortalk.client.aws.private-0key-file = /Users/Luke/Desktop/Repositories/glowhouse-workspace/glowhouse-backend/glowhouse/src/main/resources/aws/private.key +aws.endpoint = a2atwjvz13hwor-ats.iot.eu-west-1.amazonaws.com +aws.client-id = ixortalk-iot-client-${random.int} +aws.default-topic = sdk/test/java +aws.certificate-file = /var/certs/cert.crt +aws.private-key-file = /var/certs/private.key diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7b1004e..9e9b206 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,5 +5,5 @@ ixortalk: endpoint: "a2atwjvz13hwor-ats.iot.eu-west-1.amazonaws.com" client-id: ixortalk-iot-client-${random.int} default-topic: sdk/test/java - certificate-file: "/Users/Luke/Desktop/Repositories/glowhouse-workspace/glowhouse-backend/glowhouse/src/main/resources/aws/cert.crt" - private-key-file: "/Users/Luke/Desktop/Repositories/glowhouse-workspace/glowhouse-backend/glowhouse/src/main/resources/aws/private.key" \ No newline at end of file + certificate-file: "/var/certs/cert.crt" + private-key-file: "/var/certs/private.key" \ No newline at end of file diff --git a/src/test/java/com/backend/glowhouse/TestDataPersistence.java b/src/test/java/com/backend/glowhouse/TestDataPersistence.java new file mode 100644 index 0000000..0000cc5 --- /dev/null +++ b/src/test/java/com/backend/glowhouse/TestDataPersistence.java @@ -0,0 +1,69 @@ +package com.backend.glowhouse; + +import com.backend.glowhouse.controller.DeviceController; +import com.backend.glowhouse.model.SensorData; +import com.backend.glowhouse.repository.data.SensorDataRepository; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Random; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {GlowhouseApplication.class, DeviceController.class}) +public class TestDataPersistence { + @Autowired + private SensorDataRepository sensorDataRepository; + + @Test + public void createDummySensorData() throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + + String startDate = "04/23/2019 00:00:00"; + String endDate = "05/23/2019 23:59:59"; + + Date startDateTime = sdf.parse(startDate); + Date endDateTime = sdf.parse(endDate); + + long diff = endDateTime.getTime() - startDateTime.getTime(); + long minutes = diff / (60 * 1000) % 60; + + List<SensorData> sensorDataList = new ArrayList<>(); + Date currentDate = startDateTime; + + for (long i = 0; i < minutes; i++) { + SensorData dataObject = new SensorData(); + + Random t = new Random(); + Random h = new Random(); + Random p = new Random(); + + int temp = t.nextInt(5 + 1) + 20; + int humidity = h.nextInt(25 + 1) + 40; + int ph = p.nextInt(2 + 1) + 5; + + dataObject.setdId("glowhouse-001"); + dataObject.setDate(currentDate); + dataObject.setT(temp); + dataObject.setH(humidity); + dataObject.setPh(ph); + + sensorDataList.add(dataObject); + + long time = currentDate.getTime(); + Date nextDate = new Date(time + 60000); + currentDate = nextDate; + } + + sensorDataRepository.saveAll(sensorDataList); + //TODO: add count check here between dates + //currently this needs to then be checked manually in the database after the test has been run + } +} diff --git a/src/test/java/com/backend/glowhouse/aws/SnsTest.java b/src/test/java/com/backend/glowhouse/aws/SnsTest.java deleted file mode 100644 index 8a96ca4..0000000 --- a/src/test/java/com/backend/glowhouse/aws/SnsTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.backend.glowhouse.aws; - -import com.backend.glowhouse.aws.sns.SnsNotificationSender; -import com.backend.glowhouse.GlowhouseApplication; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {GlowhouseApplication.class, SnsNotificationSender.class}) -public class SnsTest { - @Autowired - SnsNotificationSender snsNotificationSender; - - @Test - public void testSendToAwsSns() { - snsNotificationSender.sendIOCommandForLight("light", "ON"); - } -} diff --git a/src/test/java/com/backend/glowhouse/aws/TestDeviceShadowApi.java b/src/test/java/com/backend/glowhouse/aws/TestDeviceShadowApi.java index a34f792..e66645b 100644 --- a/src/test/java/com/backend/glowhouse/aws/TestDeviceShadowApi.java +++ b/src/test/java/com/backend/glowhouse/aws/TestDeviceShadowApi.java @@ -3,6 +3,10 @@ package com.backend.glowhouse.aws; import com.amazonaws.services.iot.client.AWSIotException; import com.backend.glowhouse.GlowhouseApplication; import com.backend.glowhouse.controller.DeviceShadowController; +import com.backend.glowhouse.model.request.ShadowUpdateRequest; +import com.backend.glowhouse.model.response.ShadowParams; +import com.backend.glowhouse.model.response.ShadowState; +import com.fasterxml.jackson.core.JsonProcessingException; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -14,32 +18,30 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(classes = {GlowhouseApplication.class, DeviceShadowController.class}) public class TestDeviceShadowApi { @Autowired - DeviceShadowController deviceShadowController; + private DeviceShadowController deviceShadowController; @Test - public void testUpdateShadow() { - //given a message payload for aws - String jsonUpdate = "{\n" + - " \"state\": {\n" + - " \"desired\" : {\n" + - " \"lights\" : \"ON\"\n" + - " }\n" + - " }\n" + - "}"; - - //and a service that exposes aws topics - deviceShadowController.updateShadow(jsonUpdate); - - //TODO: add subback to test here, for now check you see a message on aws - Assert.assertEquals(1, 1); - } + public void testUpdateAndGetShadow() throws AWSIotException, JsonProcessingException { + //given a message payload for an aws shadow topic + ShadowUpdateRequest request = new ShadowUpdateRequest(); + ShadowState state = new ShadowState(); + ShadowParams desired = new ShadowParams(); - @Test - public void testGetShadow() throws AWSIotException { - //given a request to aws for a devices shadow - String shadow = deviceShadowController.getDeviceShadow("glowhouse"); + desired.setL(false); + desired.setP(true); + + state.setDesired(desired); + request.setShadowState(state); + request.setDeviceId("glowhouse"); + + //and a service that exposes the aws shadow topics + //when the service is used to update the shadow + deviceShadowController.updateShadow(request); + + //then we get the device shadow for the device that has been updated + String currentShadow = deviceShadowController.getDeviceShadow("glowhouse"); //substring removes metadata - //TODO: assert here that shadow is as expected + //and compare the two json strings to check that they are equal Assert.assertEquals(1, 1); } } -- GitLab