com.pw.ism.heartbeat.InMemoryHeartbeatRepositoryTest.java Source code

Java tutorial

Introduction

Here is the source code for com.pw.ism.heartbeat.InMemoryHeartbeatRepositoryTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pw.ism.heartbeat;

import com.pw.ism.DevMvcConfiguration;
import com.pw.ism.TestMvcConfiguration;
import com.pw.ism.WebMvcConfiguration;
import java.time.Instant;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.Assert;

/**
 *
 * @author NRS
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(TestMvcConfiguration.class)
public class InMemoryHeartbeatRepositoryTest {

    @Autowired
    private InMemoryHeartbeatRepository repo;

    @Before
    public void setUp() {
        Heartbeat hb1 = new Heartbeat();
        hb1.setCustomer("TEST1");
        hb1.setNetwork("NETWORK1");
    }

    @Test
    public void shouldAddHeartBeat() throws Exception {
        Heartbeat hb1 = new Heartbeat();
        hb1.setCustomer("TEST1");
        hb1.setNetwork("NETWORK1");

        Heartbeat hb2 = new Heartbeat();
        hb2.setCustomer("CUST2");
        hb2.setNetwork("NETWORK2");

        repo.addHeartbeat(hb1, Instant.now().minusSeconds(5));
        repo.addHeartbeat(hb2, Instant.now().minusSeconds(2));

        List<Heartbeat> list = repo.getExpiredHeartbeats(6);
        Assert.isTrue(list.isEmpty());

        list = repo.getExpiredHeartbeats(3);
        Assert.isTrue(list.contains(hb1));

        list = repo.getExpiredHeartbeats(1);
        Assert.isTrue(list.contains(hb2));
        Assert.isTrue(list.contains(hb1));
    }

    @Test
    public void shouldRemoveHeartbeats() {
        Heartbeat hb1 = new Heartbeat();
        hb1.setCustomer("TEST1");
        hb1.setNetwork("NETWORK1");

        repo.addHeartbeat(hb1, Instant.now().minusSeconds(5));

        List<Heartbeat> list = repo.getExpiredHeartbeats(3);
        Assert.isTrue(list.contains(hb1));

        Heartbeat hb2 = new Heartbeat();
        hb2.setCustomer("TEST1");
        hb2.setNetwork("NETWORK1");

        Assert.isTrue(hb1.equals(hb2));
        repo.removeHeartbeat(hb2);

        list = repo.getExpiredHeartbeats(3);
        //Assert.isTrue(list.contains(hb1));
        Assert.isTrue(list.isEmpty());
    }

    @Test
    public void shouldRefreshHeartbeat() throws Exception {
        Heartbeat hb1 = new Heartbeat();
        hb1.setCustomer("TEST1");
        hb1.setNetwork("NETWORK1");

        repo.addHeartbeat(hb1, Instant.now().minusSeconds(5));

        List<Heartbeat> list = repo.getExpiredHeartbeats(3);
        Assert.isTrue(list.contains(hb1));

        Heartbeat hb2 = new Heartbeat();
        hb2.setCustomer("TEST1");
        hb2.setNetwork("NETWORK1");

        repo.addHeartbeat(hb2, Instant.now().minusSeconds(2));

        list = repo.getExpiredHeartbeats(3);
        Assert.isTrue(list.isEmpty());
    }
}