Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changing int to long return type to avoid useless casts.
- Loading branch information
1 parent
9ef0a8c
commit dddda84
Showing
2 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
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
151 changes: 151 additions & 0 deletions
151
graylog2-server/src/test/java/org/graylog2/agents/AgentServiceImplTest.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,151 @@ | ||
package org.graylog2.agents; | ||
|
||
import com.lordofthejars.nosqlunit.annotation.CustomComparisonStrategy; | ||
import com.lordofthejars.nosqlunit.annotation.IgnorePropertyValue; | ||
import com.lordofthejars.nosqlunit.annotation.ShouldMatchDataSet; | ||
import com.lordofthejars.nosqlunit.annotation.UsingDataSet; | ||
import com.lordofthejars.nosqlunit.core.LoadStrategyEnum; | ||
import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb; | ||
import com.lordofthejars.nosqlunit.mongodb.MongoFlexibleComparisonStrategy; | ||
import org.graylog2.bindings.ServerObjectMapperModule; | ||
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider; | ||
import org.graylog2.database.MongoConnectionRule; | ||
import org.joda.time.DateTime; | ||
import org.jukito.JukitoRunner; | ||
import org.jukito.UseModules; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.List; | ||
|
||
import static com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder.newInMemoryMongoDbRule; | ||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(JukitoRunner.class) | ||
@UseModules(ServerObjectMapperModule.class) | ||
@CustomComparisonStrategy(comparisonStrategy = MongoFlexibleComparisonStrategy.class) | ||
public class AgentServiceImplTest { | ||
@ClassRule | ||
public static final InMemoryMongoDb IN_MEMORY_MONGO_DB = newInMemoryMongoDbRule().build(); | ||
|
||
@Rule | ||
public MongoConnectionRule mongoRule = MongoConnectionRule.build("test"); | ||
|
||
private AgentService agentService; | ||
|
||
@Before | ||
public void setUp(MongoJackObjectMapperProvider mapperProvider) throws Exception { | ||
this.agentService = new AgentServiceImpl(mongoRule.getMongoConnection(), mapperProvider); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
public void testCountEmptyCollection() throws Exception { | ||
final long result = this.agentService.count(); | ||
|
||
assertEquals(0, result); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testCountNonEmptyCollection() throws Exception { | ||
final long result = this.agentService.count(); | ||
|
||
assertEquals(3, result); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
@ShouldMatchDataSet(location = "agentsSingleDataset.json") | ||
@IgnorePropertyValue(properties = {"_id", "last_seen"}) | ||
public void testSaveFirstRecord() throws Exception { | ||
final Agent agent = AgentImpl.create("agentId", "nodeId", AgentNodeDetails.create("DummyOS 1.0"), DateTime.now()); | ||
|
||
final Agent result = this.agentService.save(agent); | ||
|
||
assertNotNull(result); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testAll() throws Exception { | ||
final List<Agent> agents = this.agentService.all(); | ||
|
||
assertNotNull(agents); | ||
assertEquals(3, agents.size()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
public void testAllEmptyCollection() throws Exception { | ||
final List<Agent> agents = this.agentService.all(); | ||
|
||
assertNotNull(agents); | ||
assertEquals(0, agents.size()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testFindById() throws Exception { | ||
final String agent1id = "agent1id"; | ||
|
||
final Agent agent = this.agentService.findById(agent1id); | ||
|
||
assertNotNull(agent); | ||
assertEquals(agent1id, agent.getId()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testFindByIdNonexisting() throws Exception { | ||
final String agent1id = "nonexisting"; | ||
|
||
final Agent agent = this.agentService.findById(agent1id); | ||
|
||
assertNull(agent); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testFindByNodeId() throws Exception { | ||
final String nodeId = "uniqueid1"; | ||
|
||
final List<Agent> agents = this.agentService.findByNodeId(nodeId); | ||
|
||
assertNotNull(agents); | ||
assertEquals(1, agents.size()); | ||
|
||
for (Agent agent : agents) { | ||
assertNotNull(agent); | ||
assertEquals(nodeId, agent.getNodeId()); | ||
} | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testFindByNodeIdNonexisting() throws Exception { | ||
final String nodeId = "nonexisting"; | ||
|
||
final List<Agent> agents = this.agentService.findByNodeId(nodeId); | ||
|
||
assertNotNull(agents); | ||
assertEquals(0, agents.size()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "agentsMultipleDocuments.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
@ShouldMatchDataSet | ||
public void testDestroy() throws Exception { | ||
final Agent agent = mock(Agent.class); | ||
when(agent.getId()).thenReturn("agent2id"); | ||
|
||
final int result = this.agentService.destroy(agent); | ||
|
||
assertEquals(1, result); | ||
} | ||
} |