Do not process missing index types (#1985)

Fixes an error that arises, when a plugin with search
indices for repositories is removed and later on a repository
is deleted. This led to a null pointer exception in the
SearchableTypeResolver, but only because an "illegal" index
detail without proper type was given as a parameter. This
type cannot be resolved any longer due to the removed plugin.
We therefore can simply filter these index details.
This commit is contained in:
René Pfeuffer
2022-03-30 15:30:54 +02:00
committed by GitHub
parent 5505b3ce11
commit de9984ae2c
5 changed files with 31 additions and 53 deletions

View File

@@ -54,6 +54,7 @@ import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -341,6 +342,15 @@ class LuceneSearchEngineTest {
verify(enqueue.runAsAdmin()).enqueue(any(Task.class));
}
@Test
void shouldIgnoreDetailsWithMissingType() {
mockDetails(new LuceneIndexDetails(null, "default"));
searchEngine.forIndices().batch(index -> {});
verify(enqueue.runAsAdmin(), never()).enqueue(any(Task.class));
}
private <T extends IndexDetails> void mockDetails(LuceneIndexDetails... details) {
for (LuceneIndexDetails detail : details) {
mockType(detail.getType());
@@ -360,20 +370,19 @@ class LuceneSearchEngineTest {
}
private void mockType(Class<?> type){
LuceneSearchableType searchableType = mock(LuceneSearchableType.class);
lenient().when(searchableType.getType()).thenAnswer(ic -> type);
lenient().when(searchableType.getName()).thenReturn(type.getSimpleName().toLowerCase(Locale.ENGLISH));
lenient().when(resolver.resolve(type)).thenReturn(searchableType);
if (type != null) {
LuceneSearchableType searchableType = mock(LuceneSearchableType.class);
lenient().when(searchableType.getType()).thenAnswer(ic -> type);
lenient().when(searchableType.getName()).thenReturn(type.getSimpleName().toLowerCase(Locale.ENGLISH));
lenient().when(resolver.resolve(type)).thenReturn(searchableType);
}
}
}
public static class DummyIndexTask implements IndexTask<Repository> {
@Override
public void update(Index<Repository> index) {
}
}

View File

@@ -1,43 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.search;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.NotFoundException;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ExtendWith(MockitoExtension.class)
class SearchableTypeResolverTest {
private final SearchableTypeResolver resolver = new SearchableTypeResolver();
@Test
void shouldThrowNotFoundForNullValue() {
assertThrows(NotFoundException.class, () -> resolver.resolve(null));
}
}