Skip to content

Commit fbc6757

Browse files
authored
Merge pull request #78 from openbase/feature/#72_introduce_further_rpc_exception_handling_tests
Implement RPCResolvedExceptionTest and port exception to kotlin.
2 parents 12aebe8 + 44f14f1 commit fbc6757

5 files changed

Lines changed: 122 additions & 78 deletions

File tree

module/communication/default/src/test/java/org/openbase/jul/communication/exception/RPCResolvedExceptionTest.kt

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
11
package org.openbase.jul.communication.exception
22

3-
import io.kotest.matchers.shouldBe
4-
import io.kotest.matchers.shouldNotBe
3+
import org.junit.jupiter.api.Assertions
54
import org.junit.jupiter.api.Test
6-
import org.junit.jupiter.api.TestInstance
75
import org.openbase.jul.exception.CouldNotPerformException
6+
import org.openbase.jul.exception.ExceptionProcessor
87
import org.openbase.jul.exception.NotAvailableException
8+
import io.kotest.matchers.shouldBe
9+
import io.kotest.matchers.shouldNotBe
10+
import org.junit.jupiter.api.TestInstance
911
import org.openbase.type.communication.mqtt.ResponseType
1012

1113
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
12-
class RPCResolvedExceptionTest {
14+
internal class RPCResolvedExceptionTest {
15+
16+
companion object {
17+
const val missingEntity = "mize"
18+
const val rootMessage = "root cause"
19+
const val secondLevelMessage = "second level cause"
20+
}
21+
22+
@Test
23+
fun testExceptionResolution() {
24+
25+
val testException = CouldNotPerformException(
26+
message = rootMessage,
27+
cause = CouldNotPerformException(
28+
message = secondLevelMessage,
29+
cause = NotAvailableException(missingEntity)
30+
)
31+
)
32+
33+
RPCException(message = testException.stackTraceToString()).let { rpcException ->
34+
35+
// resolve exception via explicit method call
36+
RPCResolvedException
37+
.resolveRPCException(rpcException)
38+
.let { result ->
39+
ExceptionProcessor.getInitialCause(result).let { initialCause ->
40+
Assertions.assertEquals(initialCause.message, NotAvailableException(missingEntity).message)
41+
}
42+
}
43+
44+
// resolve exception via constructor call
45+
RPCResolvedException(rpcException)
46+
.let { result ->
47+
ExceptionProcessor.getInitialCause(result).let { initialCause ->
48+
Assertions.assertEquals(initialCause.message, NotAvailableException(missingEntity).message)
49+
}
50+
}
51+
}
52+
}
1353

1454
@Test
1555
fun testExceptionResolving() {

module/communication/mqtt/src/test/java/org/openbase/jul/communication/mqtt/RPCServerImplTest.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.openbase.jul.communication.config.CommunicatorConfig
1616
import org.openbase.jul.communication.exception.RPCException
1717
import org.openbase.jul.communication.exception.RPCResolvedException
1818
import org.openbase.jul.exception.CouldNotPerformException
19+
import org.openbase.jul.exception.ExceptionProcessor
1920
import org.openbase.jul.exception.NotAvailableException
2021
import org.openbase.jul.extension.type.processing.ScopeProcessor
2122
import org.openbase.jul.schedule.GlobalCachedExecutorService
@@ -165,12 +166,6 @@ internal class RPCServerImplTest {
165166
callback.accept(clientRequest)
166167
}
167168

168-
@Test
169-
@Timeout(value = 30)
170-
fun `test bad request id`() {
171-
//TODO: verify that id is a valid uuid
172-
}
173-
174169
/**
175170
* Verify that no matter the request, the server
176171
* always responds with an acknowledgement first.
@@ -209,7 +204,7 @@ internal class RPCServerImplTest {
209204
actualResponse.hasResult() shouldBe false
210205

211206
val error = RPCResolvedException.resolveRPCException(RPCException(actualResponse.error))
212-
error shouldBe NotAvailableException("Method $methodName")
207+
ExceptionProcessor.getInitialCause(error) shouldBe NotAvailableException("Method $methodName")
213208
}
214209

215210
@Test
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.openbase.jul.exception
2+
/*
3+
* #%L
4+
* JUL Exception
5+
* %%
6+
* Copyright (C) 2015 - 2022 openbase.org
7+
* %%
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Lesser Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Lesser Public
19+
* License along with this program. If not, see
20+
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
21+
* #L%
22+
*/ /**
23+
* @author [Divine Threepwood](mailto:divine@openbase.org)
24+
*/
25+
open class CouldNotPerformException : Exception {
26+
/**
27+
* {@inheritDoc}
28+
*
29+
* @param message {@inheritDoc}
30+
*/
31+
constructor(message: String?) : super(message)
32+
33+
/**
34+
* {@inheritDoc}
35+
*
36+
* @param message {@inheritDoc}
37+
* @param cause {@inheritDoc}
38+
*/
39+
constructor(message: String?, cause: Throwable?) : super(message, cause)
40+
41+
/**
42+
* {@inheritDoc}
43+
*
44+
* @param cause {@inheritDoc}
45+
*/
46+
constructor(cause: Throwable?) : super(cause)
47+
48+
/**
49+
* {@inheritDoc}
50+
*
51+
* @param message {@inheritDoc}
52+
* @param cause {@inheritDoc}
53+
* @param enableSuppression {@inheritDoc}
54+
* @param writableStackTrace {@inheritDoc}
55+
*/
56+
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
57+
message,
58+
cause,
59+
enableSuppression,
60+
writableStackTrace
61+
)
62+
}

module/exception/src/main/java/org/openbase/jul/exception/CouldNotProcessException.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

module/exception/src/main/java/org/openbase/jul/exception/CouldNotPerformException.java renamed to module/exception/src/main/java/org/openbase/jul/exception/CouldNotProcessException.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.openbase.jul.exception;
1+
package org.openbase.jul.exception
22

33
/*
44
* #%L
@@ -20,41 +20,32 @@
2020
* License along with this program. If not, see
2121
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
2222
* #L%
23+
*/ /**
24+
*
25+
* @author [Divine Threepwood](mailto:divine@openbase.org)
2326
*/
24-
25-
26-
/**
27-
* @author <a href="mailto:divine@openbase.org">Divine Threepwood</a>
28-
*/
29-
public class CouldNotPerformException extends Exception {
30-
27+
class CouldNotProcessException : RuntimeException {
3128
/**
3229
* {@inheritDoc}
3330
*
3431
* @param message {@inheritDoc}
3532
*/
36-
public CouldNotPerformException(String message) {
37-
super(message);
38-
}
33+
constructor(message: String?) : super(message)
3934

4035
/**
4136
* {@inheritDoc}
4237
*
4338
* @param message {@inheritDoc}
4439
* @param cause {@inheritDoc}
4540
*/
46-
public CouldNotPerformException(String message, Throwable cause) {
47-
super(message, cause);
48-
}
41+
constructor(message: String?, cause: Throwable?) : super(message, cause)
4942

5043
/**
5144
* {@inheritDoc}
5245
*
5346
* @param cause {@inheritDoc}
5447
*/
55-
public CouldNotPerformException(Throwable cause) {
56-
super(cause);
57-
}
48+
constructor(cause: Throwable?) : super(cause)
5849

5950
/**
6051
* {@inheritDoc}
@@ -64,7 +55,10 @@ public CouldNotPerformException(Throwable cause) {
6455
* @param enableSuppression {@inheritDoc}
6556
* @param writableStackTrace {@inheritDoc}
6657
*/
67-
public CouldNotPerformException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
68-
super(message, cause, enableSuppression, writableStackTrace);
69-
}
58+
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
59+
message,
60+
cause,
61+
enableSuppression,
62+
writableStackTrace
63+
)
7064
}

0 commit comments

Comments
 (0)