Skip to content
Snippets Groups Projects
Commit 08afff09 authored by Fotis Soldatos's avatar Fotis Soldatos
Browse files

test(automation): add contextSubscriberTest

parent 783dfb0f
No related branches found
No related tags found
1 merge request!54Release 2.0.0
/* /*
* Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) * Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package eu.teraflow.automation; package eu.teraflow.automation;
...@@ -22,8 +22,11 @@ import automation.Automation; ...@@ -22,8 +22,11 @@ import automation.Automation;
import context.ContextOuterClass; import context.ContextOuterClass;
import eu.teraflow.automation.context.ContextGateway; import eu.teraflow.automation.context.ContextGateway;
import eu.teraflow.automation.device.DeviceGateway; 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.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.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
...@@ -37,7 +40,7 @@ import org.junit.jupiter.api.Test; ...@@ -37,7 +40,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
@QuarkusTest @QuarkusTest
public class AutomationFunctionalServiceTest { class AutomationFunctionalServiceTest {
private static final Logger LOGGER = Logger.getLogger(AutomationFunctionalServiceTest.class); private static final Logger LOGGER = Logger.getLogger(AutomationFunctionalServiceTest.class);
@Inject AutomationService automationService; @Inject AutomationService automationService;
...@@ -97,8 +100,7 @@ public class AutomationFunctionalServiceTest { ...@@ -97,8 +100,7 @@ public class AutomationFunctionalServiceTest {
deviceConfig -> { deviceConfig -> {
LOGGER.infof("Received response %s", deviceConfig); LOGGER.infof("Received response %s", deviceConfig);
assertThat(deviceConfig.getDeviceOperationalStatus().toString()) assertThat(deviceConfig).hasToString(device.getDeviceOperationalStatus().toString());
.isEqualTo(device.getDeviceOperationalStatus().toString());
assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull(); assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull();
...@@ -163,8 +165,7 @@ public class AutomationFunctionalServiceTest { ...@@ -163,8 +165,7 @@ public class AutomationFunctionalServiceTest {
deviceConfig -> { deviceConfig -> {
LOGGER.infof("Received response %s", deviceConfig); LOGGER.infof("Received response %s", deviceConfig);
assertThat(deviceConfig.getDeviceOperationalStatus().toString()) assertThat(deviceConfig).hasToString(device.getDeviceOperationalStatus().toString());
.isEqualTo(device.getDeviceOperationalStatus().toString());
assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull(); assertThat(deviceConfig.getDeviceConfig().toString()).isNotNull();
......
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();
}
}
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment