This repository has been archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c970890
commit 237aa96
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/test/java/com/depli/service/store/persistent/impl/JMXNodeServiceImplTest.java
This file contains 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
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)); | ||
} | ||
|
||
} |