From cb3ad8bec46f7abb82707cef8f711af1b44b27f2 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Wed, 6 Sep 2023 13:54:34 +0200 Subject: [PATCH] refactor: follow-up on #5258 Signed-off-by: Marc Nuri --- .../server/mock/ErrorStreamMessage.java | 2 +- .../server/mock/OutputStreamMessage.java | 2 +- .../server/mock/StatusStreamMessage.java | 2 +- .../server/mock/ErrorStreamMessageTest.java | 25 +++++- .../server/mock/OutputStreamMessageTest.java | 25 +++++- .../server/mock/StatusStreamMessageTest.java | 77 +++++++++++-------- 6 files changed, 93 insertions(+), 40 deletions(-) diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessage.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessage.java index 4e02d8a1053..b12436512a8 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessage.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessage.java @@ -22,7 +22,7 @@ public class ErrorStreamMessage extends WebSocketMessage { - static final byte ERR_STREAM_ID = 2; + private static final byte ERR_STREAM_ID = 2; public ErrorStreamMessage(String body) { super(0L, getBodyBytes(ERR_STREAM_ID, body), true, true); diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessage.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessage.java index 97c0fe685fb..b2331619784 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessage.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessage.java @@ -22,7 +22,7 @@ public class OutputStreamMessage extends WebSocketMessage { - static final byte OUT_STREAM_ID = 1; + private static final byte OUT_STREAM_ID = 1; public OutputStreamMessage(String body) { super(0L, getBodyBytes(OUT_STREAM_ID, body), true, true); diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessage.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessage.java index 313eef5665b..85b191e7982 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessage.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessage.java @@ -25,7 +25,7 @@ public class StatusStreamMessage extends WebSocketMessage { - static final byte ERROR_CHANNEL_STREAM_ID = 3; + private static final byte ERROR_CHANNEL_STREAM_ID = 3; public StatusStreamMessage(final int exitCode) { super(0L, getBodyBytes(ERROR_CHANNEL_STREAM_ID, getStatusBody(exitCode)), true, true); diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessageTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessageTest.java index 4d7bef4fcea..376b5d95179 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessageTest.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/ErrorStreamMessageTest.java @@ -16,18 +16,37 @@ package io.fabric8.kubernetes.client.server.mock; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class ErrorStreamMessageTest { + private ErrorStreamMessage message; + + @BeforeEach + void setUp() { + message = new ErrorStreamMessage("foobar"); + } + @Test - void testMessageEncoding() { - final ErrorStreamMessage message = new ErrorStreamMessage("foobar"); + void isBinaryReturnsTrue() { assertThat(message.isBinary()).isTrue(); + } + + @Test + void isToBeRemovedReturnsTrue() { assertThat(message.isToBeRemoved()).isTrue(); - assertThat(message.getBytes()).startsWith(ErrorStreamMessage.ERR_STREAM_ID); + } + + @Test + void bodyStartsWithErrStreamId() { + assertThat(message.getBytes()).startsWith(2); + } + + @Test + void bodyContainsMessage() { assertThat(message.getBody().substring(1)).isEqualTo("foobar"); } } diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessageTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessageTest.java index c77a1e8f58e..a3f12991f9b 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessageTest.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/OutputStreamMessageTest.java @@ -16,18 +16,37 @@ package io.fabric8.kubernetes.client.server.mock; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class OutputStreamMessageTest { + private OutputStreamMessage message; + + @BeforeEach + void setUp() { + message = new OutputStreamMessage("foobar"); + } + @Test - void testMessageEncoding() { - final OutputStreamMessage message = new OutputStreamMessage("foobar"); + void isBinaryReturnsTrue() { assertThat(message.isBinary()).isTrue(); + } + + @Test + void isToBeRemovedReturnsTrue() { assertThat(message.isToBeRemoved()).isTrue(); - assertThat(message.getBytes()).startsWith(OutputStreamMessage.OUT_STREAM_ID); + } + + @Test + void bodyStartsWithOutStreamId() { + assertThat(message.getBytes()).startsWith(1); + } + + @Test + void bodyContainsMessage() { assertThat(message.getBody().substring(1)).isEqualTo("foobar"); } } diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessageTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessageTest.java index b2e28f389a4..baf230e5d5b 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessageTest.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/StatusStreamMessageTest.java @@ -18,50 +18,65 @@ import io.fabric8.kubernetes.api.model.Status; import io.fabric8.kubernetes.api.model.StatusBuilder; -import io.fabric8.kubernetes.client.utils.Serialization; +import io.fabric8.kubernetes.client.utils.KubernetesSerialization; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class StatusStreamMessageTest { + private StatusStreamMessage message; + + @BeforeEach + void setUp() { + message = new StatusStreamMessage(1); + } + @Test - void testMessageEncoding_withExitCode0() { - final StatusStreamMessage message = new StatusStreamMessage(0); + void isBinaryReturnsTrue() { assertThat(message.isBinary()).isTrue(); + } + + @Test + void isToBeRemovedReturnsTrue() { assertThat(message.isToBeRemoved()).isTrue(); - assertThat(message.getBytes()).startsWith(StatusStreamMessage.ERROR_CHANNEL_STREAM_ID); + } - final Status status = new StatusBuilder() // - .withStatus("Success") - .withReason("ExitCode") - .withNewDetails() - .addNewCause() - .withReason("ExitCode") - .withMessage(String.valueOf(0)) - .endCause() - .endDetails() - .build(); - assertThat(message.getBody().substring(1)).isEqualTo(Serialization.asJson(status)); + @Test + void bodyStartsWithErrorStreamId() { + assertThat(message.getBytes()).startsWith(3); } @Test - void testMessageEncoding_withExitCode1() { - final StatusStreamMessage message = new StatusStreamMessage(1); - assertThat(message.isBinary()).isTrue(); - assertThat(message.isToBeRemoved()).isTrue(); - assertThat(message.getBytes()).startsWith(StatusStreamMessage.ERROR_CHANNEL_STREAM_ID); + void withExitCode0_bodyContainsSuccessStatusObject() { + message = new StatusStreamMessage(0); + assertThat(new KubernetesSerialization().unmarshal(message.getBody().substring(1), Status.class)) + .isEqualTo(new StatusBuilder() + .withStatus("Success") + .withReason("ExitCode") + .withNewDetails() + .addNewCause() + .withReason("ExitCode") + .withMessage(String.valueOf(0)) + .endCause() + .endDetails() + .build()); + } - final Status status = new StatusBuilder() // - .withStatus("Failure") - .withReason("NonZeroExitCode") - .withNewDetails() - .addNewCause() - .withReason("ExitCode") - .withMessage(String.valueOf(1)) - .endCause() - .endDetails() - .build(); - assertThat(message.getBody().substring(1)).isEqualTo(Serialization.asJson(status)); + @Test + void withExitCode1_bodyContainsFailureStatusObject() { + message = new StatusStreamMessage(1); + assertThat(new KubernetesSerialization().unmarshal(message.getBody().substring(1), Status.class)) + .isEqualTo(new StatusBuilder() + .withStatus("Failure") + .withReason("NonZeroExitCode") + .withNewDetails() + .addNewCause() + .withReason("ExitCode") + .withMessage(String.valueOf(1)) + .endCause() + .endDetails() + .build()); } }