Skip to content

Commit

Permalink
Fix mocking in SyntheticSourceLicenseServiceTests (elastic#118155)
Browse files Browse the repository at this point in the history
Some mock verifies where missing and `LicenseState#copyCurrentLicenseState(...)` wasn't always mocked.

And because of incorrect mocking the testGoldOrPlatinumLicenseCustomCutoffDate() test had an incorrect assertion.
  • Loading branch information
martijnvg committed Dec 6, 2024
1 parent 69a3f84 commit d42f418
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void setup() throws Exception {

public void testLicenseAllowsSyntheticSource() {
MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(true);
licenseService.setLicenseState(licenseState);
licenseService.setLicenseService(mockLicenseService);
Expand All @@ -53,6 +54,7 @@ public void testLicenseAllowsSyntheticSource() {

public void testLicenseAllowsSyntheticSourceTemplateValidation() {
MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(true);
licenseService.setLicenseState(licenseState);
licenseService.setLicenseService(mockLicenseService);
Expand All @@ -65,6 +67,7 @@ public void testLicenseAllowsSyntheticSourceTemplateValidation() {

public void testDefaultDisallow() {
MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
licenseService.setLicenseState(licenseState);
licenseService.setLicenseService(mockLicenseService);
Expand All @@ -77,6 +80,7 @@ public void testDefaultDisallow() {

public void testFallback() {
MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(true);
licenseService.setLicenseState(licenseState);
licenseService.setLicenseService(mockLicenseService);
Expand All @@ -95,6 +99,7 @@ public void testGoldOrPlatinumLicense() throws Exception {
when(mockLicenseService.getLicense()).thenReturn(license);

MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY))).thenReturn(true);
licenseService.setLicenseState(licenseState);
Expand All @@ -103,6 +108,8 @@ public void testGoldOrPlatinumLicense() throws Exception {
"legacy licensed usage is allowed, so not fallback to stored source",
licenseService.fallbackToStoredSource(false, true)
);
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE));
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY));
Mockito.verify(licenseState, Mockito.times(1)).featureUsed(any());
}

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

MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
licenseService.setLicenseState(licenseState);
Expand All @@ -125,14 +133,16 @@ public void testGoldOrPlatinumLicenseLegacyLicenseNotAllowed() throws Exception
}

public void testGoldOrPlatinumLicenseBeyondCutoffDate() throws Exception {
long start = LocalDateTime.of(2025, 1, 1, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
long start = LocalDateTime.of(2025, 2, 5, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
License license = createGoldOrPlatinumLicense(start);
mockLicenseService = mock(LicenseService.class);
when(mockLicenseService.getLicense()).thenReturn(license);

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

long start = LocalDateTime.of(2025, 1, 1, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
long start = LocalDateTime.of(2025, 1, 3, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli();
License license = createGoldOrPlatinumLicense(start);
mockLicenseService = mock(LicenseService.class);
when(mockLicenseService.getLicense()).thenReturn(license);

MockLicenseState licenseState = MockLicenseState.createMock();
when(licenseState.copyCurrentLicenseState()).thenReturn(licenseState);
when(licenseState.getOperationMode()).thenReturn(license.operationMode());
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE))).thenReturn(false);
when(licenseState.isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY))).thenReturn(true);
licenseService.setLicenseState(licenseState);
licenseService.setLicenseService(mockLicenseService);
assertFalse("custom cutoff date, so fallback to stored source", licenseService.fallbackToStoredSource(false, true));
Mockito.verify(licenseState, Mockito.times(1)).featureUsed(any());
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY));
assertTrue("custom cutoff date, so fallback to stored source", licenseService.fallbackToStoredSource(false, true));
Mockito.verify(licenseState, Mockito.times(1)).isAllowed(same(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE));
Mockito.verify(licenseState, Mockito.never()).featureUsed(any());
}

static License createEnterpriseLicense() throws Exception {
Expand Down

0 comments on commit d42f418

Please sign in to comment.