Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Emad Alblueshi <[email protected]>
  • Loading branch information
EmadAlblueshi committed Jan 17, 2025
1 parent 4bdcf4d commit faa3750
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void testHookSendRejected() throws Exception {
}
}));
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foobar"),
"rejected");
"REJECTED", "Message is rejected");
await();
}

Expand All @@ -228,7 +228,7 @@ public void testHookSendMissingAddress() throws Exception {
be.complete(true);
}));
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foobar"),
"missing_address");
"MISSING_ADDRESS", "Message address is missing");
await();
}

Expand Down Expand Up @@ -284,7 +284,7 @@ public void testHookPubRejected() throws Exception {
}
}));
testError(new JsonObject().put("type", "publish").put("address", addr).put("body", "foobar"),
"rejected");
"REJECTED", "Message is rejected");
await();
}

Expand All @@ -299,7 +299,7 @@ public void testHookPublishMissingAddress() throws Exception {
be.complete(true);
}));
testError(new JsonObject().put("type", "publish").put("address", addr).put("body", "foobar"),
"missing_address");
"MISSING_ADDRESS", "Message address is missing");
await();
}

Expand Down Expand Up @@ -333,7 +333,7 @@ public void testHookRegisterRejected() throws Exception {
}
}));
testError(new JsonObject().put("type", "register").put("address", addr),
"rejected");
"REJECTED", "Message is rejected");
await();
}

Expand All @@ -348,7 +348,7 @@ public void testHookRegisterMissingAddress() throws Exception {
be.complete(true);
}));
testError(new JsonObject().put("type", "register").put("address", addr).put("body", "foobar"),
"missing_address");
"MISSING_ADDRESS", "Message address is missing");
await();
}

Expand Down Expand Up @@ -468,7 +468,7 @@ public void testHookUnregisterRejected() throws Exception {
}
}));
testError(new JsonObject().put("type", "unregister").put("address", addr),
"rejected");
"REJECTED", "Message is rejected");
await();
}

Expand All @@ -483,7 +483,7 @@ public void testHookUnregisterMissingAddress() throws Exception {
be.complete(true);
}));
testError(new JsonObject().put("type", "unregister").put("address", addr).put("body", "foobar"),
"missing_address");
"MISSING_ADDRESS", "Message address is missing");
await();
}

Expand Down Expand Up @@ -624,36 +624,36 @@ public void testUnregisterAllAccess() throws Exception {
public void testInvalidType() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(allAccessOptions));
testError(new JsonObject().put("type", "wibble").put("address", "addr"), "invalid_type");
testError(new JsonObject().put("type", "wibble").put("address", "addr"), "INVALID_TYPE", "Invalid message type");
}

@Test
public void testInvalidJson() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(allAccessOptions));
testError("oqiwjdioqwjdoiqjwd", "invalid_json");
testError("oqiwjdioqwjdoiqjwd", "INVALID_JSON", "Malformed JSON");
}

@Test
public void testMissingType() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(allAccessOptions));
testError(new JsonObject().put("address", "someaddress"), "missing_type");
testError(new JsonObject().put("address", "someaddress"), "MISSING_TYPE", "Message type is missing");
}

@Test
public void testMissingAddress() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(allAccessOptions));
testError(new JsonObject().put("type", "send").put("body", "hello world"), "missing_address");
testError(new JsonObject().put("type", "send").put("body", "hello world"), "MISSING_ADDRESS", "Message address is missing");
}

@Test
public void testSendNotPermittedDefaultOptions() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(defaultOptions));
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "hello world"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -663,7 +663,7 @@ public void testSendPermittedAllowAddress() throws Exception {
sockJS.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr))));
testSend(addr, "foobar");
testError(new JsonObject().put("type", "send").put("address", "allow2").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -674,7 +674,7 @@ public void testSendPermittedAllowAddressRe() throws Exception {
testSend("allow1", "foobar");
testSend("allow2", "foobar");
testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -687,7 +687,7 @@ public void testSendPermittedMultipleAddresses() throws Exception {
testSend("allow1", "foobar");
testSend("allow2", "foobar");
testError(new JsonObject().put("type", "send").put("address", "allow3").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -702,7 +702,7 @@ public void testSendPermittedMultipleAddressRe() throws Exception {
testSend("ballow1", "foobar");
testSend("ballow2", "foobar");
testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -716,9 +716,9 @@ public void testSendPermittedMixedAddressRe() throws Exception {
testSend("ballow1", "foobar");
testSend("ballow2", "foobar");
testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
testError(new JsonObject().put("type", "send").put("address", "allow2").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -732,7 +732,7 @@ public void testSendPermittedStructureMatch() throws Exception {
testSend(addr, json1);
json1.remove("fib");
testError(new JsonObject().put("type", "send").put("address", addr).put("body", json1),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -746,9 +746,9 @@ public void testSendPermittedStructureMatchWithAddress() throws Exception {
testSend(addr, json1);
json1.remove("fib");
testError(new JsonObject().put("type", "send").put("address", addr).put("body", json1),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
testError(new JsonObject().put("type", "send").put("address", "otheraddress").put("body", json1),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -758,7 +758,7 @@ public void testRegisterPermittedAllowAddress() throws Exception {
sockJS.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr))));
testReceive(addr, "foobar");
testError(new JsonObject().put("type", "register").put("address", "allow2").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -769,7 +769,7 @@ public void testRegisterPermittedAllowAddressRe() throws Exception {
testReceive("allow1", "foobar");
testReceive("allow2", "foobar");
testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -782,7 +782,7 @@ public void testRegisterPermittedMultipleAddresses() throws Exception {
testReceive("allow1", "foobar");
testReceive("allow2", "foobar");
testError(new JsonObject().put("type", "register").put("address", "allow3").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -797,7 +797,7 @@ public void testRegisterPermittedMultipleAddressRe() throws Exception {
testReceive("ballow1", "foobar");
testReceive("ballow2", "foobar");
testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand All @@ -811,9 +811,9 @@ public void testRegisterPermittedMixedAddressRe() throws Exception {
testReceive("ballow1", "foobar");
testReceive("ballow2", "foobar");
testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
testError(new JsonObject().put("type", "register").put("address", "allow2").put("body", "blah"),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand Down Expand Up @@ -1044,15 +1044,15 @@ public void testRegisterNotPermittedDefaultOptions() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(defaultOptions));
testError(new JsonObject().put("type", "register").put("address", addr),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
public void testUnregisterNotPermittedDefaultOptions() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(defaultOptions));
testError(new JsonObject().put("type", "unregister").put("address", addr),
"access_denied");
"ACCESS_DENIED", "Address access is denied");
}

@Test
Expand Down Expand Up @@ -1092,11 +1092,13 @@ public void testMaxHandlersPerSocket() throws Exception {
});

client.errorHandler(received -> {
Object rec = received.getValue("body");
Object rec = received.getValue("failureType");
int c = cnt.getAndIncrement();
if (c == 0) {
assertEquals("err", received.getString("type"));
assertEquals("max_handlers_reached", rec);
assertEquals(-1, received.getInteger("failureCode").intValue());
assertEquals("HANDLERS_MAX_LENGTH", rec);
assertEquals("Registration handlers exceed the maximum length", received.getString("message"));
} else if (c >= maxHandlers + 1) {
fail("Called too many times");
}
Expand Down Expand Up @@ -1124,7 +1126,9 @@ public void testMaxAddressLength() throws Exception {

client.errorHandler(received -> {
assertEquals("err", received.getString("type"));
assertEquals("max_address_length_reached", received.getString("body"));
assertEquals(-1, received.getInteger("failureCode").intValue());
assertEquals("ADDRESS_MAX_LENGTH", received.getString("failureType"));
assertEquals("Address exceeds maximum length", received.getString("message"));
client.close().onComplete(onSuccess(v2 -> latch.countDown()));
});

Expand All @@ -1138,7 +1142,7 @@ public void testMaxAddressLength() throws Exception {
public void testSendRequiresAuthorityNotLoggedIn() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("admin"))));
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "not_logged_in");
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "AUTHN", "Authentication failed");
}

@Test
Expand All @@ -1164,7 +1168,7 @@ public void testSendRequiresAuthorityHasnotAuthority() throws Exception {
router.route("/eventbus/*")
.handler(addLoginHandler(authProvider))
.subRouter(sockJS);
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "access_denied");
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "ACCESS_DENIED", "Address access is denied");
}

private AuthenticationHandler addLoginHandler(AuthenticationProvider authProvider) {
Expand All @@ -1183,7 +1187,7 @@ public void testInvalidClientReplyAddress() throws Exception {
router.route("/eventbus/*").subRouter(
sockJS.bridge(allAccessOptions));
testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo")
.put("replyAddress", "thishasmorethan36characters__________"), "invalid_reply_address");
.put("replyAddress", "thishasmorethan36characters__________"), "INVALID_REPLY_ADDRESS", "Reply address is invalid");
}

@Test
Expand Down Expand Up @@ -1254,17 +1258,19 @@ public void testPermittedOptionsJson() {
assertEquals(match, options.getMatch());
}

private void testError(JsonObject msg, String expectedErr) throws Exception {
testError(msg.encode(), expectedErr);
private void testError(JsonObject msg, String expectedType, String expectedMessage) throws Exception {
testError(msg.encode(), expectedType, expectedMessage);
}

private void testError(String msg, String expectedErr) throws Exception {
private void testError(String msg, String expectedType, String expectedMessage) throws Exception {

CountDownLatch latch = new CountDownLatch(1);

BridgeClient client = new BridgeClient(super.wsClient, transport);
client.errorHandler(received -> {
assertEquals(expectedErr, received.getString("body"));
assertEquals(-1, received.getInteger("failureCode").intValue());
assertEquals(expectedType, received.getString("failureType"));
assertEquals(expectedMessage, received.getString("message"));
latch.countDown();
});
client.connect(websocketURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,9 @@ public void testInvalidMessageCode() {
if (!frame.isClose()) {
JsonObject msg = new JsonObject(frame.binaryData());
assertEquals("err", msg.getString("type"));
assertEquals("invalid_json", msg.getString("body"));
assertEquals(-1, msg.getInteger("failureCode").intValue());
assertEquals("INVALID_JSON", msg.getString("failureType"));
assertEquals("Malformed JSON", msg.getString("message"));
testComplete();
ws.close();
}
Expand Down

0 comments on commit faa3750

Please sign in to comment.