Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic tests and fixtures for SavedSearchService.
Also moving out object creation from resource to service and removing reference to implementation class.
- Loading branch information
1 parent
2cc39c7
commit 6ae60b8
Showing
6 changed files
with
137 additions
and
11 deletions.
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
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
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
104 changes: 104 additions & 0 deletions
104
graylog2-server/src/test/java/org/graylog2/savedsearches/SavedSearchServiceImplTest.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,104 @@ | ||
package org.graylog2.savedsearches; | ||
|
||
import com.lordofthejars.nosqlunit.annotation.UsingDataSet; | ||
import com.lordofthejars.nosqlunit.core.LoadStrategyEnum; | ||
import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb; | ||
import org.graylog2.database.MongoConnectionRule; | ||
import org.graylog2.database.NotFoundException; | ||
import org.graylog2.plugin.Tools; | ||
import org.joda.time.DateTime; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder.newInMemoryMongoDbRule; | ||
import static org.junit.Assert.*; | ||
|
||
public class SavedSearchServiceImplTest { | ||
@ClassRule | ||
public static final InMemoryMongoDb IN_MEMORY_MONGO_DB = newInMemoryMongoDbRule().build(); | ||
|
||
@Rule | ||
public MongoConnectionRule mongoRule = MongoConnectionRule.build("test"); | ||
|
||
private SavedSearchService savedSearchService; | ||
|
||
@Before | ||
public void setUpService() throws Exception { | ||
this.savedSearchService = new SavedSearchServiceImpl(mongoRule.getMongoConnection()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
public void testAllEmptyCollection() throws Exception { | ||
final List<SavedSearch> savedSearches = this.savedSearchService.all(); | ||
|
||
assertNotNull("Returned list should not be null", savedSearches); | ||
assertEquals("Returned list should contain exactly one saved search", 0, savedSearches.size()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = "savedSearchSingleDocument.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testAllSingleDocumentInCollection() throws Exception { | ||
final List<SavedSearch> savedSearches = this.savedSearchService.all(); | ||
|
||
assertNotNull("Returned list should not be null", savedSearches); | ||
assertEquals("Returned list should contain exactly one saved search", 1, savedSearches.size()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = {"savedSearchSingleDocument.json", "savedSearchSingleDocument2.json"}, loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testAllMultipleDocumentsInCollection() throws Exception { | ||
final List<SavedSearch> savedSearches = this.savedSearchService.all(); | ||
|
||
assertNotNull("Returned list should not be null", savedSearches); | ||
assertEquals("Returned list should contain exactly one saved search", 2, savedSearches.size()); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(locations = {"savedSearchSingleDocument.json", "savedSearchSingleDocument2.json"}, loadStrategy = LoadStrategyEnum.CLEAN_INSERT) | ||
public void testLoad() throws Exception { | ||
final String id1 = "5400deadbeefdeadbeefaffe"; | ||
final SavedSearch savedSearch = savedSearchService.load(id1); | ||
|
||
assertNotNull("Should return a saved search", savedSearch); | ||
assertEquals("Should return the saved search with the correct id", id1, savedSearch.getId()); | ||
|
||
final String id2 = "54e3deadbeefdeadbeefaffe"; | ||
final SavedSearch savedSearch2 = savedSearchService.load(id2); | ||
|
||
assertNotNull("Should return a saved search", savedSearch2); | ||
assertEquals("Should return the saved search with the correct id", id2, savedSearch2.getId()); | ||
} | ||
|
||
@Test(expected = NotFoundException.class) | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
public void testLoadNonexisting() throws Exception { | ||
savedSearchService.load("54e3deadbeefdeadbeefaffe"); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
public void testLoadInvalidId() throws Exception { | ||
savedSearchService.load("foobar"); | ||
} | ||
|
||
@Test | ||
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) | ||
public void testCreate() throws Exception { | ||
final String title = "Example Title"; | ||
final Map<String, Object> query =Collections.emptyMap(); | ||
final String creatorUserId = "someuser"; | ||
final DateTime createdAt = Tools.iso8601(); | ||
|
||
final SavedSearch savedSearch = savedSearchService.create(title, query, creatorUserId, createdAt); | ||
|
||
assertNotNull("Should have returned a SavedSearch", savedSearch); | ||
assertEquals("Collection should still be empty", 0, savedSearchService.all().size()); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
graylog2-server/src/test/resources/org/graylog2/savedsearches/savedSearchSingleDocument.json
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,5 @@ | ||
{ | ||
"saved_searches" : [ | ||
{"_id" : {"$oid": "54e3deadbeefdeadbeefaffe"}, "title" : "Example Saved Search", "query" : {}, "creator_user_id" : "someuser", "created_at" : { "$date" : "2015-03-14T15:09:26.540Z" }} | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
...log2-server/src/test/resources/org/graylog2/savedsearches/savedSearchSingleDocument2.json
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,5 @@ | ||
{ | ||
"saved_searches" : [ | ||
{"_id" : {"$oid": "5400deadbeefdeadbeefaffe"}, "title" : "Another sample Saved Search", "query" : {}, "creator_user_id" : "someuser", "created_at" : { "$date" : "2015-03-14T15:09:26.540Z" }} | ||
] | ||
} |