Improve Spring Bus usage (remove stream direct use) (#2521)

* Improve Spring Bus usage (remove stream direct use)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>

* Remove getApplicaton when creating remote events

---------

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-07-02 08:32:55 +03:00
committed by GitHub
parent 65c81a3e90
commit affae1026a
104 changed files with 327 additions and 837 deletions

View File

@@ -30,7 +30,6 @@ class RemoteIdEventTest extends AbstractRemoteEventTest {
private static final long ENTITY_ID = 1L;
private static final String TENANT = "tenant";
private static final Class<? extends TenantAwareBaseEntity> ENTITY_CLASS = JpaAction.class;
private static final String NODE = "Node";
private static final String CONTROLLER_ID = "controller911";
private static final String ADDRESS = "amqp://anyhost";
@@ -55,8 +54,7 @@ class RemoteIdEventTest extends AbstractRemoteEventTest {
*/
@Test
void testTargetDeletedEvent() {
final TargetDeletedEvent deletedEvent = new TargetDeletedEvent(TENANT, ENTITY_ID, CONTROLLER_ID, ADDRESS,
ENTITY_CLASS, NODE);
final TargetDeletedEvent deletedEvent = new TargetDeletedEvent(TENANT, ENTITY_ID, ENTITY_CLASS, CONTROLLER_ID, ADDRESS);
assertEntity(deletedEvent);
}
@@ -85,13 +83,12 @@ class RemoteIdEventTest extends AbstractRemoteEventTest {
}
protected void assertAndCreateRemoteEvent(final Class<? extends RemoteIdEvent> eventType) {
final Constructor<?> constructor = Arrays.stream(eventType.getDeclaredConstructors())
.filter(con -> con.getParameterCount() == 4).findAny()
.filter(con -> con.getParameterCount() == 3).findAny()
.orElseThrow(() -> new IllegalArgumentException("Given event is not RemoteIdEvent compatible"));
try {
final RemoteIdEvent event = (RemoteIdEvent) constructor.newInstance(TENANT, ENTITY_ID, ENTITY_CLASS, NODE);
final RemoteIdEvent event = (RemoteIdEvent) constructor.newInstance(TENANT, ENTITY_ID, ENTITY_CLASS);
assertEntity(event);
} catch (final ReflectiveOperationException e) {
fail("Exception should not happen " + e.getMessage());

View File

@@ -28,7 +28,6 @@ import org.junit.jupiter.api.Test;
class RemoteTenantAwareEventTest extends AbstractRemoteEventTest {
private static final String TENANT_DEFAULT = "DEFAULT";
private static final String APPLICATION_ID_DEFAULT = "Node";
/**
* Verifies that a testMultiActionAssignEvent can be properly serialized and deserialized
@@ -38,8 +37,7 @@ class RemoteTenantAwareEventTest extends AbstractRemoteEventTest {
final List<String> controllerIds = List.of("id0", "id1", "id2", "id3", "id4loooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnng");
final List<Action> actions = controllerIds.stream().map(this::createAction).toList();
final MultiActionAssignEvent assignEvent = new MultiActionAssignEvent(TENANT_DEFAULT, APPLICATION_ID_DEFAULT,
actions);
final MultiActionAssignEvent assignEvent = new MultiActionAssignEvent(TENANT_DEFAULT, actions);
final MultiActionAssignEvent remoteAssignEventProtoStuff = createProtoStuffEvent(assignEvent);
assertThat(assignEvent).isEqualTo(remoteAssignEventProtoStuff);
@@ -58,8 +56,7 @@ class RemoteTenantAwareEventTest extends AbstractRemoteEventTest {
final List<String> controllerIds = List.of("id0", "id1", "id2", "id3", "id4loooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnng");
final List<Action> actions = controllerIds.stream().map(this::createAction).toList();
final MultiActionCancelEvent cancelEvent = new MultiActionCancelEvent(TENANT_DEFAULT, APPLICATION_ID_DEFAULT,
actions);
final MultiActionCancelEvent cancelEvent = new MultiActionCancelEvent(TENANT_DEFAULT, actions);
final MultiActionCancelEvent remoteCancelEventProtoStuff = createProtoStuffEvent(cancelEvent);
assertThat(cancelEvent).isEqualTo(remoteCancelEventProtoStuff);
@@ -75,8 +72,7 @@ class RemoteTenantAwareEventTest extends AbstractRemoteEventTest {
*/
@Test
void reloadDownloadProgressByRemoteEvent() {
final DownloadProgressEvent downloadProgressEvent = new DownloadProgressEvent(TENANT_DEFAULT, 1L, 3L,
APPLICATION_ID_DEFAULT);
final DownloadProgressEvent downloadProgressEvent = new DownloadProgressEvent(TENANT_DEFAULT, 1L, 3L);
final DownloadProgressEvent remoteEventProtoStuff = createProtoStuffEvent(downloadProgressEvent);
assertThat(downloadProgressEvent).isEqualTo(remoteEventProtoStuff);
@@ -104,8 +100,7 @@ class RemoteTenantAwareEventTest extends AbstractRemoteEventTest {
final Action action = actionRepository.save(generateAction);
final TargetAssignDistributionSetEvent assignmentEvent = new TargetAssignDistributionSetEvent(
action.getTenant(), dsA.getId(), List.of(action), serviceMatcher.getBusId(),
action.isMaintenanceWindowAvailable());
action.getTenant(), dsA.getId(), List.of(action), action.isMaintenanceWindowAvailable());
final TargetAssignDistributionSetEvent remoteEventProtoStuff = createProtoStuffEvent(assignmentEvent);
assertTargetAssignDistributionSetEvent(action, remoteEventProtoStuff);
@@ -132,8 +127,7 @@ class RemoteTenantAwareEventTest extends AbstractRemoteEventTest {
final Action action = actionRepository.save(generateAction);
final CancelTargetAssignmentEvent cancelEvent = new CancelTargetAssignmentEvent(action,
serviceMatcher.getBusId());
final CancelTargetAssignmentEvent cancelEvent = new CancelTargetAssignmentEvent(action);
final CancelTargetAssignmentEvent remoteEventProtoStuff = createProtoStuffEvent(cancelEvent);
assertCancelTargetAssignmentEvent(action, remoteEventProtoStuff);

View File

@@ -32,9 +32,7 @@ public abstract class AbstractRemoteEntityEventTest<E> extends AbstractRemoteEve
return event;
}
protected RemoteEntityEvent<?> createRemoteEvent(final E baseEntity,
final Class<? extends RemoteEntityEvent<?>> eventType) {
protected RemoteEntityEvent<?> createRemoteEvent(final E baseEntity, final Class<? extends RemoteEntityEvent<?>> eventType) {
final int constructorParamCount = getConstructorParamCount();
final Constructor<?> eventConstructor = findEventConstructorByParamCount(eventType, constructorParamCount);
final Object[] eventConstructorParams = getConstructorParams(baseEntity);
@@ -47,18 +45,17 @@ public abstract class AbstractRemoteEntityEventTest<E> extends AbstractRemoteEve
}
protected int getConstructorParamCount() {
return 2;
return 1;
}
protected Constructor<?> findEventConstructorByParamCount(final Class<? extends RemoteEntityEvent<?>> eventType,
final int paramCount) {
protected Constructor<?> findEventConstructorByParamCount(final Class<? extends RemoteEntityEvent<?>> eventType, final int paramCount) {
return Arrays.stream(eventType.getDeclaredConstructors())
.filter(constructor -> constructor.getParameterCount() == paramCount).findAny()
.orElseThrow(() -> new IllegalArgumentException("No suitable constructor founded"));
.orElseThrow(() -> new IllegalArgumentException("No suitable constructor found"));
}
protected Object[] getConstructorParams(final E baseEntity) {
return new Object[] { baseEntity, "Node" };
return new Object[] { baseEntity };
}
protected RemoteEntityEvent<?> assertEntity(final E baseEntity, final RemoteEntityEvent<?> event) {

View File

@@ -45,12 +45,12 @@ class ActionEventTest extends AbstractRemoteEntityEventTest<Action> {
@Override
protected int getConstructorParamCount() {
return 5;
return 4;
}
@Override
protected Object[] getConstructorParams(final Action baseEntity) {
return new Object[] { baseEntity, 1L, 1L, 2L, "Node" };
return new Object[] { baseEntity, 1L, 1L, 2L };
}
@Override

View File

@@ -40,5 +40,4 @@ class DistributionSetTagEventTest extends AbstractRemoteEntityEventTest<Distribu
protected DistributionSetTag createEntity() {
return distributionSetTagManagement.create(entityFactory.tag().create().name("tag1"));
}
}
}

View File

@@ -29,10 +29,8 @@ class DistributionSetUpdatedEventTest extends AbstractRemoteEntityEventTest<Dist
}
@Override
protected RemoteEntityEvent<?> createRemoteEvent(final DistributionSet baseEntity,
final Class<? extends RemoteEntityEvent<?>> eventType) {
return new DistributionSetUpdatedEvent(baseEntity, "1", true);
protected RemoteEntityEvent<?> createRemoteEvent(final DistributionSet baseEntity, final Class<? extends RemoteEntityEvent<?>> eventType) {
return new DistributionSetUpdatedEvent(baseEntity, true);
}
@Override
@@ -40,5 +38,4 @@ class DistributionSetUpdatedEventTest extends AbstractRemoteEntityEventTest<Dist
return distributionSetManagement.create(entityFactory.distributionSet().create()
.name("incomplete").version("2").description("incomplete").type("os"));
}
}
}

View File

@@ -35,8 +35,7 @@ class RolloutGroupEventTest extends AbstractRemoteEntityEventTest<RolloutGroup>
*/
@Test
void testRolloutGroupCreatedEvent() {
final RolloutGroupCreatedEvent createdEvent = (RolloutGroupCreatedEvent) assertAndCreateRemoteEvent(
RolloutGroupCreatedEvent.class);
final RolloutGroupCreatedEvent createdEvent = (RolloutGroupCreatedEvent) assertAndCreateRemoteEvent(RolloutGroupCreatedEvent.class);
assertThat(createdEvent.getRolloutId()).isNotNull();
}
@@ -50,12 +49,12 @@ class RolloutGroupEventTest extends AbstractRemoteEntityEventTest<RolloutGroup>
@Override
protected int getConstructorParamCount() {
return 3;
return 2;
}
@Override
protected Object[] getConstructorParams(final RolloutGroup baseEntity) {
return new Object[] { baseEntity, 1L, "Node" };
return new Object[] { baseEntity, 1L };
}
@Override

View File

@@ -66,8 +66,6 @@ import org.eclipse.hawkbit.repository.test.util.SecurityContextSwitch;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.test.context.ContextConfiguration;
@@ -76,7 +74,6 @@ import org.springframework.transaction.annotation.Transactional;
@Slf4j
@ContextConfiguration(classes = { RepositoryApplicationConfiguration.class, TestConfiguration.class })
@Import(TestChannelBinderConfiguration.class)
@TestPropertySource(locations = "classpath:/jpa-test.properties")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest {

View File

@@ -25,8 +25,6 @@ import org.eclipse.hawkbit.repository.test.TestConfiguration;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
@@ -37,7 +35,6 @@ import org.springframework.test.context.ContextConfiguration;
"spring.main.banner-mode=off",
"logging.level.root=ERROR" })
@ContextConfiguration(classes = { RepositoryApplicationConfiguration.class, TestConfiguration.class })
@Import(TestChannelBinderConfiguration.class)
@Disabled("For manual run only, while playing around with RSQL to SQL")
@SuppressWarnings("java:S2699") // java:S2699 - manual test, don't actually does assertions
class RSQLToSQLTest {