Skip to content

Commit

Permalink
Add more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
pauluswi committed Nov 12, 2024
1 parent 52c3bbf commit eb71110
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static String getBalance(String xmlResponse) {
return extractValueFromXml(xmlResponse, "//*[local-name()='Amt']");
}

public static String getCustomerName(String xmlResponse) {
public static String getCustomerFullName(String xmlResponse) {
return extractValueFromXml(xmlResponse, "//*[local-name()='Name']");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.pauluswi.batavia.service;

import com.pauluswi.batavia.service.demo.ISO20022ResponseParser;
import com.pauluswi.batavia.service.demo.ISO20022Service;
import com.pauluswi.dto.BalanceDataDTO;
import com.pauluswi.dto.BalanceInquiryRequestDTO;
import com.pauluswi.dto.BalanceInquiryResponseDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class CustomerBalance20022ServiceTest {

@Mock
private ISO20022Service iso20022Service;

@InjectMocks
private CustomerBalance20022Service customerBalanceService;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
public void testGetCustomerBalance() {
// Arrange
BalanceInquiryRequestDTO requestDTO = new BalanceInquiryRequestDTO();
requestDTO.setBankAccountNumber("1234567890");
requestDTO.setCustomerFullName("Ahmad Subarjo");

String iso20022Request = "<ISO20022 request XML>";
String iso20022Response = "<ISO20022 response XML>";

// Mock the methods of ISO20022Service
when(iso20022Service.buildBalanceInquiryRequest("1234567890", "Ahmad Subarjo")).thenReturn(iso20022Request);
when(iso20022Service.simulateBalanceInquiryResponse(iso20022Request)).thenReturn(iso20022Response);

// Mock the static methods of ISO20022ResponseParser
try (MockedStatic<ISO20022ResponseParser> mockedParser = mockStatic(ISO20022ResponseParser.class)) {
mockedParser.when(() -> ISO20022ResponseParser.getBankAccountNumber(iso20022Response)).thenReturn("1234567890");
mockedParser.when(() -> ISO20022ResponseParser.getCustomerFullName(iso20022Response)).thenReturn("Ahmad Subarjo");
mockedParser.when(() -> ISO20022ResponseParser.getBalance(iso20022Response)).thenReturn("1500.00");

// Act
BalanceInquiryResponseDTO responseDTO = customerBalanceService.getCustomerBalance(requestDTO);

// Assert
assertEquals("00", responseDTO.getResponseCode());
assertEquals("msg123456", responseDTO.getMTI());

BalanceDataDTO dataDTO = responseDTO.getData();
assertEquals("1234567890", dataDTO.getBankAccountNumber());
assertEquals(1500.00, dataDTO.getBalance());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ public class ISO20022ResponseParserTest {
<Document>
<Acct>
<Id>123456789</Id>
<Name>John Doe</Name>
<Amt>1000.00</Amt>
<Name>Ahmad Subarjo</Name>
<Amt>5000.00</Amt>
</Acct>
</Document>
""";

@Test
public void testGetBalance() {
String balance = ISO20022ResponseParser.getBalance(sampleXmlResponse);
assertEquals("1000.00", balance, "Balance should match the expected value from XML.");
assertEquals("5000.00", balance, "Balance should match the expected value from XML.");
}

@Test
public void testGetCustomerName() {
String customerName = ISO20022ResponseParser.getCustomerName(sampleXmlResponse);
assertEquals("John Doe", customerName, "Customer name should match the expected value from XML.");
String customerName = ISO20022ResponseParser.getCustomerFullName(sampleXmlResponse);
assertEquals("Ahmad Subarjo", customerName, "Customer name should match the expected value from XML.");
}

@Test
Expand All @@ -39,7 +39,7 @@ public void testInvalidXmlResponse() {
String invalidXmlResponse = "<InvalidXml>";

assertNull(ISO20022ResponseParser.getBalance(invalidXmlResponse), "Balance should be null for invalid XML.");
assertNull(ISO20022ResponseParser.getCustomerName(invalidXmlResponse), "Customer name should be null for invalid XML.");
assertNull(ISO20022ResponseParser.getCustomerFullName(invalidXmlResponse), "Customer name should be null for invalid XML.");
assertNull(ISO20022ResponseParser.getBankAccountNumber(invalidXmlResponse), "Bank account number should be null for invalid XML.");
}

Expand All @@ -48,13 +48,13 @@ public void testMissingFieldsInXml() {
String incompleteXmlResponse = """
<Document>
<Acct>
<Name>John Doe</Name>
<Name>Ahmad Subarjo</Name>
</Acct>
</Document>
""";

assertNull(ISO20022ResponseParser.getBalance(incompleteXmlResponse), "Balance should be null if Amt tag is missing.");
assertEquals("John Doe", ISO20022ResponseParser.getCustomerName(incompleteXmlResponse), "Customer name should still be parsed correctly.");
assertEquals("Ahmad Subarjo", ISO20022ResponseParser.getCustomerFullName(incompleteXmlResponse), "Customer name should still be parsed correctly.");
assertNull(ISO20022ResponseParser.getBankAccountNumber(incompleteXmlResponse), "Bank account number should be null if Id tag is missing.");
}
}

0 comments on commit eb71110

Please sign in to comment.