Skip to content

Commit 1a704fe

Browse files
committed
Merge branch 'fix-analytic-tests' of github.com:gchq/stroom into gh-4209_support_and_in_eval
2 parents 3b33207 + 84dc8d7 commit 1a704fe

File tree

8 files changed

+104
-24
lines changed

8 files changed

+104
-24
lines changed

stroom-app/src/test/java/stroom/analytics/AbstractAnalyticsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package stroom.analytics;
1818

19+
import stroom.analytics.impl.AnalyticHelper;
1920
import stroom.analytics.rule.impl.AnalyticRuleStore;
2021
import stroom.analytics.shared.AnalyticNotificationConfig;
2122
import stroom.analytics.shared.AnalyticNotificationDestinationType;
@@ -82,6 +83,8 @@ class AbstractAnalyticsTest extends StroomIntegrationTest {
8283
private AnalyticRuleStore analyticRuleStore;
8384
@Inject
8485
private AnalyticsDataSetup analyticsDataSetup;
86+
@Inject
87+
private AnalyticHelper analyticHelper;
8588

8689
@BeforeEach
8790
final void setup() {
@@ -107,6 +110,8 @@ final void setup() {
107110
if (analyticsDataSetup.getDetections() == null) {
108111
throw new NullPointerException("Detections missing");
109112
}
113+
114+
assertThat(analyticHelper.getRules().size()).isZero();
110115
}
111116

112117
@Override
@@ -125,6 +130,10 @@ protected DocRef writeRule(final AnalyticRuleDoc sample) {
125130
.analyticNotificationConfig(sample.getAnalyticNotificationConfig())
126131
.build();
127132
analyticRuleStore.writeDocument(analyticRuleDoc);
133+
134+
assertThat(analyticHelper.getRules().size()).isOne();
135+
assertThat(analyticHelper.getRules().getFirst().getUuid()).isEqualTo(alertRuleDocRef.getUuid());
136+
128137
return alertRuleDocRef;
129138
}
130139

stroom-app/src/test/java/stroom/analytics/AnalyticsDataSetup.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ public void addNewData(final LocalDateTime localDateTime) {
208208
.typeName(StreamTypeNames.RAW_EVENTS)
209209
.build();
210210
try (final Target target = store.openTarget(metaProperties)) {
211-
final Meta meta = target.getMeta();
212-
213211
try (final OutputStreamProvider outputStreamProvider = target.next()) {
214212
try (final InputStream inputStream =
215213
new ByteArrayInputStream(newData.getBytes(StandardCharsets.UTF_8));

stroom-index/stroom-index-impl/src/main/java/stroom/index/impl/IndexFieldCacheImpl.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import stroom.datasource.api.v2.IndexField;
2323
import stroom.docref.DocRef;
2424
import stroom.query.common.v2.IndexFieldCache;
25-
import stroom.query.common.v2.IndexFieldProvider;
25+
import stroom.query.common.v2.IndexFieldProviders;
2626
import stroom.security.api.SecurityContext;
2727
import stroom.security.shared.DocumentPermissionNames;
2828
import stroom.util.logging.LogUtil;
@@ -33,44 +33,33 @@
3333
import jakarta.inject.Provider;
3434
import jakarta.inject.Singleton;
3535

36-
import java.util.HashMap;
37-
import java.util.Map;
3836
import java.util.Objects;
39-
import java.util.Set;
4037

4138
@Singleton
4239
public class IndexFieldCacheImpl implements IndexFieldCache, Clearable {
4340

4441
private static final String CACHE_NAME = "Index Field Cache";
4542

46-
private final Map<String, IndexFieldProvider> providers = new HashMap<>();
43+
private final IndexFieldProviders indexFieldProviders;
4744
private final LoadingStroomCache<Key, IndexField> cache;
4845
private final SecurityContext securityContext;
4946

5047
@Inject
5148
IndexFieldCacheImpl(final CacheManager cacheManager,
5249
final Provider<IndexConfig> indexConfigProvider,
53-
final Set<IndexFieldProvider> indexFieldProviders,
50+
final IndexFieldProviders indexFieldProviders,
5451
final SecurityContext securityContext) {
52+
this.indexFieldProviders = indexFieldProviders;
5553
this.securityContext = securityContext;
56-
for (final IndexFieldProvider provider : indexFieldProviders) {
57-
providers.put(provider.getType(), provider);
58-
}
5954
cache = cacheManager.createLoadingCache(
6055
CACHE_NAME,
6156
() -> indexConfigProvider.get().getIndexFieldCache(),
6257
this::create);
6358
}
6459

6560
private IndexField create(final Key key) {
66-
return securityContext.asProcessingUserResult(() -> {
67-
final IndexFieldProvider provider = providers.get(key.docRef.getType());
68-
if (provider == null) {
69-
throw new NullPointerException("No provider can be found for: " + key.docRef.getType());
70-
}
71-
72-
return provider.getIndexField(key.docRef, key.fieldName);
73-
});
61+
return securityContext.asProcessingUserResult(() ->
62+
indexFieldProviders.getIndexField(key.docRef, key.fieldName));
7463
}
7564

7665
@Override
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017 Crown Copyright
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package stroom.index.impl;
19+
20+
import stroom.datasource.api.v2.IndexField;
21+
import stroom.docref.DocRef;
22+
import stroom.query.common.v2.IndexFieldProvider;
23+
import stroom.query.common.v2.IndexFieldProviders;
24+
import stroom.security.api.SecurityContext;
25+
import stroom.security.shared.DocumentPermissionNames;
26+
import stroom.util.logging.LogUtil;
27+
import stroom.util.shared.PermissionException;
28+
29+
import jakarta.inject.Inject;
30+
import jakarta.inject.Singleton;
31+
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.Objects;
35+
import java.util.Set;
36+
37+
@Singleton
38+
public class IndexFieldProvidersImpl implements IndexFieldProviders {
39+
40+
private final Map<String, IndexFieldProvider> providers = new HashMap<>();
41+
private final SecurityContext securityContext;
42+
43+
@Inject
44+
IndexFieldProvidersImpl(final Set<IndexFieldProvider> indexFieldProviders,
45+
final SecurityContext securityContext) {
46+
this.securityContext = securityContext;
47+
for (final IndexFieldProvider provider : indexFieldProviders) {
48+
providers.put(provider.getType(), provider);
49+
}
50+
}
51+
52+
@Override
53+
public IndexField getIndexField(final DocRef docRef, final String fieldName) {
54+
Objects.requireNonNull(docRef, "Null DocRef supplied");
55+
Objects.requireNonNull(docRef.getType(), "Null DocRef type supplied");
56+
Objects.requireNonNull(fieldName, "Null field name supplied");
57+
58+
if (!securityContext.hasDocumentPermission(docRef, DocumentPermissionNames.USE)) {
59+
throw new PermissionException(
60+
securityContext.getUserIdentityForAudit(),
61+
LogUtil.message("You are not authorised to read {}", docRef));
62+
}
63+
final IndexFieldProvider provider = providers.get(docRef.getType());
64+
if (provider == null) {
65+
throw new NullPointerException("No provider can be found for: " + docRef.getType());
66+
}
67+
68+
return provider.getIndexField(docRef, fieldName);
69+
}
70+
}

stroom-index/stroom-index-impl/src/main/java/stroom/index/impl/IndexModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import stroom.job.api.ScheduledJobsBinder;
2424
import stroom.lifecycle.api.LifecycleBinder;
2525
import stroom.query.common.v2.IndexFieldCache;
26+
import stroom.query.common.v2.IndexFieldProviders;
2627
import stroom.searchable.api.Searchable;
2728
import stroom.util.RunnableWrapper;
2829
import stroom.util.entityevent.EntityEvent;
@@ -45,6 +46,7 @@ protected void configure() {
4546

4647
bind(IndexShardWriterCache.class).to(IndexShardWriterCacheImpl.class);
4748
bind(LuceneIndexDocCache.class).to(LuceneIndexDocCacheImpl.class);
49+
bind(IndexFieldProviders.class).to(IndexFieldProvidersImpl.class);
4850
bind(IndexFieldCache.class).to(IndexFieldCacheImpl.class);
4951
bind(IndexStore.class).to(IndexStoreImpl.class);
5052
bind(IndexVolumeService.class).to(IndexVolumeServiceImpl.class);

stroom-index/stroom-index-mock/src/main/java/stroom/index/mock/MockIndexModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import stroom.importexport.api.ImportExportActionHandler;
2121
import stroom.index.impl.IndexElementModule;
2222
import stroom.index.impl.IndexFieldCacheImpl;
23+
import stroom.index.impl.IndexFieldProvidersImpl;
2324
import stroom.index.impl.IndexFieldService;
2425
import stroom.index.impl.IndexShardService;
2526
import stroom.index.impl.IndexShardWriterCache;
@@ -33,6 +34,7 @@
3334
import stroom.index.shared.LuceneIndexDoc;
3435
import stroom.query.common.v2.IndexFieldCache;
3536
import stroom.query.common.v2.IndexFieldProvider;
37+
import stroom.query.common.v2.IndexFieldProviders;
3638
import stroom.util.guice.GuiceUtil;
3739

3840
import com.google.inject.AbstractModule;
@@ -45,6 +47,7 @@ protected void configure() {
4547

4648
bind(IndexShardWriterCache.class).to(MockIndexShardWriterCache.class);
4749
bind(LuceneIndexDocCache.class).to(LuceneIndexDocCacheImpl.class);
50+
bind(IndexFieldProviders.class).to(IndexFieldProvidersImpl.class);
4851
bind(IndexFieldCache.class).to(IndexFieldCacheImpl.class);
4952
bind(IndexStore.class).to(IndexStoreImpl.class);
5053
bind(IndexVolumeService.class).to(MockIndexVolumeService.class);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package stroom.query.common.v2;
2+
3+
import stroom.datasource.api.v2.IndexField;
4+
import stroom.docref.DocRef;
5+
6+
public interface IndexFieldProviders {
7+
8+
IndexField getIndexField(DocRef docRef, String fieldName);
9+
}

stroom-view/stroom-view-impl/src/main/java/stroom/view/impl/ViewSearchProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import stroom.query.api.v2.SearchRequest;
1212
import stroom.query.api.v2.TableSettings;
1313
import stroom.query.common.v2.DataSourceProviderRegistry;
14-
import stroom.query.common.v2.IndexFieldCache;
1514
import stroom.query.common.v2.IndexFieldProvider;
15+
import stroom.query.common.v2.IndexFieldProviders;
1616
import stroom.query.common.v2.ResultStore;
1717
import stroom.query.common.v2.SearchProvider;
1818
import stroom.query.common.v2.StoreFactoryRegistry;
@@ -40,19 +40,19 @@ public class ViewSearchProvider implements SearchProvider, IndexFieldProvider {
4040
private final Provider<StoreFactoryRegistry> storeFactoryRegistryProvider;
4141
private final SecurityContext securityContext;
4242
private final Provider<DataSourceProviderRegistry> dataSourceProviderRegistry;
43-
private final IndexFieldCache indexFieldCache;
43+
private final IndexFieldProviders indexFieldProviders;
4444

4545
@Inject
4646
public ViewSearchProvider(final ViewStore viewStore,
4747
final Provider<StoreFactoryRegistry> storeFactoryRegistryProvider,
4848
final SecurityContext securityContext,
4949
final Provider<DataSourceProviderRegistry> dataSourceProviderRegistry,
50-
final IndexFieldCache indexFieldCache) {
50+
final IndexFieldProviders indexFieldProviders) {
5151
this.viewStore = viewStore;
5252
this.storeFactoryRegistryProvider = storeFactoryRegistryProvider;
5353
this.securityContext = securityContext;
5454
this.dataSourceProviderRegistry = dataSourceProviderRegistry;
55-
this.indexFieldCache = indexFieldCache;
55+
this.indexFieldProviders = indexFieldProviders;
5656
}
5757

5858
private DocRef getReferencedDataSource(final DocRef viewDocRef) {
@@ -91,7 +91,7 @@ public ResultPage<QueryField> getFieldInfo(final FindFieldInfoCriteria criteria)
9191
public IndexField getIndexField(final DocRef viewDocRef, final String fieldName) {
9292
final DocRef docRef = getReferencedDataSource(viewDocRef);
9393
if (docRef != null) {
94-
return indexFieldCache.get(docRef, fieldName);
94+
return indexFieldProviders.getIndexField(docRef, fieldName);
9595
}
9696
return null;
9797
}

0 commit comments

Comments
 (0)