Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed data service code flow in case of configuration change #5074

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,9 @@ public Map<String, String> getConnectionInfo() {

@Override
public void startConnectionTask() {
startConnectionMonitorTask();
if (!this.dataTransportService.isConnected()) {
startConnectionMonitorTask();
}
}

@Override
Expand Down
1 change: 1 addition & 0 deletions kura/test/org.eclipse.kura.core.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Import-Package: javax.comm,
org.mockito;version="[4.0.0,5.0.0)",
org.mockito.invocation;version="[4.0.0,5.0.0)",
org.mockito.stubbing;version="[4.0.0,5.0.0)",
org.mockito.verification;version="4.8.1",
org.osgi.framework,
org.osgi.service.cm,
org.osgi.service.component,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -45,6 +48,7 @@
import org.eclipse.kura.message.store.StoredMessage;
import org.eclipse.kura.message.store.provider.MessageStore;
import org.eclipse.kura.message.store.provider.MessageStoreProvider;
import org.eclipse.kura.status.CloudConnectionStatusComponent;
import org.eclipse.kura.status.CloudConnectionStatusEnum;
import org.eclipse.kura.status.CloudConnectionStatusService;
import org.eclipse.kura.watchdog.WatchdogService;
Expand All @@ -53,13 +57,15 @@
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.verification.VerificationMode;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;

public class DataServiceImplTest {

private DataServiceImpl dataServiceImpl;
private DataTransportService dataTransportServiceMock;
private CloudConnectionStatusService ccssMock;
private Map<String, Object> properties;
private Optional<Exception> exception = Optional.empty();
private final MessageStoreProvider messageStoreProvider = Mockito.mock(MessageStoreProvider.class);
Expand Down Expand Up @@ -151,6 +157,21 @@ public void shouldStoreMessagesWithPayloadSizeEqualThanConfiguredThreshold() thr
thenNoExceptionIsTrown();
thenMessageIsStored(0, "foo", new byte[4], 0, false, 9);
}

@Test
public void shouldNotDisconnectOnConfigChange() throws KuraStoreException {
givenDataService();
givenMessageStoreProvider();
givenDataTrasportServiceDisconnected();
givenConfigurationProperty("connect.auto-on-startup", true);
givenIsActive();
givenDataTrasportServiceConnected();

whenConfigurationIsChanged("enable.recovery.on.connection.failure", true);

thenDataTrasportStaysConnected();
thenCloudConnectionStatusServiceIsNotChanged();
}

@Test
public void shouldNotStoreMessagesWithPayloadSizeGreaterThanConfiguredThreshold() throws KuraStoreException {
Expand Down Expand Up @@ -225,7 +246,7 @@ private void givenDataService() {
DataServiceOptions dataServiceOptions = new DataServiceOptions(Collections.emptyMap());
MessageStoreProvider messageStoreProviderMock = mock(MessageStoreProvider.class);
MessageStore messageStoreMock = mock(MessageStore.class);
CloudConnectionStatusService ccssMock = mock(CloudConnectionStatusService.class);
this.ccssMock = mock(CloudConnectionStatusService.class);
WatchdogService watchdogServiceMock = mock(WatchdogService.class);
initMockMessageStore(messageStoreProviderMock, messageStoreMock);
TestUtil.setFieldValue(this.dataServiceImpl, "dataServiceOptions", dataServiceOptions);
Expand All @@ -237,6 +258,11 @@ private void givenDataService() {
}

}

private void whenConfigurationIsChanged(final String key, final Object value) {
this.properties.put(key, value);
this.dataServiceImpl.updated(properties);
}

private void whenMessageIsPublished(final String topic, final byte[] payload, final int qos, final boolean retain,
final int priority) {
Expand Down Expand Up @@ -266,7 +292,19 @@ private void thenDataTrasportIsConnected() {
fail();
}
}

private void thenDataTrasportStaysConnected() {
try {
verify(this.dataTransportServiceMock, times(0)).connect();
} catch (KuraConnectException e) {
fail();
}
}

private void thenCloudConnectionStatusServiceIsNotChanged() {
verify(this.ccssMock, times(1)).updateStatus(any(CloudConnectionStatusComponent.class), eq(CloudConnectionStatusEnum.SLOW_BLINKING));
}

private void thenStartConnectionTaskIsInvoked() {
verify(this.dataServiceImpl, times(2)).startConnectionTask();
}
Expand Down
Loading