Skip to content

Commit

Permalink
adding unittests
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Zimin <[email protected]>
  • Loading branch information
Eugene Zimin committed Oct 16, 2023
1 parent 192925c commit 191287b
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void healthUp() {
protected <T> void healthReconnecting(@Nullable T eventArgs) {
try {
writeLock.lock();
health = addSessionEventDetails(Health.status(STATUS_RECONNECTING), eventArgs).build();
health = addEventDetails(Health.status(STATUS_RECONNECTING), eventArgs).build();
logDebugStatus(STATUS_RECONNECTING);
} finally {
writeLock.unlock();
Expand All @@ -52,14 +52,14 @@ protected <T> void healthReconnecting(@Nullable T eventArgs) {
protected <T> void healthDown(@Nullable T eventArgs) {
try {
writeLock.lock();
health = addSessionEventDetails(Health.down(), eventArgs).build();
health = addEventDetails(Health.down(), eventArgs).build();
logDebugStatus(String.valueOf(Status.DOWN));
} finally {
writeLock.unlock();
}
}

public <T> Health.Builder addSessionEventDetails(Health.Builder builder, @Nullable T eventArgs) {
public <T> Health.Builder addEventDetails(Health.Builder builder, @Nullable T eventArgs) {
if (eventArgs == null) {
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.health.base;

import com.solacesystems.jcsmp.FlowEvent;
import com.solacesystems.jcsmp.FlowEventArgs;
import com.solacesystems.jcsmp.SessionEvent;
import com.solacesystems.jcsmp.SessionEventArgs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;

import static org.junit.jupiter.api.Assertions.*;

class SolaceHealthIndicatorTest {

private SolaceHealthIndicator solaceHealthIndicator;

@BeforeEach
void setUp() {
this.solaceHealthIndicator = new SolaceHealthIndicator();
}

@Test
void healthUp() {
this.solaceHealthIndicator.healthUp();
assertEquals(this.solaceHealthIndicator.getHealth(), Health.up().build());
}

@Test
void healthReconnecting() {
this.solaceHealthIndicator.healthReconnecting(null);
assertEquals(this.solaceHealthIndicator.getHealth(), Health.status("RECONNECTING").build());
}

@Test
void healthDown() {
this.solaceHealthIndicator.healthDown(null);
assertEquals(this.solaceHealthIndicator.getHealth(), Health.down().build());
}

@Test
void addFlowEventDetails() {
// as SessionEventArgs constructor has package level access modifier, we have to test with FlowEventArgs only
FlowEventArgs flowEventArgs = new FlowEventArgs(FlowEvent.FLOW_DOWN, "String_infoStr",
new Exception("Test Exception"), 500);
Health health = this.solaceHealthIndicator.addEventDetails(Health.down(),flowEventArgs).build();

assertEquals(health.getStatus(), Status.DOWN);
assertEquals(health.getDetails().get("error"), "java.lang.Exception: Test Exception");
assertEquals(health.getDetails().get("responseCode"), 500);
}

@Test
void getHealth() {
this.solaceHealthIndicator.setHealth(Health.up().build());
assertEquals(this.solaceHealthIndicator.getHealth(), Health.up().build());
}

@Test
void setHealth() {
this.solaceHealthIndicator.setHealth(Health.down().build());
assertEquals(this.solaceHealthIndicator.getHealth(), Health.down().build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.health.indicators;

import com.health.base.SolaceHealthIndicator;
import com.solacesystems.jcsmp.FlowEvent;
import com.solacesystems.jcsmp.FlowEventArgs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

class SolaceFlowHealthIndicatorTest {

private SolaceFlowHealthIndicator solaceHealthIndicator;

@BeforeEach
void setUp() {
this.solaceHealthIndicator = new SolaceFlowHealthIndicator();
}

@Test
void up() {
this.solaceHealthIndicator.up();
assertEquals(this.solaceHealthIndicator.getHealth(), Health.up().build());
}

@Test
void reconnecting() {
FlowEventArgs flowEventArgs = new FlowEventArgs(FlowEvent.FLOW_RECONNECTING, "String_infoStr",
new Exception("Test Exception"), 500);
this.solaceHealthIndicator.reconnecting(flowEventArgs);
assertEquals(this.solaceHealthIndicator.getHealth().getStatus(), Health.status("RECONNECTING").build().getStatus());
assertEquals(this.solaceHealthIndicator.getHealth().getDetails().get("error"), "java.lang.Exception: Test Exception");
assertEquals(this.solaceHealthIndicator.getHealth().getDetails().get("responseCode"), 500);

}

@Test
void down() {
FlowEventArgs flowEventArgs = new FlowEventArgs(FlowEvent.FLOW_DOWN, "String_infoStr",
new Exception("Test Exception"), 500);
this.solaceHealthIndicator.down(flowEventArgs);
assertEquals(this.solaceHealthIndicator.getHealth().getStatus(), Status.DOWN);
assertEquals(this.solaceHealthIndicator.getHealth().getDetails().get("error"), "java.lang.Exception: Test Exception");
assertEquals(this.solaceHealthIndicator.getHealth().getDetails().get("responseCode"), 500);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.health.indicators;

import com.solacesystems.jcsmp.FlowEvent;
import com.solacesystems.jcsmp.FlowEventArgs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;

import static org.junit.jupiter.api.Assertions.*;

class SolaceSessionHealthIndicatorTest {

private SolaceSessionHealthIndicator solaceHealthIndicator;

@BeforeEach
void setUp() {
this.solaceHealthIndicator = new SolaceSessionHealthIndicator();
}

@Test
void up() {
this.solaceHealthIndicator.up();
assertEquals(this.solaceHealthIndicator.getHealth(), Health.up().build());
}

@Test
void reconnecting() {
this.solaceHealthIndicator.reconnecting(null);
assertEquals(this.solaceHealthIndicator.getHealth().getStatus(), Health.status("RECONNECTING").build().getStatus());
}

@Test
void down() {
this.solaceHealthIndicator.down(null);
assertEquals(this.solaceHealthIndicator.getHealth().getStatus(), Status.DOWN);
}
}

0 comments on commit 191287b

Please sign in to comment.