FINERACT-1744 - Idempotency support
- [x] Idempotency write move to filters - [x] Idempotency integration test with success and failure - [x] Added response status code to database - [x] SynchronousCommandProcessingService not generate the command, just marked to the filter - [x] IdempotencyStoreFilter store the command request if available. - [x] Removed JpaExceptionHandler.java
This commit is contained in:
committed by
Arnold Gálovics
parent
25c0b6a08d
commit
5210bf7412
+111
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.fineract.commands.service;
|
||||
|
||||
import static org.apache.fineract.commands.domain.CommandProcessingResultType.UNDER_PROCESSING;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.Optional;
|
||||
import org.apache.fineract.batch.exception.ErrorInfo;
|
||||
import org.apache.fineract.commands.domain.CommandSource;
|
||||
import org.apache.fineract.commands.domain.CommandSourceRepository;
|
||||
import org.apache.fineract.commands.domain.CommandWrapper;
|
||||
import org.apache.fineract.infrastructure.codes.exception.CodeNotFoundException;
|
||||
import org.apache.fineract.infrastructure.core.api.JsonCommand;
|
||||
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
|
||||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
||||
import org.apache.fineract.useradministration.domain.AppUser;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
public class CommandSourceServiceTest {
|
||||
|
||||
@Mock
|
||||
private CommandSourceRepository commandSourceRepository;
|
||||
|
||||
@InjectMocks
|
||||
private CommandSourceService underTest;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
ThreadLocalContextUtil.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateFromWrapper() {
|
||||
CommandWrapper wrapper = CommandWrapper.wrap("act", "ent", 1L, 1L);
|
||||
JsonCommand jsonCommand = JsonCommand.from("{}");
|
||||
AppUser appUser = Mockito.mock(AppUser.class);
|
||||
|
||||
FineractPlatformTenant ft = new FineractPlatformTenant(1L, "t1", "n1", ZoneId.systemDefault().toString(), null);
|
||||
ThreadLocalContextUtil.setTenant(ft);
|
||||
|
||||
String idk = "idk";
|
||||
underTest.saveInitial(wrapper, jsonCommand, appUser, idk);
|
||||
|
||||
ArgumentCaptor<CommandSource> commandSourceArgumentCaptor = ArgumentCaptor.forClass(CommandSource.class);
|
||||
Mockito.verify(commandSourceRepository).saveAndFlush(commandSourceArgumentCaptor.capture());
|
||||
|
||||
CommandSource captured = commandSourceArgumentCaptor.getValue();
|
||||
Assertions.assertEquals(idk, captured.getIdempotencyKey());
|
||||
Assertions.assertEquals(UNDER_PROCESSING.getValue(), captured.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateFromExisting() {
|
||||
CommandWrapper wrapper = CommandWrapper.wrap("act", "ent", 1L, 1L);
|
||||
long commandId = 1L;
|
||||
JsonCommand jsonCommand = JsonCommand.fromExistingCommand(commandId, "", null, null, null, 1L, null, null, null, null, null, null,
|
||||
null, null, null, null, null);
|
||||
CommandSource commandMock = Mockito.mock(CommandSource.class);
|
||||
Mockito.when(commandSourceRepository.saveAndFlush(commandMock)).thenReturn(commandMock);
|
||||
Mockito.when(commandSourceRepository.findById(commandId)).thenReturn(Optional.of(commandMock));
|
||||
AppUser appUser = Mockito.mock(AppUser.class);
|
||||
|
||||
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "t1", "n1", ZoneId.systemDefault().toString(), null));
|
||||
|
||||
CommandSource actual = underTest.saveInitial(wrapper, jsonCommand, appUser, "idk");
|
||||
|
||||
ArgumentCaptor<CommandSource> commandSourceArgumentCaptor = ArgumentCaptor.forClass(CommandSource.class);
|
||||
Mockito.verify(commandSourceRepository).saveAndFlush(commandSourceArgumentCaptor.capture());
|
||||
|
||||
CommandSource captured = commandSourceArgumentCaptor.getValue();
|
||||
Assertions.assertEquals(actual, captured);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateErrorException() {
|
||||
ErrorInfo result = underTest.generateErrorException(new CodeNotFoundException("foo"));
|
||||
Assertions.assertEquals(404, result.getStatusCode());
|
||||
Assertions.assertEquals(1001, result.getErrorCode());
|
||||
Assertions.assertTrue(result.getMessage().contains("Code with name `foo` does not exist"));
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.fineract.commands.service;
|
||||
|
||||
import org.apache.fineract.commands.domain.CommandWrapper;
|
||||
import org.apache.fineract.infrastructure.core.config.FineractProperties;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
public class IdempotencyKeyResolverTest {
|
||||
|
||||
@Mock
|
||||
private IdempotencyKeyGenerator idempotencyKeyGenerator;
|
||||
|
||||
@Mock
|
||||
private FineractProperties fineractProperties;
|
||||
|
||||
@InjectMocks
|
||||
private IdempotencyKeyResolver underTest;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIPKResolveFromRequest() {
|
||||
String idkh = "foo";
|
||||
String idk = "bar";
|
||||
Mockito.when(fineractProperties.getIdempotencyKeyHeaderName()).thenReturn(idkh);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader(idkh, idk);
|
||||
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
CommandWrapper wrapper = CommandWrapper.wrap("act", "ent", 1L, 1L);
|
||||
String resolvedIdk = underTest.resolve(wrapper);
|
||||
Assertions.assertEquals(idk, resolvedIdk);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIPKResolveFromGenerate() {
|
||||
String idk = "idk";
|
||||
Mockito.when(idempotencyKeyGenerator.create()).thenReturn(idk);
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
CommandWrapper wrapper = CommandWrapper.wrap("act", "ent", 1L, 1L);
|
||||
String resolvedIdk = underTest.resolve(wrapper);
|
||||
Assertions.assertEquals(idk, resolvedIdk);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIPKResolveFromWrapper() {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
String idk = "idk";
|
||||
CommandWrapper wrapper = new CommandWrapper(null, null, null, null, null, null, null, null, null, null, null, null, null, null,
|
||||
null, null, null, idk);
|
||||
String resolvedIdk = underTest.resolve(wrapper);
|
||||
Assertions.assertEquals(idk, resolvedIdk);
|
||||
}
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.fineract.commands.service;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.apache.fineract.commands.domain.CommandProcessingResultType;
|
||||
import org.apache.fineract.commands.domain.CommandSource;
|
||||
import org.apache.fineract.commands.domain.CommandWrapper;
|
||||
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
|
||||
import org.apache.fineract.commands.provider.CommandHandlerProvider;
|
||||
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
|
||||
import org.apache.fineract.infrastructure.core.api.JsonCommand;
|
||||
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
|
||||
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
|
||||
import org.apache.fineract.useradministration.domain.AppUser;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
public class SynchronousCommandProcessingServiceTest {
|
||||
|
||||
@Mock
|
||||
private PlatformSecurityContext context;
|
||||
@Mock
|
||||
private ApplicationContext applicationContext;
|
||||
@Mock
|
||||
private ToApiJsonSerializer<Map<String, Object>> toApiJsonSerializer;
|
||||
@Mock
|
||||
private ToApiJsonSerializer<CommandProcessingResult> toApiResultJsonSerializer;
|
||||
@Mock
|
||||
private ConfigurationDomainService configurationDomainService;
|
||||
@Mock
|
||||
private CommandHandlerProvider commandHandlerProvider;
|
||||
@Mock
|
||||
private IdempotencyKeyResolver idempotencyKeyResolver;
|
||||
@Mock
|
||||
private IdempotencyKeyGenerator idempotencyKeyGenerator;
|
||||
@Mock
|
||||
private CommandSourceService commandSourceService;
|
||||
|
||||
@InjectMocks
|
||||
private SynchronousCommandProcessingService underTest;
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteCommandSuccess() {
|
||||
|
||||
CommandWrapper commandWrapper = Mockito.mock(CommandWrapper.class);
|
||||
when(commandWrapper.isDatatableResource()).thenReturn(false);
|
||||
when(commandWrapper.isNoteResource()).thenReturn(false);
|
||||
when(commandWrapper.isSurveyResource()).thenReturn(false);
|
||||
when(commandWrapper.isLoanDisburseDetailResource()).thenReturn(false);
|
||||
JsonCommand jsonCommand = Mockito.mock(JsonCommand.class);
|
||||
|
||||
NewCommandSourceHandler newCommandSourceHandler = Mockito.mock(NewCommandSourceHandler.class);
|
||||
CommandProcessingResult commandProcessingResult = Mockito.mock(CommandProcessingResult.class);
|
||||
when(commandProcessingResult.isRollbackTransaction()).thenReturn(false);
|
||||
when(newCommandSourceHandler.processCommand(jsonCommand)).thenReturn(commandProcessingResult);
|
||||
when(commandHandlerProvider.getHandler(Mockito.any(), Mockito.any())).thenReturn(newCommandSourceHandler);
|
||||
|
||||
when(configurationDomainService.isMakerCheckerEnabledForTask(Mockito.any())).thenReturn(false);
|
||||
String idk = "idk";
|
||||
when(idempotencyKeyResolver.resolve(commandWrapper)).thenReturn(idk);
|
||||
CommandSource commandSource = Mockito.mock(CommandSource.class);
|
||||
when(commandSourceService.findCommandSource(commandWrapper, idk)).thenReturn(null).thenReturn(commandSource);
|
||||
|
||||
AppUser appUser = Mockito.mock(AppUser.class);
|
||||
when(commandSourceService.saveInitial(commandWrapper, jsonCommand, appUser, idk)).thenReturn(commandSource);
|
||||
when(context.authenticatedUser(Mockito.any(CommandWrapper.class))).thenReturn(appUser);
|
||||
|
||||
CommandProcessingResult actualCommandProcessingResult = underTest.executeCommand(commandWrapper, jsonCommand, false);
|
||||
|
||||
verify(commandSourceService).saveInitial(commandWrapper, jsonCommand, appUser, idk);
|
||||
verify(commandSource).setStatus(CommandProcessingResultType.PROCESSED.getValue());
|
||||
verify(commandSourceService).saveResult(commandSource);
|
||||
|
||||
Assertions.assertEquals(commandProcessingResult, actualCommandProcessingResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteCommandFails() {
|
||||
CommandWrapper commandWrapper = Mockito.mock(CommandWrapper.class);
|
||||
when(commandWrapper.isDatatableResource()).thenReturn(false);
|
||||
when(commandWrapper.isNoteResource()).thenReturn(false);
|
||||
when(commandWrapper.isSurveyResource()).thenReturn(false);
|
||||
when(commandWrapper.isLoanDisburseDetailResource()).thenReturn(false);
|
||||
JsonCommand jsonCommand = Mockito.mock(JsonCommand.class);
|
||||
|
||||
NewCommandSourceHandler newCommandSourceHandler = Mockito.mock(NewCommandSourceHandler.class);
|
||||
CommandProcessingResult commandProcessingResult = Mockito.mock(CommandProcessingResult.class);
|
||||
CommandSource commandSource = Mockito.mock(CommandSource.class);
|
||||
when(commandProcessingResult.isRollbackTransaction()).thenReturn(false);
|
||||
RuntimeException runtimeException = new RuntimeException("foo");
|
||||
when(newCommandSourceHandler.processCommand(jsonCommand)).thenThrow(runtimeException);
|
||||
when(commandHandlerProvider.getHandler(Mockito.any(), Mockito.any())).thenReturn(newCommandSourceHandler);
|
||||
|
||||
when(configurationDomainService.isMakerCheckerEnabledForTask(Mockito.any())).thenReturn(false);
|
||||
String idk = "idk";
|
||||
when(idempotencyKeyResolver.resolve(commandWrapper)).thenReturn(idk);
|
||||
when(commandSourceService.findCommandSource(commandWrapper, idk)).thenReturn(null);
|
||||
|
||||
AppUser appUser = Mockito.mock(AppUser.class);
|
||||
when(context.authenticatedUser(Mockito.any(CommandWrapper.class))).thenReturn(appUser);
|
||||
when(commandSourceService.saveInitial(commandWrapper, jsonCommand, appUser, idk)).thenReturn(commandSource);
|
||||
|
||||
CommandSource initialCommandSource = Mockito.mock(CommandSource.class);
|
||||
|
||||
when(commandSourceService.findCommandSource(commandWrapper, idk)).thenReturn(initialCommandSource);
|
||||
|
||||
Assertions.assertThrows(RuntimeException.class, () -> {
|
||||
underTest.executeCommand(commandWrapper, jsonCommand, false);
|
||||
});
|
||||
|
||||
verify(commandSourceService).saveInitial(commandWrapper, jsonCommand, appUser, idk);
|
||||
verify(commandSourceService).generateErrorException(runtimeException);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user