-
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.
Each BackendQuery type knows which search types it supports. Changes …
…in validation.
- Loading branch information
1 parent
6152085
commit 8c2b061
Showing
12 changed files
with
215 additions
and
16 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
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
52 changes: 52 additions & 0 deletions
52
...graylog/plugins/views/search/engine/validation/SearchTypesMatchBackendQueryValidator.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,52 @@ | ||
/* | ||
* 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.graylog.plugins.views.search.engine.validation; | ||
|
||
import org.graylog.plugins.views.search.Query; | ||
import org.graylog.plugins.views.search.Search; | ||
import org.graylog.plugins.views.search.errors.SearchError; | ||
import org.graylog.plugins.views.search.errors.SearchTypeError; | ||
import org.graylog.plugins.views.search.permissions.SearchUser; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class SearchTypesMatchBackendQueryValidator implements SearchValidator { | ||
|
||
public static final String INVALID_SEARCH_TYPE_FOR_GIVEN_QUERY_TYPE_MSG = "Invalid search type for given query type"; | ||
|
||
@Override | ||
public Set<SearchError> validate(final Search search, | ||
final SearchUser searchUser) { | ||
return search.queries() | ||
.stream() | ||
.flatMap(query -> validate(query, searchUser).stream()) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public Set<SearchError> validate(final Query query, | ||
final SearchUser searchUser) { | ||
return query.searchTypes().stream() | ||
.filter(searchType -> query.query() | ||
.supportedSearchTypes() | ||
.stream() | ||
.anyMatch(supportedType -> !supportedType.isAssignableFrom(searchType.getClass()))) | ||
.map(searchType -> new SearchTypeError(query, searchType.id(), INVALID_SEARCH_TYPE_FOR_GIVEN_QUERY_TYPE_MSG)) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...er/src/main/java/org/graylog/plugins/views/search/searchtypes/SearchEngineSearchType.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,25 @@ | ||
/* | ||
* 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.graylog.plugins.views.search.searchtypes; | ||
|
||
import org.graylog.plugins.views.search.SearchType; | ||
|
||
/** | ||
* Marker interface for search types that are search engine related. | ||
*/ | ||
public interface SearchEngineSearchType extends SearchType { | ||
} |
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
103 changes: 103 additions & 0 deletions
103
...log/plugins/views/search/engine/validation/SearchTypesMatchBackendQueryValidatorTest.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,103 @@ | ||
/* | ||
* 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.graylog.plugins.views.search.engine.validation; | ||
|
||
import org.graylog.plugins.views.search.Query; | ||
import org.graylog.plugins.views.search.SearchType; | ||
import org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString; | ||
import org.graylog.plugins.views.search.errors.SearchError; | ||
import org.graylog.plugins.views.search.permissions.SearchUser; | ||
import org.graylog.plugins.views.search.searchtypes.MessageList; | ||
import org.graylog.plugins.views.search.searchtypes.pivot.Pivot; | ||
import org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.graylog.plugins.views.search.engine.validation.SearchTypesMatchBackendQueryValidator.INVALID_SEARCH_TYPE_FOR_GIVEN_QUERY_TYPE_MSG; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SearchTypesMatchBackendQueryValidatorTest { | ||
|
||
private SearchTypesMatchBackendQueryValidator toTest; | ||
@Mock | ||
private SearchUser searchUser; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
toTest = new SearchTypesMatchBackendQueryValidator(); | ||
} | ||
|
||
@Test | ||
void testValidationIsOkOnProperSearchTypes() { | ||
Query query = Query.builder() | ||
.query(ElasticsearchQueryString.of("bayobongo")) | ||
.searchTypes(Set.of( | ||
MessageList.builder() | ||
.id("messageListId") | ||
.build(), | ||
|
||
Pivot.builder() | ||
.rowGroups( | ||
Values.builder() | ||
.fields(List.of("id", "whatever")) | ||
.limit(10) | ||
.build() | ||
) | ||
.series(List.of()) | ||
.rollup(false) | ||
.build() | ||
)) | ||
.build(); | ||
|
||
assertEquals(Set.of(), toTest.validate(query, searchUser)); | ||
} | ||
|
||
@Test | ||
void testValidationDiscoversWrongSearchTypes() { | ||
final SearchType wrongSearchType = mock(SearchType.class); | ||
doReturn("wrong!").when(wrongSearchType).id(); | ||
Query query = Query.builder() | ||
.id("query_id") | ||
.query(ElasticsearchQueryString.of("bayobongo")) | ||
.searchTypes(Set.of( | ||
MessageList.builder() | ||
.id("messageListId") | ||
.build(), | ||
|
||
wrongSearchType | ||
)) | ||
.build(); | ||
|
||
final Set<SearchError> errors = toTest.validate(query, searchUser); | ||
assertThat(errors) | ||
.isNotNull() | ||
.hasSize(1); | ||
assertThat(errors.stream().findFirst().get().description()) | ||
.isEqualTo(INVALID_SEARCH_TYPE_FOR_GIVEN_QUERY_TYPE_MSG); | ||
|
||
} | ||
} |