Skip to content

Commit 94581db

Browse files
authored
Fix mocking in SyntheticSourceLicenseServiceTests (#118155) (#118179)
Some mock verifies where missing and `LicenseState#copyCurrentLicenseState(...)` wasn't always mocked. And because of incorrect mocking the testGoldOrPlatinumLicenseCustomCutoffDate() test had an incorrect assertion.
1 parent 69a3f84 commit 94581db

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

x-pack/plugin/logsdb/src/test/java/org/elasticsearch/xpack/logsdb/SyntheticSourceLicenseServiceTests.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void setup() throws Exception {
4141

4242
public void testLicenseAllowsSyntheticSource() {
4343
MockLicenseState licenseState = MockLicenseState.createMock();
44+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
4445
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(true);
4546
licenseService.setLicenseState(licenseState);
4647
licenseService.setLicenseService(mockLicenseService);
@@ -53,6 +54,7 @@ public void testLicenseAllowsSyntheticSource() {
5354

5455
public void testLicenseAllowsSyntheticSourceTemplateValidation() {
5556
MockLicenseState licenseState = MockLicenseState.createMock();
57+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
5658
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(true);
5759
licenseService.setLicenseState(licenseState);
5860
licenseService.setLicenseService(mockLicenseService);
@@ -65,6 +67,7 @@ public void testLicenseAllowsSyntheticSourceTemplateValidation() {
6567

6668
public void testDefaultDisallow() {
6769
MockLicenseState licenseState = MockLicenseState.createMock();
70+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
6871
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
6972
licenseService.setLicenseState(licenseState);
7073
licenseService.setLicenseService(mockLicenseService);
@@ -77,6 +80,7 @@ public void testDefaultDisallow() {
7780

7881
public void testFallback() {
7982
MockLicenseState licenseState = MockLicenseState.createMock();
83+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
8084
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(true);
8185
licenseService.setLicenseState(licenseState);
8286
licenseService.setLicenseService(mockLicenseService);
@@ -95,6 +99,7 @@ public void testGoldOrPlatinumLicense() throws Exception {
9599
when(mockLicenseService.getLicense()).thenReturn(license);
96100

97101
MockLicenseState licenseState = MockLicenseState.createMock();
102+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
98103
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
99104
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY))).thenReturn(true);
100105
licenseService.setLicenseState(licenseState);
@@ -103,6 +108,8 @@ public void testGoldOrPlatinumLicense() throws Exception {
103108
"legacy licensed usage is allowed, so not fallback to stored source",
104109
licenseService.fallbackToStoredSource(false, true)
105110
);
111+
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE));
112+
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY));
106113
Mockito.verify(licenseState, Mockito.times(1)).featureUsed(any());
107114
}
108115

@@ -112,6 +119,7 @@ public void testGoldOrPlatinumLicenseLegacyLicenseNotAllowed() throws Exception
112119
when(mockLicenseService.getLicense()).thenReturn(license);
113120

114121
MockLicenseState licenseState = MockLicenseState.createMock();
122+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
115123
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
116124
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
117125
licenseService.setLicenseState(licenseState);
@@ -125,14 +133,16 @@ public void testGoldOrPlatinumLicenseLegacyLicenseNotAllowed() throws Exception
125133
}
126134

127135
public void testGoldOrPlatinumLicenseBeyondCutoffDate() throws Exception {
128-
long start = LocalDateTime.of(2025, 1, 1, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
136+
long start = LocalDateTime.of(2025, 2, 5, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
129137
License license = createGoldOrPlatinumLicense(start);
130138
mockLicenseService = mock(LicenseService.class);
131139
when(mockLicenseService.getLicense()).thenReturn(license);
132140

133141
MockLicenseState licenseState = MockLicenseState.createMock();
142+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
134143
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
135144
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
145+
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY))).thenReturn(true);
136146
licenseService.setLicenseState(licenseState);
137147
licenseService.setLicenseService(mockLicenseService);
138148
assertTrue("beyond cutoff date, so fallback to stored source", licenseService.fallbackToStoredSource(false, true));
@@ -143,19 +153,21 @@ public void testGoldOrPlatinumLicenseBeyondCutoffDate() throws Exception {
143153
public void testGoldOrPlatinumLicenseCustomCutoffDate() throws Exception {
144154
licenseService = new SyntheticSourceLicenseService(Settings.EMPTY, "2025-01-02T00:00");
145155

146-
long start = LocalDateTime.of(2025, 1, 1, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
156+
long start = LocalDateTime.of(2025, 1, 3, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
147157
License license = createGoldOrPlatinumLicense(start);
148158
mockLicenseService = mock(LicenseService.class);
149159
when(mockLicenseService.getLicense()).thenReturn(license);
150160

151161
MockLicenseState licenseState = MockLicenseState.createMock();
162+
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
152163
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
164+
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
153165
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY))).thenReturn(true);
154166
licenseService.setLicenseState(licenseState);
155167
licenseService.setLicenseService(mockLicenseService);
156-
assertFalse("custom cutoff date, so fallback to stored source", licenseService.fallbackToStoredSource(false, true));
157-
Mockito.verify(licenseState, Mockito.times(1)).featureUsed(any());
158-
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY));
168+
assertTrue("custom cutoff date, so fallback to stored source", licenseService.fallbackToStoredSource(false, true));
169+
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE));
170+
Mockito.verify(licenseState, Mockito.never()).featureUsed(any());
159171
}
160172

161173
static License createEnterpriseLicense() throws Exception {

0 commit comments

Comments
 (0)