Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.6 kB
2
Indexable
Never
package com.gaian.darpa.controller.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gaian.darpa.domain.AccountHolderCardDetails;
import com.gaian.darpa.domain.request.AccountHolderCardDetailsUpdateRequest;
import com.gaian.darpa.service.AccountHolderCardDetailsService;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import java.util.Set;

import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class AccountHolderCardDetailsControllerImplTest {

    private final Logger log = LoggerFactory.getLogger(AccountHolderCardDetailsControllerImplTest.class);

    @InjectMocks
    private AccountHolderCardDetailsControllerImpl accountHolderCardDetailsControllerImpl;

    @Mock
    private AccountHolderCardDetailsService accountHolderCardDetailsService;

    private AccountHolderCardDetails accountHolderCardDetailsCreateRequest;
    private AccountHolderCardDetailsUpdateRequest accountHolderCardDetailsUpdateRequest;
    private AccountHolderCardDetails accountHolderCardDetails;

    private ObjectMapper mapper;

    @Mock
    HttpServletRequest request;

    @BeforeEach
    public void init() throws JsonParseException, JsonMappingException, IOException {
        MockitoAnnotations.initMocks(this);

        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

        InputStream inputStream1 = AccountHolderCardDetailsControllerImplTest.class.getResourceAsStream("AccountHolderCardDetails.json");
        accountHolderCardDetails = new ObjectMapper().readValue(inputStream1, AccountHolderCardDetails.class);

        InputStream inputStream2 =
            AccountHolderCardDetailsControllerImplTest.class.getResourceAsStream("AccountHolderCardDetailsCreate.json");
        accountHolderCardDetailsCreateRequest = new ObjectMapper().readValue(inputStream2, AccountHolderCardDetails.class);

        InputStream inputStream3 =
            AccountHolderCardDetailsControllerImplTest.class.getResourceAsStream("AccountHolderCardDetailsUpdateRequest.json");
        accountHolderCardDetailsUpdateRequest = new ObjectMapper().readValue(inputStream3, AccountHolderCardDetailsUpdateRequest.class);
    }

    @Test
    public void testCreateAccountHolderCardDetailsUsingPOST() throws Exception {
        Mockito
            .doReturn(accountHolderCardDetails)
            .when(accountHolderCardDetailsService)
            .createAccountHolderCardDetailsUsingPOST(Mockito.any());
        ResponseEntity<AccountHolderCardDetails> response = accountHolderCardDetailsControllerImpl.createAccountHolderCardDetailsUsingPOST(
            accountHolderCardDetailsCreateRequest
        );
        log.info("Response : {}", response);
        assertEquals(response.getBody().getId(), "60fc3d1ba216347964fe0f3d");
    }

    @Test
    public void testDeleteAccountHolderCardDetailsUsingDELETE() throws Exception {
        Mockito.doNothing().when(accountHolderCardDetailsService).deleteAccountHolderCardDetailsByIdUsingDELETE(Mockito.anyString());
        ResponseEntity<Void> response = accountHolderCardDetailsControllerImpl.deleteAccountHolderCardDetailsByIdUsingDELETE(
            accountHolderCardDetails.getId()
        );
        assertEquals(
            204,
            response.getStatusCodeValue(),
            "The httpStatus code after removing the accountHolderCardDetails from the resource must be 204"
        );
    }

    @Test
    public void testGetAllAccountHolderCardDetailssUsingGET() throws Exception {
        Set<AccountHolderCardDetails> listOfAppConsumers = new LinkedHashSet<>();
        listOfAppConsumers.add(accountHolderCardDetails);

        Mockito
            .doReturn(listOfAppConsumers)
            .when(accountHolderCardDetailsService)
            .getAllAccountHolderCardDetailsOfGivenAccHolderId(Mockito.any());
        ResponseEntity<Set<AccountHolderCardDetails>> response = accountHolderCardDetailsControllerImpl.getAllAccountHolderCardDetailsOfGivenAccHolderId(
            ""
        );
        assertEquals(1, response.getBody().size(), "There is only one record the accountHolderCardDetails list.");
    }

    @Test
    public void testGetAccountHolderCardDetailsUsingGET() throws Exception {
        Mockito
            .doReturn(accountHolderCardDetails)
            .when(accountHolderCardDetailsService)
            .getAccountHolderCardDetailsByIdUsingGET(Mockito.anyString());
        ResponseEntity<AccountHolderCardDetails> response = accountHolderCardDetailsControllerImpl.getAccountHolderCardDetailsByIdUsingGET(
            accountHolderCardDetails.getId()
        );
        assertEquals(response.getBody().getId(), "60fc3d1ba216347964fe0f3d");
    }

    @Test
    public void testUpdateAccountHolderCardDetailsUsingPUT() throws Exception {
        Mockito
            .doReturn(accountHolderCardDetails)
            .when(accountHolderCardDetailsService)
            .updateAccountHolderCardDetailsByIdUsingPUT(Mockito.any(), Mockito.any());

        ResponseEntity<AccountHolderCardDetails> response = accountHolderCardDetailsControllerImpl.updateAccountHolderCardDetailsByIdUsingPUT(
            accountHolderCardDetailsUpdateRequest,
            accountHolderCardDetails.getId()
        );
        log.info("Reponse after updation : {}", response.getBody());
        assertEquals(response.getBody().getId(), "60fc3d1ba216347964fe0f3d");
    }
}
Leave a Comment