Skip to content

Commit a8da9a1

Browse files
committed
SNAPSHOT
1 parent 689e95d commit a8da9a1

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.common.streams;
11+
12+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
13+
import org.elasticsearch.cluster.metadata.StreamsMetadata;
14+
import org.elasticsearch.test.ESTestCase;
15+
import org.junit.Before;
16+
17+
import java.util.Collections;
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
import static org.hamcrest.Matchers.sameInstance;
22+
import static org.mockito.Mockito.any;
23+
import static org.mockito.Mockito.eq;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
26+
27+
public class StreamsPermissionsUtilsTests extends ESTestCase {
28+
29+
private StreamsPermissionsUtils utils;
30+
private ProjectMetadata projectMetadataMock;
31+
private StreamsMetadata streamsMetadataMock;
32+
33+
@Before
34+
public void setup() {
35+
utils = StreamsPermissionsUtils.getInstance();
36+
projectMetadataMock = mock(ProjectMetadata.class);
37+
streamsMetadataMock = mock(StreamsMetadata.class);
38+
when(projectMetadataMock.custom(eq(StreamsMetadata.TYPE), any())).thenReturn(streamsMetadataMock);
39+
}
40+
41+
public void testGetInstanceReturnsSingleton() {
42+
StreamsPermissionsUtils instance1 = StreamsPermissionsUtils.getInstance();
43+
StreamsPermissionsUtils instance2 = StreamsPermissionsUtils.getInstance();
44+
assertThat(instance1, sameInstance(instance2));
45+
}
46+
47+
public void testStreamTypeIsEnabledReturnsTrueWhenLogsEnabled() {
48+
when(streamsMetadataMock.isLogsEnabled()).thenReturn(true);
49+
50+
boolean result = utils.streamTypeIsEnabled(StreamTypes.LOGS, projectMetadataMock);
51+
assertTrue(result);
52+
}
53+
54+
public void testStreamTypeIsEnabledReturnsFalseWhenLogsDisabled() {
55+
when(streamsMetadataMock.isLogsEnabled()).thenReturn(false);
56+
57+
boolean result = utils.streamTypeIsEnabled(StreamTypes.LOGS, projectMetadataMock);
58+
assertFalse(result);
59+
}
60+
61+
public void testIfRetrouteToSubstreamNotAllowedThrows() {
62+
when(streamsMetadataMock.isLogsEnabled()).thenReturn(true);
63+
64+
Set<String> indexHistory = new HashSet<>(); // empty, so reroute not allowed
65+
String destination = StreamTypes.LOGS.getStreamName() + ".substream";
66+
67+
IllegalArgumentException ex = expectThrows(
68+
IllegalArgumentException.class,
69+
() -> utils.throwIfRetrouteToSubstreamNotAllowed(projectMetadataMock, indexHistory, destination)
70+
);
71+
72+
assertTrue(ex.getMessage().contains("Cannot reroute to substream"));
73+
assertTrue(ex.getMessage().contains(destination));
74+
}
75+
76+
public void testThrowIfRetrouteToSubstreamNotAllowedDoesNotThrowWhenStreamTypeDisabled() {
77+
when(streamsMetadataMock.isLogsEnabled()).thenReturn(false);
78+
79+
Set<String> indexHistory = Collections.emptySet();
80+
String destination = StreamTypes.LOGS.getStreamName() + ".substream";
81+
82+
// Should not throw since stream type is disabled
83+
utils.throwIfRetrouteToSubstreamNotAllowed(projectMetadataMock, indexHistory, destination);
84+
}
85+
86+
public void testThrowIfRetrouteToSubstreamNotAllowedDoesNotThrowWhenDestinationNotSubstream() {
87+
when(streamsMetadataMock.isLogsEnabled()).thenReturn(true);
88+
89+
Set<String> indexHistory = Collections.emptySet();
90+
String destination = StreamTypes.LOGS.getStreamName(); // not a substream
91+
92+
// Should not throw since destination is not a substream
93+
utils.throwIfRetrouteToSubstreamNotAllowed(projectMetadataMock, indexHistory, destination);
94+
}
95+
96+
public void testThrowIfRetrouteToSubstreamNotAllowedDoesNotThrowWhenIndexHistoryContainsStream() {
97+
when(streamsMetadataMock.isLogsEnabled()).thenReturn(true);
98+
99+
Set<String> indexHistory = new HashSet<>();
100+
indexHistory.add(StreamTypes.LOGS.getStreamName());
101+
String destination = StreamTypes.LOGS.getStreamName() + ".substream";
102+
103+
// Should not throw since indexHistory contains the stream name
104+
utils.throwIfRetrouteToSubstreamNotAllowed(projectMetadataMock, indexHistory, destination);
105+
}
106+
}

0 commit comments

Comments
 (0)