Skip to content

Commit

Permalink
refactor: follow-up on #5258
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Sep 6, 2023
1 parent f58cdde commit cb3ad8b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit cb3ad8b

Please sign in to comment.