Skip to content

Commit

Permalink
#124 : Added datetime attribute to REST API BotStatus response
Browse files Browse the repository at this point in the history
  • Loading branch information
gazbert committed Apr 4, 2020
1 parent 113acc2 commit f1e1208
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.base.MoreObjects;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;

/**
* Domain object representing the Bot's status.
Expand All @@ -40,16 +41,18 @@ public class BotStatus {

private String displayName;
private String status;
private Date datetime;

// Required by ConfigurableComponentFactory
public BotStatus() {
}

/** Creates a new BotStatus. */
public BotStatus(String botId, String displayName, String status) {
public BotStatus(String botId, String displayName, String status, Date datetime) {
this.botId = botId;
this.displayName = displayName;
this.status = status;
setDatetime(datetime);
}

public String getBotId() {
Expand All @@ -76,12 +79,21 @@ public void setStatus(String status) {
this.status = status;
}

public Date getDatetime() {
return datetime != null ? new Date(datetime.getTime()) : null;
}

public void setDatetime(Date datetime) {
this.datetime = datetime != null ? new Date(datetime.getTime()) : null;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("botId", botId)
.add("displayName", displayName)
.add("status", status)
.add("datetime", getDatetime())
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import org.junit.Test;

/**
Expand All @@ -38,13 +40,21 @@ public class TestBotStatus {
private static final String BOT_ID = "avro-707_1";
private static final String DISPLAY_NAME = "Avro 707";
private static final String STATUS = "running";
private static final Date DATE = new Date();

@Test
public void testInitialisationWorksAsExpected() {
final BotStatus botStatus = new BotStatus(BOT_ID, DISPLAY_NAME, STATUS);
final BotStatus botStatus = new BotStatus(BOT_ID, DISPLAY_NAME, STATUS, DATE);
assertEquals(BOT_ID, botStatus.getBotId());
assertEquals(DISPLAY_NAME, botStatus.getDisplayName());
assertEquals(STATUS, botStatus.getStatus());
assertEquals(DATE.getTime(), botStatus.getDatetime().getTime());

final BotStatus botStatusNullDate = new BotStatus(BOT_ID, DISPLAY_NAME, STATUS, null);
assertEquals(BOT_ID, botStatusNullDate.getBotId());
assertEquals(DISPLAY_NAME, botStatusNullDate.getDisplayName());
assertEquals(STATUS, botStatusNullDate.getStatus());
assertNull(botStatusNullDate.getDatetime());
}

@Test
Expand All @@ -53,6 +63,7 @@ public void testSettersWorkAsExpected() {
assertNull(botStatus.getBotId());
assertNull(botStatus.getDisplayName());
assertNull(botStatus.getStatus());
assertNull(botStatus.getDatetime());

botStatus.setBotId(BOT_ID);
assertEquals(BOT_ID, botStatus.getBotId());
Expand All @@ -62,12 +73,18 @@ public void testSettersWorkAsExpected() {

botStatus.setStatus(STATUS);
assertEquals(STATUS, botStatus.getStatus());

botStatus.setDatetime(null);
assertNull(botStatus.getDatetime());

botStatus.setDatetime(DATE);
assertEquals(DATE.getTime(), botStatus.getDatetime().getTime());
}

@Test
public void testToStringWorksAsExpected() {
final BotStatus botStatus = new BotStatus(BOT_ID, DISPLAY_NAME, STATUS);
assertEquals(
"BotStatus{botId=avro-707_1, displayName=Avro 707, status=running}", botStatus.toString());
final BotStatus botStatus = new BotStatus(BOT_ID, DISPLAY_NAME, STATUS, DATE);
assertTrue(botStatus.toString().startsWith(
"BotStatus{botId=avro-707_1, displayName=Avro 707, status=running, datetime="));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.gazbert.bxbot.services.runtime.BotStatusService;
import io.swagger.annotations.Api;
import java.security.Principal;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -84,6 +85,7 @@ public BotStatus getStatus(@ApiIgnore Principal principal) {
botStatus.setBotId(engineConfig.getBotId());
botStatus.setDisplayName(engineConfig.getBotName());
botStatus.setStatus(status);
botStatus.setDatetime(new Date());

LOG.info(() -> "Response: " + botStatus);
return botStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public void testGetBotStatusWithValidToken() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$.botId").value(BOT_ID))
.andExpect(jsonPath("$.displayName").value(BOT_NAME))
.andExpect(jsonPath("$.status").value(BOT_STATUS));
.andExpect(jsonPath("$.status").value(BOT_STATUS))
.andExpect(jsonPath("$.datetime").isNotEmpty());

verify(engineConfigService, times(1)).getEngineConfig();
}
Expand Down

0 comments on commit f1e1208

Please sign in to comment.