From 4de34eacc36b119ad43437ada778765cba21617d Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Fri, 22 Nov 2024 17:36:07 +0200 Subject: [PATCH] Fix AmqpMessageDispatcherServiceTest.testSendCancelRequest - set action tenant (#2098) Signed-off-by: Avgustin Marinov --- .../EventPublisherAutoConfiguration.java | 3 +- .../eclipse/hawkbit/amqp/BaseAmqpService.java | 2 +- .../amqp/DelayedRequeueExceptionStrategy.java | 2 +- .../MessageConversionExceptionHandler.java | 3 +- .../AmqpMessageDispatcherServiceTest.java | 1 + .../remote/CancelTargetAssignmentEvent.java | 1 - .../remote/entity/RemoteEntityEvent.java | 3 +- .../event/BusProtoStuffMessageConverter.java | 30 ++++++------------ .../org/eclipse/hawkbit/event/EventType.java | 16 +++++----- .../jpa/event/JpaEventEntityManager.java | 6 ++-- .../security/SecurityContextTenantAware.java | 5 ++- .../security/SystemSecurityContext.java | 31 +++++++++---------- 12 files changed, 42 insertions(+), 61 deletions(-) diff --git a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/event/EventPublisherAutoConfiguration.java b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/event/EventPublisherAutoConfiguration.java index e25086048..909976e15 100644 --- a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/event/EventPublisherAutoConfiguration.java +++ b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/event/EventPublisherAutoConfiguration.java @@ -72,8 +72,7 @@ public class EventPublisherAutoConfiguration { } /** - * @return default {@link ApplicationEventFilter} that does not filter any - * events + * @return default {@link ApplicationEventFilter} that does not filter any events */ @Bean @ConditionalOnMissingBean diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java index a4624261e..7daa10d71 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java @@ -66,7 +66,7 @@ public class BaseAmqpService { return ObjectUtils.isEmpty(message.getBody()); } - protected static final void logAndThrowMessageError(final Message message, final String error) { + protected static void logAndThrowMessageError(final Message message, final String error) { log.debug("Warning! \"{}\" reported by message: {}", error, message); throw new AmqpRejectAndDontRequeueException(error); } diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DelayedRequeueExceptionStrategy.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DelayedRequeueExceptionStrategy.java index 814a5a608..8a5524c30 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DelayedRequeueExceptionStrategy.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DelayedRequeueExceptionStrategy.java @@ -90,4 +90,4 @@ public class DelayedRequeueExceptionStrategy extends ConditionalRejectingErrorHa cause instanceof MessageConversionException || cause instanceof MessageHandlingException; } -} +} \ No newline at end of file diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/MessageConversionExceptionHandler.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/MessageConversionExceptionHandler.java index 6c05dd3a6..e126b28c5 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/MessageConversionExceptionHandler.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/MessageConversionExceptionHandler.java @@ -27,8 +27,7 @@ public class MessageConversionExceptionHandler extends AbstractAmqpErrorHandler< @Override public String getErrorMessage(Throwable throwable) { final String errorMessage = super.getErrorMessage(throwable); - //since the detailed error message lies in the first parent of current throwable we retrieve it - // and append it to the errorMessage + // since the detailed error message lies in the first parent of current throwable we retrieve it and append it to the errorMessage final Optional detailedErrorMessage = getFirstAncestralErrorMessage(throwable.getCause()); return detailedErrorMessage.isPresent() ? (detailedErrorMessage.get() + errorMessage) : errorMessage; } diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java index ba7141069..4a9822fa9 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java @@ -224,6 +224,7 @@ class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest { void testSendCancelRequest() { final Action action = mock(Action.class); when(action.getId()).thenReturn(1L); + when(action.getTenant()).thenReturn(TENANT); when(action.getTarget()).thenReturn(testTarget); final CancelTargetAssignmentEvent cancelTargetAssignmentDistributionSetEvent = new CancelTargetAssignmentEvent( action, serviceMatcher.getBusId()); diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/CancelTargetAssignmentEvent.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/CancelTargetAssignmentEvent.java index 701c31ef3..f9ee58b77 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/CancelTargetAssignmentEvent.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/CancelTargetAssignmentEvent.java @@ -31,6 +31,5 @@ public class CancelTargetAssignmentEvent extends AbstractAssignmentEvent { public CancelTargetAssignmentEvent(final String tenant, final List a, final String applicationId) { super(applicationId, tenant, a, applicationId); - } } \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/entity/RemoteEntityEvent.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/entity/RemoteEntityEvent.java index bab42df55..584fbde44 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/entity/RemoteEntityEvent.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/entity/RemoteEntityEvent.java @@ -57,8 +57,7 @@ public class RemoteEntityEvent extends RemoteId private E reloadEntityFromRepository() { try { final Class clazz = (Class) Class.forName(getEntityClass()); - return EventEntityManagerHolder.getInstance().getEventEntityManager().findEntity( - getTenant(), getEntityId(), clazz); + return EventEntityManagerHolder.getInstance().getEventEntityManager().findEntity(getTenant(), getEntityId(), clazz); } catch (final ClassNotFoundException e) { log.error("Cannot reload entity because class is not found", e); } diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/BusProtoStuffMessageConverter.java b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/BusProtoStuffMessageConverter.java index 364285acd..248aa5e72 100644 --- a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/BusProtoStuffMessageConverter.java +++ b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/BusProtoStuffMessageConverter.java @@ -22,24 +22,19 @@ import org.springframework.messaging.converter.MessageConversionException; import org.springframework.util.MimeType; /** - * A customize message converter for the spring cloud events. The converter is - * registered for the application/binary+protostuff type. - * - * The clazz-type-information is encoded into the message payload infront with a - * length of {@link #EVENT_TYPE_LENGTH}. This is necessary due in case of - * rabbitMQ batching the message headers will be merged together and custom - * message header information will get lost. So in this implementation the - * information about the event-type is encoded in the payload of the message - * directly using the encoded values of {@link EventType}. + * A customize message converter for the spring cloud events. The converter is registered for the application/binary+protostuff type. + *

+ * The clazz-type-information is encoded into the message payload infront with a length of {@link #EVENT_TYPE_LENGTH}. This is necessary + * due in case of rabbitMQ batching the message headers will be merged together and custom message header information will get lost. + * So in this implementation the information about the event-type is encoded in the payload of the message directly using the encoded + * values of {@link EventType}. */ @Slf4j public class BusProtoStuffMessageConverter extends AbstractMessageConverter { public static final MimeType APPLICATION_BINARY_PROTOSTUFF = new MimeType("application", "binary+protostuff"); - /** - * The length of the class type length of the payload. - */ + /** The length of the class type length of the payload. */ private static final byte EVENT_TYPE_LENGTH = 2; public BusProtoStuffMessageConverter() { @@ -52,8 +47,7 @@ public class BusProtoStuffMessageConverter extends AbstractMessageConverter { } @Override - public Object convertFromInternal(final Message message, final Class targetClass, - final Object conversionHint) { + public Object convertFromInternal(final Message message, final Class targetClass, final Object conversionHint) { final Object objectPayload = message.getPayload(); if (objectPayload instanceof byte[] payload) { final byte[] clazzHeader = extractClazzHeader(payload); @@ -66,13 +60,9 @@ public class BusProtoStuffMessageConverter extends AbstractMessageConverter { } @Override - protected Object convertToInternal(final Object payload, final MessageHeaders headers, - final Object conversionHint) { - + protected Object convertToInternal(final Object payload, final MessageHeaders headers, final Object conversionHint) { final byte[] clazzHeader = writeClassHeader(payload.getClass()); - final byte[] writeContent = writeContent(payload); - return mergeClassHeaderAndContent(clazzHeader, writeContent); } @@ -132,4 +122,4 @@ public class BusProtoStuffMessageConverter extends AbstractMessageConverter { final LinkedBuffer buffer = LinkedBuffer.allocate(); return ProtobufIOUtil.toByteArray(clazzEventType, schema, buffer); } -} +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/EventType.java b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/EventType.java index 869d291f9..cb0b5a5c5 100644 --- a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/EventType.java +++ b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/EventType.java @@ -86,10 +86,8 @@ public class EventType { private int value; - /** - * The associated event-type-value must remain the same as initially - * declared. Otherwise, messages cannot correctly de-serialized. - */ + // The associated event-type-value must remain the same as initially + // declared. Otherwise, messages cannot correctly de-serialized. static { // target TYPES.put(1, TargetCreatedEvent.class); @@ -190,10 +188,12 @@ public class EventType { * does not have a {@link EventType}. */ public static EventType from(final Class clazz) { - final Optional foundEventType = TYPES.entrySet().stream() - .filter(entry -> entry.getValue().equals(clazz)).map(Entry::getKey).findAny(); - - return foundEventType.map(EventType::new).orElse(null); + return TYPES.entrySet().stream() + .filter(entry -> entry.getValue().equals(clazz)) + .map(Entry::getKey) + .findAny() + .map(EventType::new) + .orElse(null); } public Class getTargetClass() { diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/event/JpaEventEntityManager.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/event/JpaEventEntityManager.java index 5069d0c0c..bdd11273a 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/event/JpaEventEntityManager.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/event/JpaEventEntityManager.java @@ -17,8 +17,7 @@ import org.eclipse.hawkbit.tenancy.TenantAware; import org.springframework.transaction.annotation.Transactional; /** - * A TenantAwareEvent entity manager, which loads an entity by id and type for - * remote events. + * A TenantAwareEvent entity manager, which loads an entity by id and type for remote events. */ @Transactional(readOnly = true) public class JpaEventEntityManager implements EventEntityManager { @@ -38,8 +37,7 @@ public class JpaEventEntityManager implements EventEntityManager { } @Override - public E findEntity(final String tenant, final Long id, - final Class entityType) { + public E findEntity(final String tenant, final Long id, final Class entityType) { return tenantAware.runAsTenant(tenant, () -> entityManager.find(entityType, id)); } } \ No newline at end of file diff --git a/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SecurityContextTenantAware.java b/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SecurityContextTenantAware.java index 7ab628e37..91f1780d4 100644 --- a/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SecurityContextTenantAware.java +++ b/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SecurityContextTenantAware.java @@ -165,9 +165,8 @@ public class SecurityContextTenantAware implements ContextAware { } /** - * An {@link Authentication} implementation to delegate to an existing - * {@link Authentication} object except setting the details specifically for - * a specific tenant and user. + * An {@link Authentication} implementation to delegate to an existing {@link Authentication} object except setting the details + * specifically for a specific tenant and user. */ private static final class AuthenticationDelegate implements Authentication { diff --git a/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SystemSecurityContext.java b/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SystemSecurityContext.java index 01d144564..f32b0b55b 100644 --- a/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SystemSecurityContext.java +++ b/hawkbit-security/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/security/SystemSecurityContext.java @@ -65,14 +65,14 @@ public class SystemSecurityContext { /** * Runs a given {@link Callable} within a system security context, which is * permitted to call secured system code. Often the system needs to call - * secured methods by it's own without relying on the current security + * secured methods by its own without relying on the current security * context e.g. if the current security context does not contain the * necessary permission it's necessary to execute code as system code to * execute necessary methods and functionality. - * + *
* The security context will be switched to the system code and back after * the callable is called. - * + *
* The system code is executed for a current tenant by using the * {@link TenantAware#getCurrentTenant()}. * @@ -88,7 +88,7 @@ public class SystemSecurityContext { /** * Runs a given {@link Callable} within a system security context, which is * permitted to call secured system code. Often the system needs to call - * secured methods by it's own without relying on the current security + * secured methods by its own without relying on the current security * context e.g. if the current security context does not contain the * necessary permission it's necessary to execute code as system code to * execute necessary methods and functionality. @@ -120,12 +120,10 @@ public class SystemSecurityContext { } /** - * Runs a given {@link Callable} within a system security context, which has - * the provided {@link GrantedAuthority}s to successfully run the - * {@link Callable}. - * - * The security context will be switched to the a new - * {@link SecurityContext} and back after the callable is called. + * Runs a given {@link Callable} within a system security context, which has the provided {@link GrantedAuthority}s to successfully + * run the {@link Callable}. + *
+ * The security context will be switched to a new {@link SecurityContext} and back after the callable is called. * * @param tenant under which the {@link Callable#call()} must be executed. * @param callable to call within the security context @@ -193,18 +191,17 @@ public class SystemSecurityContext { } /** - * An implementation of the Spring's {@link Authentication} object which is - * used within a system security code block and wraps the original - * authentication object. The wrapped object contains the necessary - * {@link SpringEvalExpressions#SYSTEM_ROLE} which is allowed to execute all - * secured methods. + * An implementation of the Spring's {@link Authentication} object which is used within a system security code block and + * wraps the original authentication object. The wrapped object contains the necessary {@link SpringEvalExpressions#SYSTEM_ROLE} + * which is allowed to execute all secured methods. */ public static final class SystemCodeAuthentication implements Authentication { @Serial private static final long serialVersionUID = 1L; - private static final List AUTHORITIES = Collections - .singletonList(new SimpleGrantedAuthority(SpringEvalExpressions.SYSTEM_ROLE)); + + private static final List AUTHORITIES = + Collections.singletonList(new SimpleGrantedAuthority(SpringEvalExpressions.SYSTEM_ROLE)); private final Authentication oldAuthentication; private SystemCodeAuthentication(final Authentication oldAuthentication) {