Created
April 17, 2018 08:49
-
-
Save mobynote/b964f35de5312e00ce0fdd4277aa087a to your computer and use it in GitHub Desktop.
UT Example - mockito fake, list.sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobynote.listener; | |
import com.mobynote.dto.HealthValue; | |
import com.mobynote.entity.DeviceErrorLog; | |
import com.mobynote.listener.event.DeviceErrorLogEvent; | |
import com.mobynote.repository.DeviceErrorLogRepository; | |
import com.mobynote.util.CommonUtils; | |
import org.apache.commons.lang.StringUtils; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.InjectMocks; | |
import org.mockito.Spy; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.util.Comparator; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Vector; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.Collectors; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.Matchers.*; | |
@RunWith(MockitoJUnitRunner.class) | |
public class MonitorListenerTest { | |
private List<DeviceErrorLog> logs = new Vector<>(); | |
@InjectMocks | |
private MonitorListener listener; | |
@Spy | |
private FakeDeviceErrorLogRepository repository; | |
@Before | |
public void setUp() { | |
repository.deleteAllInBatch(); | |
} | |
@Test | |
public void should_save_error_log_when_given_error_event() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("1", "1", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(1)); | |
assertThat(errorLogs.get(0).getDeviceId(), equalTo(deviceErrorLog.getDeviceId())); | |
assertThat(errorLogs.get(0).getErrorCode(), equalTo(deviceErrorLog.getErrorCode())); | |
assertThat(errorLogs.get(0).getErrorMsg(), equalTo(deviceErrorLog.getErrorMsg())); | |
assertThat(errorLogs.get(0).getStatusCode(), equalTo(deviceErrorLog.getStatusCode())); | |
} | |
@Test | |
public void should_not_save_error_log_when_health_is_up() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("0", "0", HealthValue.UP); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(0)); | |
} | |
@Test | |
public void should_do_not_save_error_log_when_error_log_exists() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("1", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
DeviceErrorLog eventEntity = this.createErrorLog("1", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, eventEntity)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(1)); | |
assertThat(errorLogs.get(0).getDeviceId(), equalTo(deviceErrorLog.getDeviceId())); | |
assertThat(errorLogs.get(0).getCreateTime(), equalTo(deviceErrorLog.getCreateTime())); | |
assertThat(errorLogs.get(0).getErrorCode(), equalTo(deviceErrorLog.getErrorCode())); | |
assertThat(errorLogs.get(0).getErrorMsg(), equalTo(deviceErrorLog.getErrorMsg())); | |
assertThat(errorLogs.get(0).getStatusCode(), equalTo(deviceErrorLog.getStatusCode())); | |
} | |
@Test | |
public void should_do_not_save_error_log_when_health_is_up() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("1", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
DeviceErrorLog eventEntity = this.createErrorLog("0", "0", HealthValue.UP); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, eventEntity)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(1)); | |
assertThat(errorLogs.get(0).getDeviceId(), equalTo(deviceErrorLog.getDeviceId())); | |
assertThat(errorLogs.get(0).getCreateTime(), equalTo(deviceErrorLog.getCreateTime())); | |
assertThat(errorLogs.get(0).getRecoveryTime(), notNullValue()); | |
assertThat(errorLogs.get(0).getErrorCode(), equalTo(deviceErrorLog.getErrorCode())); | |
assertThat(errorLogs.get(0).getErrorMsg(), equalTo(deviceErrorLog.getErrorMsg())); | |
assertThat(errorLogs.get(0).getStatusCode(), equalTo(deviceErrorLog.getStatusCode())); | |
} | |
@Test | |
public void should_save_error_log_when_error_log_exists_but_status_not_equal() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("1", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
DeviceErrorLog eventEntity = this.createErrorLog("2", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, eventEntity)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(2)); | |
assertThat(errorLogs.get(0).getDeviceId(), equalTo(deviceErrorLog.getDeviceId())); | |
assertThat(errorLogs.get(0).getCreateTime(), is(eventEntity.getCreateTime())); | |
assertThat(errorLogs.get(0).getErrorCode(), equalTo(eventEntity.getErrorCode())); | |
assertThat(errorLogs.get(0).getErrorMsg(), equalTo(eventEntity.getErrorMsg())); | |
assertThat(errorLogs.get(0).getStatusCode(), equalTo(eventEntity.getStatusCode())); | |
assertThat(errorLogs.get(1).getCreateTime(), is(deviceErrorLog.getCreateTime())); | |
assertThat(errorLogs.get(1).getRecoveryTime(), notNullValue()); | |
assertThat(errorLogs.get(1).getErrorCode(), equalTo(deviceErrorLog.getErrorCode())); | |
assertThat(errorLogs.get(1).getErrorMsg(), equalTo(deviceErrorLog.getErrorMsg())); | |
assertThat(errorLogs.get(1).getStatusCode(), equalTo(deviceErrorLog.getStatusCode())); | |
} | |
@Test | |
public void should_save_error_log_when_error_log_exists_but_error_not_equal() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("0", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
DeviceErrorLog eventEntity = this.createErrorLog("0", "1", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, eventEntity)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(2)); | |
assertThat(errorLogs.get(0).getDeviceId(), equalTo(deviceErrorLog.getDeviceId())); | |
assertThat(errorLogs.get(0).getCreateTime(), is(eventEntity.getCreateTime())); | |
assertThat(errorLogs.get(0).getErrorCode(), equalTo(eventEntity.getErrorCode())); | |
assertThat(errorLogs.get(0).getErrorMsg(), equalTo(eventEntity.getErrorMsg())); | |
assertThat(errorLogs.get(0).getStatusCode(), equalTo(eventEntity.getStatusCode())); | |
assertThat(errorLogs.get(1).getCreateTime(), is(deviceErrorLog.getCreateTime())); | |
assertThat(errorLogs.get(1).getRecoveryTime(), notNullValue()); | |
assertThat(errorLogs.get(1).getErrorCode(), equalTo(deviceErrorLog.getErrorCode())); | |
assertThat(errorLogs.get(1).getErrorMsg(), equalTo(deviceErrorLog.getErrorMsg())); | |
assertThat(errorLogs.get(1).getStatusCode(), equalTo(deviceErrorLog.getStatusCode())); | |
} | |
@Test | |
public void should_save_error_log_when_error_log_exists_but_error_not_equal_and_status_not_equal() { | |
DeviceErrorLog deviceErrorLog = this.createErrorLog("0", "0", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, deviceErrorLog)); | |
DeviceErrorLog eventEntity = this.createErrorLog("1", "1", HealthValue.DOWN); | |
listener.saveDeviceErrorLog(new DeviceErrorLogEvent(this, eventEntity)); | |
List<DeviceErrorLog> errorLogs = repository.findByDeviceIdOrderByCreateTimeDesc("deviceId"); | |
assertThat(errorLogs.size(), equalTo(2)); | |
assertThat(errorLogs.get(0).getDeviceId(), equalTo(deviceErrorLog.getDeviceId())); | |
assertThat(errorLogs.get(0).getCreateTime(), is(eventEntity.getCreateTime())); | |
assertThat(errorLogs.get(0).getErrorCode(), equalTo(eventEntity.getErrorCode())); | |
assertThat(errorLogs.get(0).getErrorMsg(), equalTo(eventEntity.getErrorMsg())); | |
assertThat(errorLogs.get(0).getStatusCode(), equalTo(eventEntity.getStatusCode())); | |
assertThat(errorLogs.get(1).getCreateTime(), is(deviceErrorLog.getCreateTime())); | |
assertThat(errorLogs.get(1).getRecoveryTime(), notNullValue()); | |
assertThat(errorLogs.get(1).getErrorCode(), equalTo(deviceErrorLog.getErrorCode())); | |
assertThat(errorLogs.get(1).getErrorMsg(), equalTo(deviceErrorLog.getErrorMsg())); | |
assertThat(errorLogs.get(1).getStatusCode(), equalTo(deviceErrorLog.getStatusCode())); | |
} | |
private DeviceErrorLog createErrorLog(String status, String error, HealthValue healthValue) { | |
DeviceErrorLog deviceErrorLog = new DeviceErrorLog(); | |
deviceErrorLog.setDeviceId("deviceId"); | |
deviceErrorLog.setCreateTime(new Date()); | |
deviceErrorLog.setProductNo("productNo"); | |
deviceErrorLog.setStatusCode(status); | |
deviceErrorLog.setErrorCode(error); | |
deviceErrorLog.setErrorMsg("Not Active."); | |
deviceErrorLog.setHealth(healthValue); | |
return deviceErrorLog; | |
} | |
abstract class FakeDeviceErrorLogRepository implements DeviceErrorLogRepository { | |
@Override | |
public <S extends DeviceErrorLog> S save(S entity) { | |
if (StringUtils.isNotBlank(entity.getId())) { | |
logs = logs.stream().filter(e -> !e.getId().equals(entity.getId())).collect(Collectors.toList()); | |
} else { | |
entity.setId(CommonUtils.genUUID()); | |
} | |
logs.add(entity); | |
return entity; | |
} | |
@Override | |
public List<DeviceErrorLog> findByDeviceIdOrderByCreateTimeDesc(String deviceId) { | |
logs = logs.stream().filter(e -> e.getDeviceId().equals(deviceId)).collect(Collectors.toList()); | |
logs.sort(Comparator.comparing( | |
DeviceErrorLog::getCreateTime, | |
Comparator.nullsLast(Comparator.naturalOrder())) | |
.reversed()); | |
/*logs.sort(new CustomComparator());*/ | |
try { | |
TimeUnit.MILLISECONDS.sleep(10); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return logs; | |
} | |
@Override | |
public void deleteAllInBatch() { | |
logs = new Vector<>(); | |
} | |
} | |
public class CustomComparator implements Comparator<DeviceErrorLog> { | |
@Override | |
public int compare(DeviceErrorLog o1, DeviceErrorLog o2) { | |
if (o1.getCreateTime() == null) { | |
return (o2.getCreateTime() == null) ? 0 : -1; | |
} | |
if (o2.getCreateTime() == null) { | |
return 1; | |
} | |
LocalDateTime d1 = LocalDateTime.ofInstant(o1.getCreateTime().toInstant(), ZoneId.systemDefault()); | |
LocalDateTime d2 = LocalDateTime.ofInstant(o2.getCreateTime().toInstant(), ZoneId.systemDefault()); | |
return d2.compareTo(d1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment