ExtensionWebSocketClient { synchronized public CompletableFuture<Boolean> authenticate(String user, String pass) { Map<String, String> authData = new LinkedHashMap<>(); authData.put("username", user); authData.put("password", pass); this.authData = authData; if (webSocketFuture != null && (authFuture == null || !authFuture.getNow(true))) { authFuture = webSocketFuture.thenComposeAsync( (wsSuccess) -> { if (wsSuccess) { doAuthentication(); return new CompletableFuture<Boolean>(); } else { return CompletableFuture.completedFuture(false); } } ); } return authFuture; } ExtensionWebSocketClient(String sourceName); ExtensionWebSocketListener getListener(); String getSourceName(); CompletableFuture<Boolean> initiateFullConnection(String url, String token); synchronized CompletableFuture<Boolean> initiateWebsocketConnection(String url); CompletableFuture<Boolean> getWebsocketConnectionFuture(); void sendNotification(Object data); void sendQueryResponseEmpty(String replyAddress); void sendQueryResponse(int httpCode, String replyAddress, Map body); void sendQueryResponse(int httpCode, String replyAddress, Map[] body); void sendQueryError(String replyAddress, String messageCode, String messageTemplate, Object[] parameters); void send(Object obj); synchronized CompletableFuture<Boolean> authenticate(String user, String pass); synchronized CompletableFuture<Boolean> authenticate(String token); CompletableFuture<Boolean> getAuthenticationFuture(); synchronized CompletableFuture<Boolean> connectToSource(); CompletableFuture<Boolean> getSourceConnectionFuture(); void sourceHasDisconnected(); void setAutoReconnect(boolean value); boolean isOpen(); boolean isAuthed(); boolean isConnected(); void close(); void stop(); void useHandlersFrom(ExtensionWebSocketListener listener); void useHandlersFrom(ExtensionWebSocketClient client); void setCloseHandler(Handler<ExtensionWebSocketClient> closeHandler); void setHttpHandler(Handler<Response> httpHandler); void setPublishHandler(Handler<ExtensionServiceMessage> publishHandler); void setQueryHandler(Handler<ExtensionServiceMessage> queryHandler); void setConfigHandler(Handler<ExtensionServiceMessage> configHandler); void setAuthHandler(Handler<Response> authHandler); void setReconnectHandler(Handler<ExtensionServiceMessage> reconnectHandler); static final int QUERY_CHUNK_CODE; static final int QUERY_DATA_CODE; static final int QUERY_NODATA_CODE; }
@Test public void testAuthenticateWithUsername() throws InterruptedException { String user = "myName"; String pass = "p@s$w0rd"; client.webSocketFuture = CompletableFuture.completedFuture(true); client.authenticate(user, pass); waitUntilTrue(5 * 1000, () -> socket.receivedMessage()); assert socket.compareData("object.username", user); assert socket.compareData("object.password", pass); assert socket.compareData("op", "authenticate"); assert socket.compareData("resourceName", "system.credentials"); }
@Test public void testAuthenticateWithToken() throws InterruptedException { client.webSocketFuture = CompletableFuture.completedFuture(true); String token = "ajeoslvkencmvkejshegwt="; client.authenticate(token); waitUntilTrue(5 * 1000, () -> socket.receivedMessage()); assert socket.compareData("object", token); assert socket.compareData("op", "validate"); assert socket.compareData("resourceName", "system.credentials"); }