-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #351: Service for automatic cleanup of activations with failed on…
…boarding (#403)
- Loading branch information
Showing
11 changed files
with
244 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...rc/main/java/com/wultra/app/onboardingserver/task/cleaning/ActivationCleaningService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* PowerAuth Enrollment Server | ||
* Copyright (C) 2022 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.wultra.app.onboardingserver.task.cleaning; | ||
|
||
import com.wultra.app.onboardingserver.common.database.OnboardingProcessRepository; | ||
import com.wultra.app.onboardingserver.common.database.entity.OnboardingProcessEntity; | ||
import com.wultra.app.onboardingserver.common.errorhandling.RemoteCommunicationException; | ||
import com.wultra.app.onboardingserver.impl.service.ActivationService; | ||
import com.wultra.security.powerauth.client.v3.ActivationStatus; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Service to cleaning activations. | ||
* | ||
* @author Lubos Racansky, lubos.racansky@wultra.com | ||
*/ | ||
@Service | ||
@Slf4j | ||
class ActivationCleaningService { | ||
|
||
private final OnboardingProcessRepository onboardingProcessRepository; | ||
|
||
private final ActivationService activationService; | ||
|
||
@Autowired | ||
public ActivationCleaningService( | ||
final OnboardingProcessRepository onboardingProcessRepository, | ||
final ActivationService activationService) { | ||
|
||
this.onboardingProcessRepository = onboardingProcessRepository; | ||
this.activationService = activationService; | ||
} | ||
|
||
/** | ||
* Cleanup activations of failed onboarding processes. | ||
*/ | ||
public void cleanupActivations() { | ||
onboardingProcessRepository.findProcessesToRemoveActivation() | ||
.forEach(this::cleanupActivation); | ||
} | ||
|
||
private void cleanupActivation(final OnboardingProcessEntity process) { | ||
final String activationId = process.getActivationId(); | ||
logger.info("Removing activation ID: {} of process ID: {}", activationId, process.getId()); | ||
|
||
try { | ||
removeActivation(activationId); | ||
process.setActivationRemoved(true); | ||
onboardingProcessRepository.save(process); | ||
} catch (RemoteCommunicationException e) { | ||
logger.error("Unable to remove activation ID: {}", activationId, e); | ||
} | ||
} | ||
|
||
private void removeActivation(String activationId) throws RemoteCommunicationException { | ||
final ActivationStatus activationStatus = activationService.fetchActivationStatus(activationId); | ||
if (activationStatus == ActivationStatus.REMOVED) { | ||
logger.debug("Activation ID: {} has been already removed", activationId); | ||
return; | ||
} | ||
|
||
activationService.removeActivation(activationId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...est/java/com/wultra/app/onboardingserver/task/cleaning/ActivationCleaningServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* PowerAuth Enrollment Server | ||
* Copyright (C) 2022 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.wultra.app.onboardingserver.task.cleaning; | ||
|
||
import com.wultra.app.onboardingserver.EnrollmentServerTestApplication; | ||
import com.wultra.app.onboardingserver.common.database.entity.OnboardingProcessEntity; | ||
import com.wultra.app.onboardingserver.common.errorhandling.RemoteCommunicationException; | ||
import com.wultra.app.onboardingserver.impl.service.ActivationService; | ||
import com.wultra.security.powerauth.client.v3.ActivationStatus; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import javax.persistence.EntityManager; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.util.AssertionErrors.assertFalse; | ||
import static org.springframework.test.util.AssertionErrors.assertTrue; | ||
|
||
/** | ||
* Test for {@link ActivationCleaningService}. | ||
* | ||
* @author Lubos Racansky, lubos.racansky@wultra.com | ||
*/ | ||
@SpringBootTest(classes = EnrollmentServerTestApplication.class) | ||
@ActiveProfiles("mock") | ||
@Transactional | ||
@Sql | ||
class ActivationCleaningServiceTest { | ||
|
||
@Autowired | ||
private ActivationCleaningService tested; | ||
|
||
@MockBean | ||
private ActivationService activationService; | ||
|
||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
@Test | ||
void testSuccessful() throws Exception { | ||
when(activationService.fetchActivationStatus("a2")) | ||
.thenReturn(ActivationStatus.ACTIVE); | ||
|
||
tested.cleanupActivations(); | ||
|
||
final OnboardingProcessEntity process = fetchOnboardingProcess("22222222-df91-4053-bb3d-3970979baf5d"); | ||
assertTrue("activation should be marked as removed", process.isActivationRemoved()); | ||
verify(activationService).removeActivation("a2"); | ||
} | ||
|
||
@Test | ||
void testAlreadyDeleted() throws Exception { | ||
when(activationService.fetchActivationStatus("a2")) | ||
.thenReturn(ActivationStatus.REMOVED); | ||
|
||
tested.cleanupActivations(); | ||
|
||
final OnboardingProcessEntity process = fetchOnboardingProcess("22222222-df91-4053-bb3d-3970979baf5d"); | ||
assertTrue("activation should be marked as removed", process.isActivationRemoved()); | ||
verify(activationService, never()).removeActivation("a2"); | ||
} | ||
|
||
@Test | ||
void testCommunicationException() throws Exception { | ||
when(activationService.fetchActivationStatus("a2")) | ||
.thenThrow(new RemoteCommunicationException("test exception")); | ||
|
||
tested.cleanupActivations(); | ||
|
||
final OnboardingProcessEntity process = fetchOnboardingProcess("22222222-df91-4053-bb3d-3970979baf5d"); | ||
assertFalse("activation should not be marked as removed", process.isActivationRemoved()); | ||
verify(activationService, never()).removeActivation("a2"); | ||
} | ||
|
||
private OnboardingProcessEntity fetchOnboardingProcess(final String id) { | ||
return entityManager.find(OnboardingProcessEntity.class, id); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...resources/com/wultra/app/onboardingserver/task/cleaning/ActivationCleaningServiceTest.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
INSERT INTO es_onboarding_process(id, identification_data, status, activation_id, activation_removed, error_score, timestamp_created) VALUES | ||
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 'a1', false, 0, now()), | ||
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'FAILED', 'a2', false, 0, now()), | ||
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'FAILED', 'a3', true, 0, now()); |