diff --git a/src/main/java/com/backend/glowhouse/controller/DeviceController.java b/src/main/java/com/backend/glowhouse/controller/DeviceController.java index 3da090b488508bcd052118b2df84f13c6d11e57a..eecbe172226d731c4cb85a0515fb5ca25c3d879b 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 def5b4e41e4b71d66746924d64efdc4427435334..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..4c97d8022bb50b6d6b1faa8319eba2f1abee142d --- /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 9a9a0e737f380741b4bf45778d0747bc03905a94..fcc3f43d33227914e7c5d29c94d85597ea1bccc1 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 e96d83211a756c63f2720f005ccfe29994fb629a..e6fa894560f195cef40e32092a2e1c154ed652d8 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 2167e55579e36cf11ffa50d943cef6e8f1636098..a6a9d63bdbc655594af9a2be0f61205ec6aea827 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 7b1004e6e2013fc3e86a7cdbfc203146ea7cfdeb..9e9b20669fe5b963f9c15f992183d555e0b8b8c3 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 0000000000000000000000000000000000000000..0000cc5435046cab873950b17ac73b0c20c7a3ac --- /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 8a96ca4bbb5e203b66454f084361a86aa81406a9..0000000000000000000000000000000000000000 --- 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 a34f792db861990ae1046382d2e3c1009f2d17f4..e66645bba713375006a85cb4a0d0a177a7a7381a 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); } }