Skip to content

Commit

Permalink
Changing int to long return type to avoid useless casts.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers committed Apr 1, 2015
1 parent 9ef0a8c commit dddda84
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

public interface AgentService {
int count();
long count();
Agent save(Agent agent);
List<Agent> all();
Agent findById(String id);
Expand Down
@@ -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);
}
}

0 comments on commit dddda84

Please sign in to comment.