-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
046af86
commit fcbfd7c
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
graylog2-server/src/test/java/org/graylog2/database/filtering/SingleValueFilterTest.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,55 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
package org.graylog2.database.filtering; | ||
|
||
import com.mongodb.client.model.Filters; | ||
import org.bson.conversions.Bson; | ||
import org.graylog2.database.filtering.inmemory.InMemoryFilterable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class SingleValueFilterTest { | ||
|
||
@Test | ||
void testPredicateCreation() { | ||
final SingleValueFilter toTest = new SingleValueFilter("field", "value"); | ||
final Predicate<InMemoryFilterable> predicate = toTest.toPredicate(); | ||
|
||
assertTrue(predicate.test(createMock(Map.of("field", "value")))); | ||
assertFalse(predicate.test(createMock(Map.of()))); | ||
assertFalse(predicate.test(createMock(Map.of("field", "wrong_value")))); | ||
} | ||
|
||
@Test | ||
void testBsonCreation() { | ||
final SingleValueFilter toTest = new SingleValueFilter("field", "value"); | ||
final Bson bson = toTest.toBson(); | ||
|
||
assertEquals(Filters.eq("field", "value"), bson); | ||
} | ||
|
||
private InMemoryFilterable createMock(final Map<String, Object> fields) { | ||
return fieldName -> Optional.ofNullable(fields.get(fieldName)); | ||
} | ||
} |