This is one of my converter class, package com.sc.we.idv.model.converter; import…
- User
This is one of my converter class, package com.sc.we.idv.model.converter;
import java.nio.charset.Charset; import java.sql.Timestamp;
import org.springframework.stereotype.Component;
import com.sc.we.idv.common.model.BiometricVerificationResponse; import com.sc.we.idv.entity.EkycGoldenSourcePayload; import com.sc.we.idv.entity.IDVEkycGoldenSource;
import static com.sc.we.idv.common.constants.IDVConstants.*;
@Component public class BiometricResponseToEntityConverter {
public IDVEkycGoldenSource bioMetricToGoldenSourceExtracted(BiometricVerificationResponse res, EkycGoldenSourcePayload ekycGoldenSourcePayload, String idNumber) { IDVEkycGoldenSource iDVEkycGoldenSource = new IDVEkycGoldenSource(); iDVEkycGoldenSource.setIdvEkycGoldenSourcePayloadId(ekycGoldenSourcePayload.getId()); iDVEkycGoldenSource.setCreatedDate(new Timestamp(System.currentTimeMillis())); iDVEkycGoldenSource.setAckReferenceNo(res.getServiceInfo().getAckReference()); iDVEkycGoldenSource.setTransactionReferenceNo(res.getServiceInfo().getTransactionReference()); iDVEkycGoldenSource.setChannelReference(res.getServiceInfo().getChannelReference()); if (null != res.getResponseInfo().getProcessStatus() && res.getResponseInfo().getProcessStatus().equalsIgnoreCase("100")) { iDVEkycGoldenSource.setResultCode(SCCUESS_CODE); iDVEkycGoldenSource.setResultMessage(res.getResponseInfo().getProcessDescription()); } iDVEkycGoldenSource.setDocumentNumber(idNumber); return iDVEkycGoldenSource; } public EkycGoldenSourcePayload bioMetricResponseToGoldenSourcePayload(BiometricVerificationResponse response) { EkycGoldenSourcePayload idvEkycGoldenSourcePayload = new EkycGoldenSourcePayload(); idvEkycGoldenSourcePayload.setTransactionReference(response.getServiceInfo().getTransactionReference()); idvEkycGoldenSourcePayload.setAckReferenceNo(response.getServiceInfo().getAckReference()); idvEkycGoldenSourcePayload.setChannelReference(response.getServiceInfo().getChannelReference()); idvEkycGoldenSourcePayload.setFingerprintMatchGoldenSourceData(getResponseBytes(response)); idvEkycGoldenSourcePayload.setCreatedDate(getCurrentTime()); return idvEkycGoldenSourcePayload; } private byte[] getResponseBytes(BiometricVerificationResponse response) { return (null != response) ? response.toString().getBytes(Charset.forName("UTF-8")) : null; } private Timestamp getCurrentTime() { return new Timestamp(System.currentTimeMillis()); }}
This is its test class, package com.sc.we.idv.model.converter;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test; import org.mockito.InjectMocks;
import com.sc.we.idv.TestCVUtils; import com.sc.we.idv.TestCommonUtils; import com.sc.we.idv.common.model.BiometricVerificationRequest; import com.sc.we.idv.common.model.BiometricVerificationResponse; import com.sc.we.idv.entity.EkycGoldenSourcePayload; import com.sc.we.idv.entity.IDVEkycGoldenSource; import com.sc.we.idv.exceptions.UnexpectedErrorExcpetion;
public class BiometricResponseToEntityConverterTest {
@InjectMocks private BiometricResponseToEntityConverter converter = new BiometricResponseToEntityConverter(); @Test public void testBioMetricIDVBOToGoldenSourceExtractedConvertor() { // Given BiometricVerificationResponse res = TestCommonUtils.buildBiometricVerificationResponse(); res.getServiceInfo().setAckReference("IDV-PK-01-20241118213041-4b1168c1-6fa7-4662-8"); res.getServiceInfo().setTransactionReference("PKNREF1002312323129"); res.getServiceInfo().setChannelReference("PK20240423001"); Long id = (long) 1; EkycGoldenSourcePayload ekyc = new EkycGoldenSourcePayload(); ekyc.setId(id); // When IDVEkycGoldenSource goldenSourceExtracted = converter.bioMetricToGoldenSourceExtracted(res, ekyc, "12312345"); // Then assertNotNull(goldenSourceExtracted); assertEquals(goldenSourceExtracted.getDocumentNumber(), "12312345"); } @Test public void testBioMetricResponseToGoldenSourcePayloadConverter() throws UnexpectedErrorExcpetion { // Given BiometricVerificationResponse response = TestCommonUtils.buildBiometricVerificationResponse(); response.setResponseInfo(TestCommonUtils.buildResponseInfoBioMetric()); // When EkycGoldenSourcePayload goldenSourcePayload = converter.bioMetricResponseToGoldenSourcePayload(response); // Then assertNotNull(goldenSourcePayload); assertEquals(goldenSourcePayload.getTransactionReference(), response.getServiceInfo().getTransactionReference()); }}
Similarly this is a new converter class, package com.sc.we.idv.faceCapture.model.convertor;
import com.sc.we.idv.faceCapture.model.InitializeResponse; import com.sc.we.idv.faceCapture.model.InternalInfo; import com.sc.we.idv.faceCapture.model.ServiceInfo; import com.sc.we.idv.model.IDVTransactionBO; import org.springframework.stereotype.Component;
@Component public class IDVTransactionBOToInitializeResponse { public InitializeResponse convert(IDVTransactionBO transactionBO) { InitializeResponse response = new InitializeResponse(); response.serviceInfo(buildServiceInfo(transactionBO)); response.setInternalInfo(buildInternalInfo(transactionBO)); return response; }
private InternalInfo buildInternalInfo(IDVTransactionBO transactionBO) { InternalInfo internalInfo = new InternalInfo(); internalInfo.setServiceType(transactionBO.getServiceType()); internalInfo.setSourceCountry(transactionBO.getSourceCountry()); internalInfo.setSystemIdentifier(transactionBO.getSystemIdentifier()); return internalInfo; } private ServiceInfo buildServiceInfo(IDVTransactionBO transactionBO) { ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setTransactionReference(transactionBO.getTransactionReference()); serviceInfo.setChannelReference(transactionBO.getChannelReference()); serviceInfo.setAckReference(transactionBO.getAckReferenceNo()); serviceInfo.setVendorTransactionId(transactionBO.getVendorTransactionId()); serviceInfo.setSegmentCode(transactionBO.getSegmentCode()); serviceInfo.setProductIdentifier(transactionBO.getProductIdentifier()); return serviceInfo; }}
Finish this test class for this class, package com.sc.we.idv.faceCapture.model.converter;
import com.sc.we.idv.TestCommonUtils; import com.sc.we.idv.common.model.BiometricVerificationResponse; import com.sc.we.idv.entity.EkycGoldenSourcePayload; import com.sc.we.idv.entity.IDVEkycGoldenSource; import com.sc.we.idv.faceCapture.model.convertor.IDVTransactionBOToInitializeResponse; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull;
public class IDVTransactionBOToInitializeResponseTest {
@InjectMocks IDVTransactionBOToInitializeResponse conveter = new IDVTransactionBOToInitializeResponse();}
- Assistant
Here’s the completed test class for the
IDVTransactionBOToInitializeResponseconverter. This class includes unit tests to verify that the conversion fromIDVTransactionBOtoInitializeResponseis performed correctly.package com.sc.we.idv.faceCapture.model.converter; import com.sc.we.idv.TestCommonUtils; // This import assumes you have utility methods to create test instances import com.sc.we.idv.faceCapture.model.InitializeResponse; import com.sc.we.idv.faceCapture.model.InternalInfo; import com.sc.we.idv.faceCapture.model.ServiceInfo; import com.sc.we.idv.model.IDVTransactionBO; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; public class IDVTransactionBOToInitializeResponseTest { @InjectMocks IDVTransactionBOToInitializeResponse converter = new IDVTransactionBOToInitializeResponse(); @Test public void testConvert() { // Given IDVTransactionBO transactionBO = TestCommonUtils.buildIDVTransactionBO(); // Assumes this method creates a valid IDVTransactionBO // When InitializeResponse response = converter.convert(transactionBO); // Then assertNotNull(response); assertNotNull(response.getServiceInfo()); assertNotNull(response.getInternalInfo()); // Assertions to verify service info ServiceInfo serviceInfo = response.getServiceInfo(); assertEquals(transactionBO.getTransactionReference(), serviceInfo.getTransactionReference()); assertEquals(transactionBO.getChannelReference(), serviceInfo.getChannelReference()); assertEquals(transactionBO.getAckReferenceNo(), serviceInfo.getAckReference()); assertEquals(transactionBO.getVendorTransactionId(), serviceInfo.getVendorTransactionId()); assertEquals(transactionBO.getSegmentCode(), serviceInfo.getSegmentCode()); assertEquals(transactionBO.getProductIdentifier(), serviceInfo.getProductIdentifier()); // Assertions to verify internal info InternalInfo internalInfo = response.getInternalInfo(); assertEquals(transactionBO.getServiceType(), internalInfo.getServiceType()); assertEquals(transactionBO.getSourceCountry(), internalInfo.getSourceCountry()); assertEquals(transactionBO.getSystemIdentifier(), internalInfo.getSystemIdentifier()); } }Key Points:
-
Test Setup: The test class uses
@InjectMocksto inject theIDVTransactionBOToInitializeResponseconverter instance, which makes testing easier. -
Test Method: The
testConvert()method creates a test instance ofIDVTransactionBO(defined in your test utility class) and calls theconvertmethod of the converter. -
Assertions: Various assertions are performed to ensure that the converted
InitializeResponseobject contains expected values. You need to ensure thatTestCommonUtils.buildIDVTransactionBO()provides a well-defined instance for the conversion test. -
Null Checks: Before making assertions on the properties of the converted object, null checks are conducted to confirm that the object and its properties are properly initialized.
This structure provides a solid foundation for testing the
IDVTransactionBOToInitializeResponseconverter. Adjust theTestCommonUtilsas necessary to generate valid test data forIDVTransactionBO. -
- User
This is my another converter class, package com.sc.we.idv.faceCapture.model.convertor;
import com.sc.we.idv.common.enums.IDVTransactionStatus; import com.sc.we.idv.faceCapture.entity.IDVFaceCaptureTransaction; import com.sc.we.idv.model.IDVTransactionBO; import org.springframework.stereotype.Component;
import java.sql.Timestamp;
@Component public class IDVTransactionBOToIDVFaceCaptureTxn { public IDVFaceCaptureTransaction convert(IDVTransactionBO transactionBO) { IDVFaceCaptureTransaction idvFaceCaptureTransaction = new IDVFaceCaptureTransaction(); idvFaceCaptureTransaction.setTxnReferenceNo(transactionBO.getTransactionReference()); idvFaceCaptureTransaction.setChannelReference(transactionBO.getChannelReference()); idvFaceCaptureTransaction.setAckReferenceNo(transactionBO.getAckReferenceNo()); idvFaceCaptureTransaction.setVendorTransactionId(transactionBO.getVendorTransactionId()); idvFaceCaptureTransaction.setSourceCountry(transactionBO.getSourceCountry()); idvFaceCaptureTransaction.setCreatedDate(new Timestamp(System.currentTimeMillis())); idvFaceCaptureTransaction.setCurrentState(IDVTransactionStatus.INITIALIZED.name()); return idvFaceCaptureTransaction; } }
Finish this test class also, package com.sc.we.idv.faceCapture.model.converter;
public class IDVTransactionBOToIDVFaceCaptureTxnTest { }
- Assistant
Here’s the completed test class for the
IDVTransactionBOToIDVFaceCaptureTxnconverter. This test validates the conversion fromIDVTransactionBOtoIDVFaceCaptureTransaction.package com.sc.we.idv.faceCapture.model.converter; import com.sc.we.idv.TestCommonUtils; // Assuming this provides utility methods to create test instances import com.sc.we.idv.faceCapture.entity.IDVFaceCaptureTransaction; import com.sc.we.idv.model.IDVTransactionBO; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import java.sql.Timestamp; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; public class IDVTransactionBOToIDVFaceCaptureTxnTest { @InjectMocks IDVTransactionBOToIDVFaceCaptureTxn converter = new IDVTransactionBOToIDVFaceCaptureTxn(); @Test public void testConvert() { // Given IDVTransactionBO transactionBO = TestCommonUtils.buildIDVTransactionBO(); // Assumes this method creates a valid IDVTransactionBO // When IDVFaceCaptureTransaction faceCaptureTransaction = converter.convert(transactionBO); // Then assertNotNull(faceCaptureTransaction); // Verifying properties of IDVFaceCaptureTransaction assertEquals(transactionBO.getTransactionReference(), faceCaptureTransaction.getTxnReferenceNo()); assertEquals(transactionBO.getChannelReference(), faceCaptureTransaction.getChannelReference()); assertEquals(transactionBO.getAckReferenceNo(), faceCaptureTransaction.getAckReferenceNo()); assertEquals(transactionBO.getVendorTransactionId(), faceCaptureTransaction.getVendorTransactionId()); assertEquals(transactionBO.getSourceCountry(), faceCaptureTransaction.getSourceCountry()); // You can also validate the createdDate and the currentState assertNotNull(faceCaptureTransaction.getCreatedDate()); assertEquals(IDVTransactionStatus.INITIALIZED.name(), faceCaptureTransaction.getCurrentState()); } }Explanation and Key Points:
-
Test Class: The test class is named
IDVTransactionBOToIDVFaceCaptureTxnTestand includes the necessary imports. -
Test Injection: An instance of
IDVTransactionBOToIDVFaceCaptureTxnis created using the@InjectMocksannotation. This allows you to test the conversion logic directly. -
Setup Test Data: The
TestCommonUtils.buildIDVTransactionBO()method is assumed to return a valid instance ofIDVTransactionBOthat can be used for testing. -
Conversion Test: The
testConvert()method performs the conversion fromIDVTransactionBOtoIDVFaceCaptureTransaction. -
Assertions:
- The test checks that the returned
IDVFaceCaptureTransactionobject is not null. - It verifies that all relevant fields in the
IDVFaceCaptureTransactionobject match those in the providedIDVTransactionBOinstance. - It confirms that the creation date is not null and that the current state matches the expected status of
INITIALIZED.
- The test checks that the returned
Adjustments:
- Ensure that the utility method
TestCommonUtils.buildIDVTransactionBO()is defined and returns an object populated with valid test data. - You may want to add more tests for edge cases or exceptional conditions based on how your application is expected to behave.
-