Skip to content

Commit e0d4813

Browse files
authored
Merge pull request #1254 from OSGP/feature/SMHE-1699_error_handling_with_protocol_exception_as_cause
SMHE-1699 Improve error handling if a FunctionalException has causing…
2 parents 623ccf2 + f4b625e commit e0d4813

File tree

2 files changed

+155
-3
lines changed

2 files changed

+155
-3
lines changed

osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/exceptions/OsgpExceptionConverter.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ public class OsgpExceptionConverter {
3434
public OsgpException ensureOsgpOrTechnicalException(final Exception e) {
3535

3636
final boolean osgpExceptionSupportedByShared =
37-
!(e instanceof ImageTransferException || e instanceof ProtocolAdapterException);
37+
!(e instanceof ImageTransferException
38+
|| e instanceof ProtocolAdapterException
39+
|| (e instanceof FunctionalException
40+
&& e.getCause() instanceof ProtocolAdapterException));
3841

39-
if (e instanceof OsgpException && osgpExceptionSupportedByShared) {
40-
return (OsgpException) e;
42+
if (e instanceof final OsgpException osgpException && osgpExceptionSupportedByShared) {
43+
return osgpException;
4144
}
4245

4346
if (e instanceof ConnectionException) {
@@ -54,6 +57,15 @@ public OsgpException ensureOsgpOrTechnicalException(final Exception e) {
5457
new OsgpException(ComponentType.PROTOCOL_DLMS, e.getMessage()));
5558
}
5659

60+
if (e instanceof final FunctionalException functionalException
61+
&& e.getCause() instanceof final ProtocolAdapterException protocolAdapterException) {
62+
return new FunctionalException(
63+
functionalException.getExceptionType(),
64+
functionalException.getComponentType(),
65+
new OsgpException(
66+
protocolAdapterException.getComponentType(), protocolAdapterException.getMessage()));
67+
}
68+
5769
return new TechnicalException(
5870
ComponentType.PROTOCOL_DLMS,
5971
"Unexpected exception while handling protocol request/response message",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright Contributors to the GXF project
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package org.opensmartgridplatform.adapter.protocol.dlms.exceptions;
8+
9+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.mockito.InjectMocks;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
import org.opensmartgridplatform.shared.exceptionhandling.ComponentType;
17+
import org.opensmartgridplatform.shared.exceptionhandling.FunctionalException;
18+
import org.opensmartgridplatform.shared.exceptionhandling.FunctionalExceptionType;
19+
import org.opensmartgridplatform.shared.exceptionhandling.OsgpException;
20+
import org.opensmartgridplatform.shared.exceptionhandling.TechnicalException;
21+
22+
@ExtendWith(MockitoExtension.class)
23+
class OsgpExceptionConverterTest {
24+
25+
@InjectMocks private OsgpExceptionConverter osgpExceptionConverter;
26+
27+
@Test
28+
void testException() {
29+
final Exception exception = new Exception("Test Exception");
30+
31+
final TechnicalException expectedException =
32+
new TechnicalException(
33+
ComponentType.PROTOCOL_DLMS,
34+
"Unexpected exception while handling protocol request/response message",
35+
new OsgpException(ComponentType.PROTOCOL_DLMS, exception.getMessage()));
36+
37+
final OsgpException result =
38+
this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
39+
40+
this.assertThatExceptionEquals(result, expectedException);
41+
}
42+
43+
@Test
44+
void testFunctionalException() {
45+
final FunctionalException exception =
46+
new FunctionalException(
47+
FunctionalExceptionType.DECRYPTION_EXCEPTION, ComponentType.PROTOCOL_DLMS);
48+
49+
final FunctionalException expectedException =
50+
new FunctionalException(
51+
FunctionalExceptionType.DECRYPTION_EXCEPTION, ComponentType.PROTOCOL_DLMS);
52+
53+
final OsgpException result =
54+
this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
55+
56+
this.assertThatExceptionEquals(result, expectedException);
57+
}
58+
59+
@Test
60+
void testFunctionalExceptionWithProtocolAdapterException() {
61+
final FunctionalException exception =
62+
new FunctionalException(
63+
FunctionalExceptionType.SESSION_PROVIDER_ERROR,
64+
ComponentType.PROTOCOL_DLMS,
65+
new ProtocolAdapterException("protocol adapter exception message"));
66+
67+
final FunctionalException expectedException =
68+
new FunctionalException(
69+
FunctionalExceptionType.SESSION_PROVIDER_ERROR,
70+
ComponentType.PROTOCOL_DLMS,
71+
new OsgpException(exception.getComponentType(), exception.getCause().getMessage()));
72+
73+
final OsgpException result =
74+
this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
75+
76+
this.assertThatExceptionEquals(result, expectedException);
77+
}
78+
79+
@Test
80+
void testProtocolAdapterException() {
81+
final ProtocolAdapterException exception = new ProtocolAdapterException("Test Exception");
82+
83+
final TechnicalException expectedException =
84+
new TechnicalException(
85+
ComponentType.PROTOCOL_DLMS,
86+
"Unexpected exception while handling protocol request/response message",
87+
new OsgpException(ComponentType.PROTOCOL_DLMS, exception.getMessage()));
88+
89+
final OsgpException result =
90+
this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
91+
92+
this.assertThatExceptionEquals(result, expectedException);
93+
}
94+
95+
@Test
96+
void testConnectionException() {
97+
final ConnectionException exception = new ConnectionException("Test Exception");
98+
99+
final FunctionalException expectedException =
100+
new FunctionalException(
101+
FunctionalExceptionType.CONNECTION_ERROR,
102+
ComponentType.PROTOCOL_DLMS,
103+
new OsgpException(ComponentType.PROTOCOL_DLMS, exception.getMessage()));
104+
105+
final OsgpException result =
106+
this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
107+
108+
this.assertThatExceptionEquals(result, expectedException);
109+
}
110+
111+
@Test
112+
void testNotSupportedByProtocolException() {
113+
final NotSupportedByProtocolException exception =
114+
new NotSupportedByProtocolException("Test Exception");
115+
116+
final FunctionalException expectedException =
117+
new FunctionalException(
118+
FunctionalExceptionType.OPERATION_NOT_SUPPORTED_BY_PLATFORM_FOR_PROTOCOL,
119+
ComponentType.PROTOCOL_DLMS,
120+
new OsgpException(ComponentType.PROTOCOL_DLMS, exception.getMessage()));
121+
122+
final OsgpException result =
123+
this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
124+
125+
this.assertThatExceptionEquals(result, expectedException);
126+
}
127+
128+
void assertThatExceptionEquals(
129+
final OsgpException result, final OsgpException expectedException) {
130+
assertThat(result.getComponentType()).isEqualTo(expectedException.getComponentType());
131+
assertThat(result.getMessage()).isEqualTo(expectedException.getMessage());
132+
if (expectedException.getCause() != null) {
133+
assertThat(result.getCause().getClass()).isEqualTo(expectedException.getCause().getClass());
134+
assertThat(result.getCause().getMessage())
135+
.isEqualTo(expectedException.getCause().getMessage());
136+
} else {
137+
assertNull(result.getCause());
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)