Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add failureDetail to DataException
- Loading branch information
Showing
7 changed files
with
448 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/io/neonbee/internal/codec/DataExceptionMessageCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.neonbee.internal.codec; | ||
|
||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.stream.Collectors; | ||
|
||
import io.neonbee.data.DataException; | ||
import io.netty.util.CharsetUtil; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.eventbus.MessageCodec; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
public class DataExceptionMessageCodec implements MessageCodec<DataException, DataException> { | ||
|
||
@Override | ||
public void encodeToWire(Buffer buffer, DataException exception) { | ||
buffer.appendInt(exception.failureCode()); | ||
|
||
String msgEncoded = exception.getMessage(); | ||
if (msgEncoded != null) { | ||
byte[] msgBytes = msgEncoded.getBytes(CharsetUtil.UTF_8); | ||
buffer.appendInt(msgBytes.length); | ||
buffer.appendBytes(msgBytes); | ||
} else { | ||
buffer.appendInt(-1); | ||
} | ||
|
||
Buffer failureDetailEncoded = new JsonObject(exception.failureDetail()).toBuffer(); | ||
buffer.appendInt(failureDetailEncoded.length()); | ||
buffer.appendBuffer(failureDetailEncoded); | ||
} | ||
|
||
@Override | ||
public DataException decodeFromWire(int pos, Buffer buffer) { | ||
int tmpPos = pos; | ||
int failureCode = buffer.getInt(tmpPos); | ||
tmpPos += Integer.BYTES; | ||
|
||
int msgLength = buffer.getInt(tmpPos); | ||
tmpPos += Integer.BYTES; | ||
String message = | ||
msgLength != -1 ? new String(buffer.getBytes(tmpPos, tmpPos + msgLength), CharsetUtil.UTF_8) : null; | ||
tmpPos += msgLength != -1 ? msgLength : 0; | ||
|
||
int failureDetailLength = buffer.getInt(tmpPos); | ||
tmpPos += Integer.BYTES; | ||
|
||
Buffer buff = buffer.getBuffer(tmpPos, tmpPos + failureDetailLength); | ||
Map<String, Object> failureDetail = | ||
buff.toJsonObject().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue)); | ||
|
||
return new DataException(failureCode, message, failureDetail); | ||
} | ||
|
||
@Override | ||
public DataException transform(DataException exception) { | ||
return exception; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "dataexception"; | ||
} | ||
|
||
@Override | ||
public byte systemCodecID() { | ||
return -1; | ||
} | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
src/test/java/io/neonbee/cluster/DataExceptionRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package io.neonbee.cluster; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static io.neonbee.test.helper.DeploymentHelper.deployVerticle; | ||
import static io.vertx.core.Future.failedFuture; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import io.neonbee.NeonBee; | ||
import io.neonbee.NeonBeeExtension; | ||
import io.neonbee.NeonBeeInstanceConfiguration; | ||
import io.neonbee.data.DataContext; | ||
import io.neonbee.data.DataException; | ||
import io.neonbee.data.DataMap; | ||
import io.neonbee.data.DataQuery; | ||
import io.neonbee.data.DataRequest; | ||
import io.neonbee.data.DataVerticle; | ||
import io.neonbee.data.internal.DataContextImpl; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
|
||
@ExtendWith(NeonBeeExtension.class) | ||
class DataExceptionRequestTest { | ||
private static final JsonObject FAILURE_OBJECT = new JsonObject().put("code", new JsonArray() | ||
.add(new JsonObject().put("message", "This is a bad response")).add(new JsonObject().put("lang", "en"))); | ||
|
||
private static final Map<String, Object> FAILURE_DETAIL = Map.of("error", FAILURE_OBJECT); | ||
|
||
private static final DataVerticle<Buffer> DATA_EXCEPTION_VERTICLE = new DataVerticle<>() { | ||
@Override | ||
public String getName() { | ||
return "DataException"; | ||
} | ||
|
||
@Override | ||
public Future<Buffer> retrieveData(DataQuery query, DataMap dataMap, DataContext context) { | ||
return failedFuture(new DataException(400, "Bad Response", FAILURE_DETAIL)); | ||
} | ||
}; | ||
|
||
private static final DataVerticle<Buffer> DATA_EXCEPTION_VERTICLE_ONLY_FAILURE_CODE = new DataVerticle<>() { | ||
@Override | ||
public String getName() { | ||
return "DataExceptionFailureCodeOnly"; | ||
} | ||
|
||
@Override | ||
public Future<Buffer> retrieveData(DataQuery query, DataMap dataMap, DataContext context) { | ||
return failedFuture(new DataException(400)); | ||
} | ||
}; | ||
|
||
private static final DataVerticle<Buffer> DATA_EXCEPTION_VERTICLE_FAILURE_CODE_AND_MESSAGE = new DataVerticle<>() { | ||
@Override | ||
public String getName() { | ||
return "DataExceptionFailureCodeAndMessage"; | ||
} | ||
|
||
@Override | ||
public Future<Buffer> retrieveData(DataQuery query, DataMap dataMap, DataContext context) { | ||
return failedFuture(new DataException(400, "Bad response")); | ||
} | ||
}; | ||
|
||
@Test | ||
@Timeout(value = 10, timeUnit = TimeUnit.HOURS) | ||
@DisplayName("Test that DataException can be returned via the event bus") | ||
void testDataExceptionRequest(@NeonBeeInstanceConfiguration(clustered = true, activeProfiles = {}) NeonBee source, | ||
@NeonBeeInstanceConfiguration(clustered = true, activeProfiles = {}) NeonBee target, | ||
VertxTestContext testContext) { | ||
deployVerticle(target.getVertx(), DATA_EXCEPTION_VERTICLE).compose(s -> { | ||
DataRequest request = new DataRequest(DATA_EXCEPTION_VERTICLE.getName()); | ||
return DataVerticle.<JsonObject>requestData(source.getVertx(), request, new DataContextImpl()); | ||
}).onComplete(testContext.failing(response -> { | ||
testContext.verify(() -> { | ||
assertThat(response).isInstanceOf(DataException.class); | ||
assertThat(((DataException) response).failureCode()).isEqualTo(400); | ||
assertThat(response.getMessage()).isEqualTo("Bad Response"); | ||
assertThat(((DataException) response).failureDetail().get("error")).isEqualTo(FAILURE_OBJECT); | ||
}); | ||
testContext.completeNow(); | ||
})); | ||
} | ||
|
||
@Test | ||
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS) | ||
@DisplayName("Test that DataException with failure code and message be returned via the event bus") | ||
void testDataExceptionRequestFailureCodeAndMessage( | ||
@NeonBeeInstanceConfiguration(clustered = true, activeProfiles = {}) NeonBee source, | ||
@NeonBeeInstanceConfiguration(clustered = true, activeProfiles = {}) NeonBee target, | ||
VertxTestContext testContext) { | ||
deployVerticle(target.getVertx(), DATA_EXCEPTION_VERTICLE_FAILURE_CODE_AND_MESSAGE).compose(s -> { | ||
DataRequest request = new DataRequest(DATA_EXCEPTION_VERTICLE_FAILURE_CODE_AND_MESSAGE.getName()); | ||
return DataVerticle.<JsonObject>requestData(source.getVertx(), request, new DataContextImpl()); | ||
}).onComplete(testContext.failing(response -> { | ||
testContext.verify(() -> { | ||
assertThat(response).isInstanceOf(DataException.class); | ||
assertThat(((DataException) response).failureCode()).isEqualTo(400); | ||
assertThat(response.getMessage()).isEqualTo("Bad response"); | ||
}); | ||
testContext.completeNow(); | ||
})); | ||
} | ||
|
||
@Test | ||
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS) | ||
@DisplayName("Test that DataException with failure code be returned via the event bus") | ||
void testDataExceptionRequestFailureCodeOnly( | ||
@NeonBeeInstanceConfiguration(clustered = true, activeProfiles = {}) NeonBee source, | ||
@NeonBeeInstanceConfiguration(clustered = true, activeProfiles = {}) NeonBee target, | ||
VertxTestContext testContext) { | ||
deployVerticle(target.getVertx(), DATA_EXCEPTION_VERTICLE_ONLY_FAILURE_CODE).compose(s -> { | ||
DataRequest request = new DataRequest(DATA_EXCEPTION_VERTICLE_ONLY_FAILURE_CODE.getName()); | ||
return DataVerticle.<JsonObject>requestData(source.getVertx(), request, new DataContextImpl()); | ||
}).onComplete(testContext.failing(response -> { | ||
testContext.verify(() -> { | ||
assertThat(response).isInstanceOf(DataException.class); | ||
assertThat(((DataException) response).failureCode()).isEqualTo(400); | ||
assertThat(response.getMessage()).isEqualTo(null); | ||
assertThat(((DataException) response).failureDetail()).isEqualTo(Map.of()); | ||
}); | ||
testContext.completeNow(); | ||
})); | ||
} | ||
} |
Oops, something went wrong.