Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

Commit

Permalink
fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoperillo committed Oct 2, 2017
1 parent c970890 commit 237aa96
Showing 1 changed file with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.depli.service.store.persistent.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.depli.store.persistent.entity.JMXNode;
import com.depli.store.persistent.repository.JMXNodeRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JMXNodeServiceImplTest {

@Autowired
private JMXNodeServiceImpl nodeServiceImpl;

@MockBean
private JMXNodeRepository jmxNodeRepository;

@Test
public void findTwoNodes() {
Mockito.when(jmxNodeRepository.findAll()).then(new Answer<List<JMXNode>>() {
@Override
public List<JMXNode> answer(InvocationOnMock invocation) throws Throwable {
return Arrays.asList(new JMXNode("node1", "host1", 1111, false),
new JMXNode("node2", "host2", 2222, false));
}
});


List<JMXNode> nodes = nodeServiceImpl.findAll();
Assert.assertThat(nodes, Matchers.hasSize(2));
}

@Test
public void findNodeWithId1() {
Mockito.when(jmxNodeRepository.findByNodeId(1L)).then(new Answer<JMXNode>() {
@Override
public JMXNode answer(InvocationOnMock invocation) throws Throwable {
return new JMXNode("node1", "host1", 1111, false);
}
});


JMXNode node = nodeServiceImpl.findByNodeId(1L);
Assert.assertThat(node.getPort(), Matchers.is((1111)));
Assert.assertThat(node.getHostname(), Matchers.is(("host1")));
Assert.assertThat(node.getUsername(), Matchers.isEmptyOrNullString());
}


@Test
public void updateNodeWithId1() {
JMXNode node = new JMXNode("node1", "host1", 1111, false);
Mockito.when(jmxNodeRepository.existsByNodeId(1L)).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return true;
}
});

boolean result = nodeServiceImpl.updateByNodeId(1L, node);
Assert.assertThat(result, Matchers.is(true));
}

@Test
public void removeNodewithId1() {
Mockito.when(jmxNodeRepository.existsByNodeId(1L)).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return true;
}
});

boolean result = nodeServiceImpl.removeByNodeId(1L);
Assert.assertThat(result, Matchers.is(true));
}

@Test
public void saveANode() throws IOException {
JMXNode node = new JMXNode("node1", "host1", 1111, false);
Mockito.when(jmxNodeRepository.save(node)).then(new Answer<JMXNode>() {
@Override
public JMXNode answer(InvocationOnMock invocation) throws Throwable {
node.setNodeId(1L);
return node;
}
});

boolean result = nodeServiceImpl.save(node);
Assert.assertThat(result, Matchers.is(true));
}

}

0 comments on commit 237aa96

Please sign in to comment.