FINERACT-1269: (fix) add a try-catch block to ensure that the json is deserialized successfully.

- Wrap the fromJson() method with try-catch block and throw proper exception.
- Add `publishHookEventHandlesInvalidJson()` method to test the invalid json is handled successfully.
This commit is contained in:
zeyad2003
2024-03-27 12:50:38 +01:00
committed by Adam Saghy
parent ed5098b5a5
commit ae6dde0224
2 changed files with 68 additions and 40 deletions
@@ -19,6 +19,7 @@
package org.apache.fineract.commands.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -34,10 +35,10 @@ import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDoma
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.domain.FineractRequestContextHolder;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
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;
@@ -163,11 +164,25 @@ public class SynchronousCommandProcessingServiceTest {
when(commandSourceService.processCommand(commandHandler, jsonCommand, commandSource, appUser, false, false))
.thenThrow(runtimeException);
Assertions.assertThrows(RuntimeException.class, () -> {
assertThrows(RuntimeException.class, () -> {
underTest.executeCommand(commandWrapper, jsonCommand, false);
});
verify(commandSourceService).getCommandSource(commandId);
verify(commandSourceService).generateErrorInfo(runtimeException);
}
@Test
public void publishHookEventHandlesInvalidJson() {
String entityName = "entity";
String actionName = "action";
JsonCommand command = Mockito.mock(JsonCommand.class);
String invalidJson = "{ invalidJson }";
when(command.json()).thenReturn(invalidJson);
assertThrows(PlatformApiDataValidationException.class, () -> {
underTest.publishHookEvent(entityName, actionName, command, Object.class);
});
}
}