|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.organisation.teller.serialization; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import java.math.BigDecimal; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Optional; |
| 30 | +import org.apache.fineract.infrastructure.core.exception.InvalidJsonException; |
| 31 | +import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException; |
| 32 | +import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper; |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.function.Executable; |
| 36 | +import org.springframework.lang.NonNull; |
| 37 | +import org.springframework.lang.Nullable; |
| 38 | + |
| 39 | +class TellerCommandFromApiJsonDeserializerTest { |
| 40 | + |
| 41 | + private final FromJsonHelper fromJsonHelper = new FromJsonHelper(); |
| 42 | + private final TellerCommandFromApiJsonDeserializer underTest = new TellerCommandFromApiJsonDeserializer(fromJsonHelper); |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testCashTxnValidJsonPassesValidation() throws JsonProcessingException { |
| 46 | + String json = cashTxnJson(BigDecimal.valueOf(1000), "10 September 2022", "Test note", "USD"); |
| 47 | + assertDoesNotThrow(() -> underTest.validateForCashTxnForCashier(json)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testCashTxnBlankOrNullJsonThrowsInvalidJsonException() { |
| 52 | + String whitespaceOnly = " "; |
| 53 | + assertThrows(InvalidJsonException.class, () -> underTest.validateForCashTxnForCashier(null)); |
| 54 | + assertThrows(InvalidJsonException.class, () -> underTest.validateForCashTxnForCashier("")); |
| 55 | + assertThrows(InvalidJsonException.class, () -> underTest.validateForCashTxnForCashier(whitespaceOnly)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testCashTxnMissingTxnAmountThrowsValidationException() { |
| 60 | + assertPlatformValidationException("The parameter `txnAmount` is mandatory.", "validation.msg.teller.txnAmount.cannot.be.blank", |
| 61 | + () -> underTest.validateForCashTxnForCashier(cashTxnJson(null, "10 September 2022", "Test note", "USD"))); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testCashTxnMissingTxnDateThrowsValidationException() { |
| 66 | + assertPlatformValidationException("The parameter `txnDate` is mandatory.", "validation.msg.teller.txnDate.cannot.be.blank", |
| 67 | + () -> underTest.validateForCashTxnForCashier(cashTxnJson(BigDecimal.valueOf(1000), null, "Test note", "USD"))); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testCashTxnTxnNoteExceedingMaxLengthThrowsValidationException() { |
| 72 | + assertPlatformValidationException("The parameter `txnNote` exceeds max length of 200.", |
| 73 | + "validation.msg.teller.txnNote.exceeds.max.length", () -> underTest |
| 74 | + .validateForCashTxnForCashier(cashTxnJson(BigDecimal.valueOf(1000), "10 September 2022", "A".repeat(201), "USD"))); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCashTxnCurrencyCodeExceedingMaxLengthThrowsValidationException() { |
| 79 | + assertPlatformValidationException("The parameter `currencyCode` exceeds max length of 3.", |
| 80 | + "validation.msg.teller.currencyCode.exceeds.max.length", () -> underTest |
| 81 | + .validateForCashTxnForCashier(cashTxnJson(BigDecimal.valueOf(1000), "10 September 2022", "Test note", "ABCD"))); |
| 82 | + } |
| 83 | + |
| 84 | + @NonNull |
| 85 | + private String cashTxnJson(@Nullable BigDecimal txnAmount, @Nullable String txnDate, @Nullable String txnNote, |
| 86 | + @Nullable String currencyCode) throws JsonProcessingException { |
| 87 | + Map<String, Object> map = new HashMap<>(); |
| 88 | + map.put("dateFormat", "dd MMMM yyyy"); |
| 89 | + map.put("locale", "en"); |
| 90 | + Optional.ofNullable(txnAmount).ifPresent(a -> map.put("txnAmount", a)); |
| 91 | + Optional.ofNullable(txnDate).ifPresent(d -> map.put("txnDate", d)); |
| 92 | + Optional.ofNullable(txnNote).ifPresent(n -> map.put("txnNote", n)); |
| 93 | + Optional.ofNullable(currencyCode).ifPresent(c -> map.put("currencyCode", c)); |
| 94 | + return createJsonCommand(map); |
| 95 | + } |
| 96 | + |
| 97 | + @NonNull |
| 98 | + private String createJsonCommand(Map<String, Object> jsonMap) throws JsonProcessingException { |
| 99 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 100 | + return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonMap); |
| 101 | + } |
| 102 | + |
| 103 | + private void assertPlatformValidationException(String message, String code, Executable executable) { |
| 104 | + PlatformApiDataValidationException validationException = assertThrows(PlatformApiDataValidationException.class, executable); |
| 105 | + assertPlatformException(message, code, validationException); |
| 106 | + } |
| 107 | + |
| 108 | + private void assertPlatformException(String expectedMessage, String expectedCode, |
| 109 | + PlatformApiDataValidationException platformApiDataValidationException) { |
| 110 | + Assertions.assertEquals(expectedMessage, platformApiDataValidationException.getErrors().get(0).getDefaultUserMessage()); |
| 111 | + Assertions.assertEquals(expectedCode, platformApiDataValidationException.getErrors().get(0).getUserMessageGlobalisationCode()); |
| 112 | + } |
| 113 | +} |
0 commit comments