diff --git a/src/automation/src/test/java/eu/teraflow/automation/AutomationFunctionalServiceTest.java b/src/automation/src/test/java/eu/teraflow/automation/AutomationFunctionalServiceTest.java index f584a86ca72885df1249ea6d956384b50e129268..276a74742f80d4fcf54649241f0432635f40ee4a 100644 --- a/src/automation/src/test/java/eu/teraflow/automation/AutomationFunctionalServiceTest.java +++ b/src/automation/src/test/java/eu/teraflow/automation/AutomationFunctionalServiceTest.java @@ -1,18 +1,18 @@ /* - * Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +* Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ package eu.teraflow.automation; @@ -22,8 +22,11 @@ import automation.Automation; import context.ContextOuterClass; import eu.teraflow.automation.context.ContextGateway; import eu.teraflow.automation.device.DeviceGateway; -import eu.teraflow.automation.device.model.*; +import eu.teraflow.automation.device.model.ConfigActionEnum; +import eu.teraflow.automation.device.model.ConfigRule; import eu.teraflow.automation.device.model.Device; +import eu.teraflow.automation.device.model.DeviceConfig; +import eu.teraflow.automation.device.model.DeviceOperationalStatus; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.mockito.InjectMock; import io.smallrye.mutiny.Uni; @@ -37,7 +40,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; @QuarkusTest -public class AutomationFunctionalServiceTest { +class AutomationFunctionalServiceTest { private static final Logger LOGGER = Logger.getLogger(AutomationFunctionalServiceTest.class); @Inject AutomationService automationService; @@ -97,8 +100,7 @@ public class AutomationFunctionalServiceTest { deviceConfig -> { LOGGER.infof("Received response %s", deviceConfig); - assertThat(deviceConfig.getDeviceOperationalStatus().toString()) - .isEqualTo(device.getDeviceOperationalStatus().toString()); + assertThat(deviceConfig).hasToString(device.getDeviceOperationalStatus().toString()); assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull(); @@ -163,8 +165,7 @@ public class AutomationFunctionalServiceTest { deviceConfig -> { LOGGER.infof("Received response %s", deviceConfig); - assertThat(deviceConfig.getDeviceOperationalStatus().toString()) - .isEqualTo(device.getDeviceOperationalStatus().toString()); + assertThat(deviceConfig).hasToString(device.getDeviceOperationalStatus().toString()); assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull(); diff --git a/src/automation/src/test/java/eu/teraflow/automation/ContextSubscriberTest.java b/src/automation/src/test/java/eu/teraflow/automation/ContextSubscriberTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6039de58ec8f991f5ccbe0bcfd21871ee1e4c432 --- /dev/null +++ b/src/automation/src/test/java/eu/teraflow/automation/ContextSubscriberTest.java @@ -0,0 +1,199 @@ +package eu.teraflow.automation; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import automation.Automation; +import context.ContextOuterClass; +import eu.teraflow.automation.context.ContextGateway; +import eu.teraflow.automation.context.model.Event; +import eu.teraflow.automation.context.model.EventTypeEnum; +import eu.teraflow.automation.device.model.DeviceEvent; +import io.quarkus.runtime.StartupEvent; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.smallrye.mutiny.Multi; +import java.util.UUID; +import javax.inject.Inject; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@QuarkusTest +class ContextSubscriberTest { + + private static final String UUID_FOR_DEVICE_ROLE_ID = + UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString(); + private static final String UUID_FOR_DEVICE_ID = + UUID.fromString("9f14d0ab-9608-7862-a9e4-5ed26688389c").toString(); + + @Inject ContextSubscriber contextSubscriber; + + @InjectMock ContextGateway contextGateway; + + @InjectMock AutomationService automationService; + + @InjectMock AutomationConfiguration automationConfiguration; + + @Test + void shouldCallAddDeviceUponCreateEvent() { + final var uuidForDeviceRoleId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ROLE_ID).build(); + + final var uuidForDeviceId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ID).build(); + + final var outDeviceId = + ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(uuidForDeviceId).build(); + + final var outDeviceRoleId = + Automation.DeviceRoleId.newBuilder() + .setDevRoleId(uuidForDeviceRoleId) + .setDevId(outDeviceId) + .build(); + + String deviceId = outDeviceRoleId.getDevId().toString(); + + Event event = new Event(3.4, EventTypeEnum.CREATE); + DeviceEvent deviceEvent = new DeviceEvent(deviceId, event); + final var deviceEventsMulti = Multi.createFrom().item(deviceEvent); + + Mockito.when(contextGateway.getDeviceEvents()).thenReturn(deviceEventsMulti); + + contextSubscriber.listenForDeviceEvents(); + + verify(automationService, times(1)).addDevice(deviceId); + } + + @Test + void shouldNotCallAddDeviceUponUpdateEvent() { + final var uuidForDeviceRoleId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ROLE_ID).build(); + + final var uuidForDeviceId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ID).build(); + + final var outDeviceId = + ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(uuidForDeviceId).build(); + + final var outDeviceRoleId = + Automation.DeviceRoleId.newBuilder() + .setDevRoleId(uuidForDeviceRoleId) + .setDevId(outDeviceId) + .build(); + + String deviceId = outDeviceRoleId.getDevId().toString(); + + Event event = new Event(3.4, EventTypeEnum.UPDATE); + DeviceEvent deviceEvent = new DeviceEvent(deviceId, event); + final var deviceEventsMulti = Multi.createFrom().item(deviceEvent); + + Mockito.when(contextGateway.getDeviceEvents()).thenReturn(deviceEventsMulti); + + contextSubscriber.listenForDeviceEvents(); + + verify(automationService, times(0)).addDevice(deviceId); + } + + @Test + void shouldNotCallAddDeviceUponRemoveEvent() { + final var uuidForDeviceRoleId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ROLE_ID).build(); + + final var uuidForDeviceId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ID).build(); + + final var outDeviceId = + ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(uuidForDeviceId).build(); + + final var outDeviceRoleId = + Automation.DeviceRoleId.newBuilder() + .setDevRoleId(uuidForDeviceRoleId) + .setDevId(outDeviceId) + .build(); + + String deviceId = outDeviceRoleId.getDevId().toString(); + + Event event = new Event(3.4, EventTypeEnum.REMOVE); + DeviceEvent deviceEvent = new DeviceEvent(deviceId, event); + final var deviceEventsMulti = Multi.createFrom().item(deviceEvent); + + Mockito.when(contextGateway.getDeviceEvents()).thenReturn(deviceEventsMulti); + + contextSubscriber.listenForDeviceEvents(); + + verify(automationService, times(0)).addDevice(deviceId); + } + + @Test + void shouldNotCallAddDeviceUponNullEvent() { + final var uuidForDeviceRoleId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ROLE_ID).build(); + + final var uuidForDeviceId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ID).build(); + + final var outDeviceId = + ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(uuidForDeviceId).build(); + + final var outDeviceRoleId = + Automation.DeviceRoleId.newBuilder() + .setDevRoleId(uuidForDeviceRoleId) + .setDevId(outDeviceId) + .build(); + + String deviceId = outDeviceRoleId.getDevId().toString(); + + DeviceEvent deviceEvent = new DeviceEvent(deviceId, null); + final var deviceEventsMulti = Multi.createFrom().item(deviceEvent); + + Mockito.when(contextGateway.getDeviceEvents()).thenReturn(deviceEventsMulti); + + contextSubscriber.listenForDeviceEvents(); + + verify(automationService, times(0)).addDevice(deviceId); + } + + @Test + void shouldCallListenForDeviceEventsUponStart() { + final var uuidForDeviceRoleId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ROLE_ID).build(); + + final var uuidForDeviceId = + ContextOuterClass.Uuid.newBuilder().setUuid(UUID_FOR_DEVICE_ID).build(); + + final var outDeviceId = + ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(uuidForDeviceId).build(); + + final var outDeviceRoleId = + Automation.DeviceRoleId.newBuilder() + .setDevRoleId(uuidForDeviceRoleId) + .setDevId(outDeviceId) + .build(); + + String deviceId = outDeviceRoleId.getDevId().toString(); + + Event event = new Event(3.4, EventTypeEnum.CREATE); + DeviceEvent deviceEvent = new DeviceEvent(deviceId, event); + final var deviceEventsMulti = Multi.createFrom().item(deviceEvent); + + Mockito.when(contextGateway.getDeviceEvents()).thenReturn(deviceEventsMulti); + Mockito.when(automationConfiguration.shouldSubscribeToContextComponent()).thenReturn(true); + + StartupEvent y = new StartupEvent(); + contextSubscriber.onStart(y); + + verify(contextGateway, times(1)).getDeviceEvents(); + verify(automationService, times(1)).addDevice(deviceId); + } + + @Test + void shouldNotCallListenForDeviceEventsUponStart() { + final var automationConfiguration = Mockito.mock(AutomationConfiguration.class); + Mockito.when(automationConfiguration.shouldSubscribeToContextComponent()).thenReturn(false); + + StartupEvent y = new StartupEvent(); + contextSubscriber.onStart(y); + + verify(contextGateway, times(0)).getDeviceEvents(); + } +} diff --git a/src/automation/src/test/java/eu/teraflow/automation/MockAutomationConfiguration.java b/src/automation/src/test/java/eu/teraflow/automation/MockAutomationConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..24c0433e0ed5fb5e3d20ff762f640406d8bf9f1b --- /dev/null +++ b/src/automation/src/test/java/eu/teraflow/automation/MockAutomationConfiguration.java @@ -0,0 +1,18 @@ +package eu.teraflow.automation; + +import io.smallrye.config.SmallRyeConfig; +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.ws.rs.Produces; +import org.eclipse.microprofile.config.Config; + +public class MockAutomationConfiguration { + @Inject Config config; + + @Produces + @ApplicationScoped + @io.quarkus.test.Mock + AutomationConfiguration automationConfiguration() { + return config.unwrap(SmallRyeConfig.class).getConfigMapping(AutomationConfiguration.class); + } +}