Skip to content

Commit

Permalink
Update the osgi-test version to 1.3.0-SNAPSHOT
Browse files Browse the repository at this point in the history
This upgrade improves the lifecycle of osgi-test considerably, particularly for configurations. It allows some test tidy-up, as well as preparing for more stable tests with the new OSGi Technology Jakarta REST implementation

Signed-off-by: Tim Ward <timothyjward@apache.org>
  • Loading branch information
timothyjward committed Jan 25, 2024
1 parent b36801f commit 0a4ee42
Show file tree
Hide file tree
Showing 37 changed files with 74 additions and 247 deletions.
6 changes: 3 additions & 3 deletions core/impl/integration-test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
org.opentest4j;version='[1.2.0,1.2.1)',\
org.osgi.service.component;version='[1.5.0,1.5.1)',\
org.osgi.service.typedevent;version='[1.0.0,1.0.1)',\
org.osgi.test.common;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5.cm;version='[1.2.1,1.2.2)',\
org.osgi.test.common;version='[1.3.0,1.3.1)',\
org.osgi.test.junit5;version='[1.3.0,1.3.1)',\
org.osgi.test.junit5.cm;version='[1.3.0,1.3.1)',\
org.osgi.util.converter;version='[1.0.9,1.0.10)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.3.0,1.3.1)',\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -37,21 +36,16 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.test.common.annotation.InjectBundleContext;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.common.annotation.Property;
import org.osgi.test.common.annotation.config.WithConfiguration;
import org.osgi.test.junit5.cm.ConfigurationExtension;
import org.osgi.test.junit5.context.BundleContextExtension;
import org.osgi.test.junit5.service.ServiceExtension;

/**
* Tests the metrics service
*/
@ExtendWith({ BundleContextExtension.class, ServiceExtension.class, ConfigurationExtension.class })
public class MetricsTest {

private static final String GAUGE_NAME = "test.gauge";
Expand All @@ -60,37 +54,20 @@ public class MetricsTest {
private static final String GAUGES_2 = "gauges-2";
private static final String[] GAUGES_NAMES = new String[] { GAUGES_1, GAUGES_2 };

/**
* Tested service
*/
@InjectService
IMetricsManager metrics;

/**
* Bundle context
*/
@InjectBundleContext
BundleContext context;

/**
* Blocking queue to wait for events
*/
BlockingQueue<BulkGenericDto> queue;
final BlockingQueue<BulkGenericDto> queue = new ArrayBlockingQueue<>(4);

/**
* Flag to indicate if a gauge callback has been called
*/
AtomicBoolean gaugeCalled;
final AtomicBoolean gaugeCalled = new AtomicBoolean();

/**
* Flags to indicate which gauge callback were called
*/
Map<String, Boolean> gaugesCalled;

/**
* List of active service registrations
*/
private final List<ServiceRegistration<?>> svcRegs = new ArrayList<>();
final Map<String, Boolean> gaugesCalled = new ConcurrentHashMap<>();

/**
* Test gauge class
Expand Down Expand Up @@ -128,46 +105,34 @@ public void onMetricsReport(BulkGenericDto dto) {
}
}

/**
* Registers a listener and gauges
*/
@BeforeEach
void setUp() throws Exception {
queue = new ArrayBlockingQueue<>(4);
gaugeCalled = new AtomicBoolean();
gaugesCalled = new HashMap<>();
void setUp(@InjectBundleContext BundleContext context) throws Exception {
context.registerService(IMetricsListener.class, new TestListener(), null);

registerServices();
}

@AfterEach
void cleanUp() {
for (ServiceRegistration<?> ref : svcRegs) {
ref.unregister();
}
svcRegs.clear();

metrics.clear();

queue = null;
gaugeCalled = null;
gaugesCalled = null;
TestGauge gauge = new TestGauge();
context.registerService(IMetricsGauge.class, gauge,
new Hashtable<>(Map.of(IMetricsGauge.NAME, GAUGE_NAME)));
context.registerService(IMetricsMultiGauge.class, gauge,
new Hashtable<>(Map.of(IMetricsMultiGauge.NAMES, GAUGES_NAMES)));
}

/**
* Registers a listener and gauges
* Registered services will be cleaned up by the {@link BundleContextExtension}.
* instance fields will be cleared out by the standard per-test instance lifecycle
* @param metrics
*/
private void registerServices() throws Exception {
svcRegs.add(context.registerService(IMetricsListener.class, new TestListener(), null));

TestGauge gauge = new TestGauge();
svcRegs.add(context.registerService(IMetricsGauge.class, gauge,
new Hashtable<>(Map.of(IMetricsGauge.NAME, GAUGE_NAME))));
svcRegs.add(context.registerService(IMetricsMultiGauge.class, gauge,
new Hashtable<>(Map.of(IMetricsMultiGauge.NAMES, GAUGES_NAMES))));
@AfterEach
void cleanUp(@InjectService IMetricsManager metrics) {
metrics.clear();
}

@Test
@WithConfiguration(pid = "sensinact.metrics", location = "?", properties = {
@Property(key = "enabled", value = "false"), @Property(key = "metrics.rate", value = "1") })
void testDisabled() throws Exception {
void testDisabled(@InjectService(filter = "(enabled=false)") IMetricsManager metrics) throws Exception {
// At this point, listener and gauges should be registered
metrics.getCounter("toto").inc();

Expand All @@ -179,7 +144,7 @@ void testDisabled() throws Exception {
@Test
@WithConfiguration(pid = "sensinact.metrics", location = "?", properties = {
@Property(key = "enabled", value = "true"), @Property(key = "metrics.rate", value = "1") })
void testEnabled() throws Exception {
void testEnabled(@InjectService(filter = "(enabled=true)") IMetricsManager metrics) throws Exception {
metrics.getCounter("toto").inc();

// Wait for a bit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@
import org.eclipse.sensinact.model.core.provider.ProviderPackage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.junit5.service.ServiceExtension;

@ExtendWith(ServiceExtension.class)
public class SubscribeTest {

private static final UserInfo USER = UserInfo.ANONYMOUS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.junit5.service.ServiceExtension;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Tests the behavior around the admin service
*/
@ExtendWith(ServiceExtension.class)
public class AdminServiceTest {

private static final UserInfo USER = UserInfo.ANONYMOUS;
Expand All @@ -59,7 +56,7 @@ void start() {

@AfterEach
void stop() {
session = null;
session.expire();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions northbound/filters/ldap/integration-test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
org.opentest4j;version='[1.2.0,1.2.1)',\
org.osgi.service.component;version='[1.5.0,1.5.1)',\
org.osgi.service.typedevent;version='[1.0.0,1.0.1)',\
org.osgi.test.common;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5;version='[1.2.1,1.2.2)',\
org.osgi.test.common;version='[1.3.0,1.3.1)',\
org.osgi.test.junit5;version='[1.3.0,1.3.1)',\
org.osgi.util.converter;version='[1.0.9,1.0.10)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.3.0,1.3.1)',\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
import org.eclipse.sensinact.northbound.filters.api.IFilterHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.junit5.service.ServiceExtension;

@ExtendWith(ServiceExtension.class)
public class LdapComponentTest {

@InjectService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.sensinact.northbound.filters.ldap.LdapParserTest;
import org.eclipse.sensinact.core.command.AbstractTwinCommand;
import org.eclipse.sensinact.core.command.GatewayThread;
import org.eclipse.sensinact.core.push.DataUpdate;
Expand All @@ -31,15 +30,13 @@
import org.eclipse.sensinact.core.snapshot.ProviderSnapshot;
import org.eclipse.sensinact.core.snapshot.ResourceValueFilter;
import org.eclipse.sensinact.core.twin.SensinactDigitalTwin;
import org.eclipse.sensinact.northbound.filters.ldap.LdapParserTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.junit5.service.ServiceExtension;
import org.osgi.util.promise.Promise;
import org.osgi.util.promise.PromiseFactory;

@ExtendWith(ServiceExtension.class)
public class LdapFilterTest {

@InjectService
Expand Down
4 changes: 2 additions & 2 deletions northbound/query-handler/integration-test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
org.opentest4j;version='[1.2.0,1.2.1)',\
org.osgi.service.component;version='[1.5.0,1.5.1)',\
org.osgi.service.typedevent;version='[1.0.0,1.0.1)',\
org.osgi.test.common;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5;version='[1.2.1,1.2.2)',\
org.osgi.test.common;version='[1.3.0,1.3.1)',\
org.osgi.test.junit5;version='[1.3.0,1.3.1)',\
org.osgi.util.converter;version='[1.0.9,1.0.10)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.3.0,1.3.1)',\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@
import org.eclipse.sensinact.northbound.query.dto.result.ResultListResourcesDTO;
import org.eclipse.sensinact.northbound.query.dto.result.ResultListServicesDTO;
import org.eclipse.sensinact.northbound.query.dto.result.TypedResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.junit5.service.ServiceExtension;

@ExtendWith(ServiceExtension.class)
public class DescriptionsTest {

private static final UserInfo USER = UserInfo.ANONYMOUS;
Expand All @@ -58,8 +54,6 @@ public class DescriptionsTest {
private static final String RESOURCE = "resource";
private static final Integer VALUE = 42;

@InjectService
SensiNactSessionManager sessionManager;
SensiNactSession session;

@InjectService
Expand All @@ -71,15 +65,10 @@ public class DescriptionsTest {
final TestUtils utils = new TestUtils();

@BeforeEach
void start() {
void start(@InjectService SensiNactSessionManager sessionManager) {
session = sessionManager.getDefaultSession(USER);
}

@AfterEach
void stop() {
session = null;
}

/**
* Complete system description
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@
import org.eclipse.sensinact.northbound.query.dto.result.ResultListResourcesDTO;
import org.eclipse.sensinact.northbound.query.dto.result.ResultListServicesDTO;
import org.eclipse.sensinact.northbound.query.dto.result.TypedResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.junit5.service.ServiceExtension;

@ExtendWith(ServiceExtension.class)
public class MissingEntityTest {

private static final UserInfo USER = UserInfo.ANONYMOUS;
Expand All @@ -59,8 +55,6 @@ public class MissingEntityTest {
private static final String RESOURCE = "resource";
private static final Integer VALUE = 42;

@InjectService
SensiNactSessionManager sessionManager;
SensiNactSession session;

@InjectService
Expand All @@ -72,15 +66,10 @@ public class MissingEntityTest {
final TestUtils utils = new TestUtils();

@BeforeEach
void start() throws InterruptedException {
void start(@InjectService SensiNactSessionManager sessionManager) throws InterruptedException {
session = sessionManager.getDefaultSession(USER);
}

@AfterEach
void stop() {
session = null;
}

/**
* Missing provider should return a 404
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public class ResourceAccessTest {
private static final Integer VALUE = 42;
private static final Integer VALUE_2 = 84;

@InjectService
SensiNactSessionManager sessionManager;
SensiNactSession session;

@InjectService
Expand All @@ -81,17 +79,14 @@ public class ResourceAccessTest {
final TestUtils utils = new TestUtils();

@BeforeEach
void start() throws Exception {
void start(@InjectService SensiNactSessionManager sessionManager) throws Exception {
session = sessionManager.getDefaultSession(USER);
}

@AfterEach
void stop() {
if (queue != null) {
session.activeListeners().keySet().forEach(session::removeListener);
queue = null;
}
session = null;
session.activeListeners().keySet().forEach(session::removeListener);
session.expire();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions northbound/rest/integration-test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
org.osgi.service.component;version='[1.5.0,1.5.1)',\
org.osgi.service.jakartars;version='[2.0.0,2.0.1)',\
org.osgi.service.typedevent;version='[1.0.0,1.0.1)',\
org.osgi.test.common;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5.cm;version='[1.2.1,1.2.2)',\
org.osgi.test.common;version='[1.3.0,1.3.1)',\
org.osgi.test.junit5;version='[1.3.0,1.3.1)',\
org.osgi.test.junit5.cm;version='[1.3.0,1.3.1)',\
org.osgi.util.converter;version='[1.0.9,1.0.10)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.3.0,1.3.1)',\
Expand Down

0 comments on commit 0a4ee42

Please sign in to comment.