Write a junit for the following class, package com.sc.we.idv.plugin.common; import…
- User
Write a junit for the following class, package com.sc.we.idv.plugin.common;
import com.sc.we.idv.plugin.service.IDVExternalPluginService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext;
import static com.sc.we.idv.common.constants.IDVConstants.EXTERNAL_PLUGIN_SERVICE_IMPL;
public class AbstractIDVService {
private static Logger logger = LoggerFactory.getLogger(AbstractPluginClient.class); @Autowired private ApplicationContext context; protected IDVExternalPluginService plugInService(String country) { IDVExternalPluginService service = (IDVExternalPluginService) context .getBean(country + EXTERNAL_PLUGIN_SERVICE_IMPL); return service; }}
Sample junit, package com.sc.we.idv.faceCapture.service.impl;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when;
import com.sc.we.idv.IDVBaseTest; import com.sc.we.idv.TestCommonUtils; import com.sc.we.idv.config.IDVConfig; import com.sc.we.idv.config.TokenConfig; import com.sc.we.idv.entity.IDVTransaction; import com.sc.we.idv.faceCapture.entity.IDVFaceCaptureTransaction; import com.sc.we.idv.faceCapture.model.convertor.IDVTransactionBOToIDVFaceCaptureTxn; import com.sc.we.idv.faceCapture.model.convertor.IDVTransactionBOToInitializeResponse; import com.sc.we.idv.faceCapture.repository.IDVFaceCaptureTransactionRepo; import com.sc.we.idv.plugin.client.zoloz.model.coverter.IDVTransactionResponseToIdvTransactionEntity; import com.sc.we.idv.plugin.service.IDVExternalPluginService; import com.sc.we.idv.repository.InitializeRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations;
import com.sc.we.idv.faceCapture.model.InitializeRequest; import com.sc.we.idv.faceCapture.model.InitializeResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext;
import java.util.HashMap;
public class IDVFaceCaptureServiceImplTest extends IDVBaseTest {
@InjectMocks private IDVFaceCaptureServiceImpl service; @Mock private IDVFaceCaptureTransactionRepo repository; @Mock private IDVConfig config; @Mock private ApplicationContext context; @Mock private IDVExternalPluginService mockPluginService; @Mock private IDVTransactionResponseToIdvTransactionEntity converter; @Mock private IDVTransactionBOToIDVFaceCaptureTxn iDVTransactionBOToIDVFaceCaptureTxnConverter; @Mock private TokenConfig tokenConfig; @Mock private IDVTransactionBOToInitializeResponse iDVTransactionBOToInitializeResponse; @BeforeEach public void setUp() { MockitoAnnotations.openMocks(this); // Set up the context mock to return the mockPluginService for any requested bean when(context.getBean(anyString())).thenReturn(mockPluginService); } @Test public void testInitialize() throws Exception { // Given InitializeRequest request = new InitializeRequest(); request.setInternalInfo(TestCommonUtils.buildInternalInfo()); request.setServiceInfo(TestCommonUtils.buildServiceInfoo()); // When HashMap<String, String> expiryMap = new HashMap<>(); expiryMap.put("HK", "15"); // Mock value for token expiry when(tokenConfig.getExpiry()).thenReturn(expiryMap); when(config.isFaceCaptureInitializeEnabled(any())).thenReturn(true); when(converter.idvTransactionConverter(any())).thenReturn(new IDVTransaction()); when(iDVTransactionBOToInitializeResponse.convert(any())).thenReturn(TestCommonUtils.buildInitializeRequestFC()); when(iDVTransactionBOToIDVFaceCaptureTxnConverter.convert(any())).thenReturn(new IDVFaceCaptureTransaction()); InitializeResponse response = service.initialize(request); // Then assertNotNull(response); assertNotNull(response.getInternalInfo()); assertEquals("New", response.getInternalInfo().getServiceType()); assertNotNull(response.getServiceInfo()); assertEquals("TXNREF1002", response.getServiceInfo().getTransactionReference()); assertEquals("HK20240423001", response.getServiceInfo().getChannelReference()); assertEquals("gdfgfj", response.getServiceInfo().getAckReference()); assertEquals("02", response.getServiceInfo().getSegmentCode()); assertEquals("D", response.getServiceInfo().getProductIdentifier()); } - Assistant