Feature/handle amqp fatal errors (#1111)

* Adding support to handle lengthy error msgs more precisely

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* Added check at conditionalHandler level and changes assertions in test class

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* Fixed sonar lint issues

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* Reverted the change on making class final

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* To trigger the circle-ci build and check

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* Addressed last set of PR comments

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* Fixe sonar issue for nullpointer dereference

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>

* Handling null case explicitly

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>
This commit is contained in:
Shruthi Manavalli Ramanna
2021-07-20 09:59:10 +02:00
committed by GitHub
parent a8f7f50cf9
commit c37c615ea6
11 changed files with 472 additions and 12 deletions

View File

@@ -0,0 +1,86 @@
/**
* Copyright (c) 2021 Bosch.IO GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.amqp;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.junit.jupiter.api.Test;
import org.springframework.util.ErrorHandler;
import java.util.ArrayList;
import java.util.List;
@Feature("Unit Tests - Delegating Conditional Error Handler")
@Story("Delegating Conditional Error Handler")
public class DelegatingAmqpErrorHandlerTest {
@Test
@Description("Verifies that with a list of conditional error handlers, the error is delegated to specific handler.")
public void verifyDelegationHandling(){
List<AmqpErrorHandler> handlers = new ArrayList<>();
handlers.add(new IllegalArgumentExceptionHandler());
handlers.add(new IndexOutOfBoundsExceptionHandler());
assertThatExceptionOfType(IllegalArgumentException.class)
.as("Expected handled exception to be of type IllegalArgumentException")
.isThrownBy(() -> new DelegatingConditionalErrorHandler(handlers, new DefaultErrorHandler())
.handleError(new Throwable(new IllegalArgumentException())));
}
@Test
@Description("Verifies that default handler is used if no handlers are defined for the specific exception.")
public void verifyDefaultDelegationHandling(){
List<AmqpErrorHandler> handlers = new ArrayList<>();
handlers.add(new IllegalArgumentExceptionHandler());
handlers.add(new IndexOutOfBoundsExceptionHandler());
assertThatExceptionOfType(RuntimeException.class)
.as("Expected handled exception to be of type RuntimeException")
.isThrownBy(() -> new DelegatingConditionalErrorHandler(handlers, new DefaultErrorHandler())
.handleError(new Throwable(new NullPointerException())));
}
// Test class
public class IllegalArgumentExceptionHandler implements AmqpErrorHandler {
@Override
public void doHandle(final Throwable t, final AmqpErrorHandlerChain chain) {
if (t.getCause() instanceof IllegalArgumentException) {
throw new IllegalArgumentException(t.getMessage());
} else {
chain.handle(t);
}
}
}
// Test class
public class IndexOutOfBoundsExceptionHandler implements AmqpErrorHandler {
@Override
public void doHandle(final Throwable t, final AmqpErrorHandlerChain chain) {
if (t.getCause() instanceof IndexOutOfBoundsException) {
throw new IndexOutOfBoundsException(t.getMessage());
} else {
chain.handle(t);
}
}
}
// Test class
public class DefaultErrorHandler implements ErrorHandler {
@Override
public void
handleError(Throwable t) {
throw new RuntimeException(t);
}
}
}