Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOSIP-39680 fix junit test case #2021

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package io.mosip.registration.processor.status.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;

import java.util.Optional;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.registration.processor.core.auth.dto.AuthRequestDTO;
import io.mosip.registration.processor.core.auth.dto.AuthResponseDTO;
import io.mosip.registration.processor.core.auth.dto.IndividualIdDto;
import io.mosip.registration.processor.core.auth.dto.ResponseDTO;
import io.mosip.registration.processor.core.exception.ApisResourceAccessException;
import io.mosip.registration.processor.core.http.ResponseWrapper;
import io.mosip.registration.processor.core.spi.restclient.RegistrationProcessorRestClientService;
import io.mosip.registration.processor.rest.client.utils.RestApiClient;
import io.mosip.registration.processor.status.service.impl.InternalAuthDelegateServiceImpl;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -21,100 +22,111 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Optional;

import io.mosip.kernel.core.exception.NoSuchAlgorithmException;
import io.mosip.registration.processor.core.auth.dto.AuthRequestDTO;
import io.mosip.registration.processor.core.auth.dto.AuthResponseDTO;
import io.mosip.registration.processor.core.auth.dto.IndividualIdDto;
import io.mosip.registration.processor.core.auth.dto.ResponseDTO;
import io.mosip.registration.processor.core.exception.ApisResourceAccessException;
import io.mosip.registration.processor.core.http.ResponseWrapper;
import io.mosip.registration.processor.core.spi.restclient.RegistrationProcessorRestClientService;
import io.mosip.registration.processor.rest.client.utils.RestApiClient;
import io.mosip.registration.processor.status.service.impl.InternalAuthDelegateServiceImpl;
import org.springframework.web.reactive.function.client.WebClient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
public class InternalAuthDelegateServiceImplTest {

@InjectMocks
InternalAuthDelegateServiceImpl internalAuthDelegateServiceImpl = new InternalAuthDelegateServiceImpl();

@Mock
RegistrationProcessorRestClientService<Object> restClientService;

@Mock
private RestApiClient restApiClient;

@Mock
RestTemplate restTemplate;

@Mock
WebClient webClient;

@Mock
ObjectMapper mapper;

AuthRequestDTO authRequestDTO = new AuthRequestDTO();
AuthResponseDTO authResponse = new AuthResponseDTO();
ResponseDTO responseDto = new ResponseDTO();

IndividualIdDto individualIdDto = new IndividualIdDto();
ResponseWrapper<IndividualIdDto> response = new ResponseWrapper<IndividualIdDto>();

@Before
public void setup() throws Exception {

ReflectionTestUtils.setField(internalAuthDelegateServiceImpl, "internalAuthUri",
"https://dev.mosip.net/idauthentication/v1/internal/auth");
ReflectionTestUtils.setField(internalAuthDelegateServiceImpl, "getCertificateUri",
"https://dev.mosip.net/idauthentication/v1/internal/getCertificate");
authRequestDTO.setEnv("Staging");
authRequestDTO.setIndividualId("45128164920495");
authRequestDTO.setIndividualIdType("UIN");
authRequestDTO.setRequest("BFijjscahGoaaol");

responseDto.setAuthStatus(true);
authResponse.setId("");
authResponse.setResponse(responseDto);
ResponseEntity<AuthResponseDTO> entity = new ResponseEntity<AuthResponseDTO>(authResponse, HttpStatus.OK);
Mockito.when(restApiClient.getRestTemplate()).thenReturn(restTemplate);
Mockito.when(restTemplate.exchange(anyString(), any(), any(), eq(AuthResponseDTO.class))).thenReturn(entity);
Mockito.when(mapper.writeValueAsString(any())).thenReturn("");
Mockito.when(mapper.readValue(anyString(), eq(IndividualIdDto.class))).thenReturn(individualIdDto);
}

@Ignore //TODO: ignored test case should be removed after the testing
@Test
public void authenticateSuccessTest() throws Exception {

individualIdDto.setIndividualId("84953741281492");
response.setResponse(individualIdDto);
response.setErrors(null);
Mockito.when(restClientService.getApi(any(), any(), anyString(), anyString(), any())).thenReturn(response);
AuthResponseDTO result = internalAuthDelegateServiceImpl.authenticate(authRequestDTO, new HttpHeaders());
assertEquals(result.getResponse().isAuthStatus(), true);
}

@Test(expected = ApisResourceAccessException.class)
public void authenticateIndividualIdNotFoundFailureTest() throws Exception {

individualIdDto.setIndividualId("84953741281491");
response.setResponse(individualIdDto);
Mockito.when(restClientService.getApi(any(), any(), anyString(), anyString(), any())).thenReturn(response);
internalAuthDelegateServiceImpl.authenticate(authRequestDTO, new HttpHeaders());
}

@Test
public void getCertificateSuccessTest() throws Exception {

Optional<String> referenceId = Optional.of("PROCESSOR");
Mockito.when(restApiClient.getApi(any(), eq(Object.class))).thenReturn(new Object());
Object result = internalAuthDelegateServiceImpl.getCertificate("REGISTRATIONCLIENT",
referenceId, new HttpHeaders());
assertNotNull(result);
}
@InjectMocks
InternalAuthDelegateServiceImpl internalAuthDelegateServiceImpl = new InternalAuthDelegateServiceImpl();

@Mock
RegistrationProcessorRestClientService<Object> restClientService;

@Mock
private RestApiClient restApiClient;

@Mock
RestTemplate restTemplate;

@Mock
WebClient webClient;

@Mock
ObjectMapper mapper;

AuthRequestDTO authRequestDTO = new AuthRequestDTO();
AuthResponseDTO authResponse = new AuthResponseDTO();
ResponseDTO responseDto = new ResponseDTO();

IndividualIdDto individualIdDto = new IndividualIdDto();
ResponseWrapper<IndividualIdDto> response = new ResponseWrapper<IndividualIdDto>();

@Before
public void setup() throws Exception {

ReflectionTestUtils.setField(internalAuthDelegateServiceImpl, "internalAuthUri",
"https://dev.mosip.net/idauthentication/v1/internal/auth");
ReflectionTestUtils.setField(internalAuthDelegateServiceImpl, "getCertificateUri",
"https://dev.mosip.net/idauthentication/v1/internal/getCertificate");
authRequestDTO.setEnv("Staging");
authRequestDTO.setIndividualId("45128164920495");
authRequestDTO.setIndividualIdType("UIN");
authRequestDTO.setRequest("BFijjscahGoaaol");

responseDto.setAuthStatus(true);
authResponse.setId("");
authResponse.setResponse(responseDto);
ResponseEntity<AuthResponseDTO> entity = new ResponseEntity<AuthResponseDTO>(authResponse, HttpStatus.OK);
Mockito.when(restApiClient.getRestTemplate()).thenReturn(restTemplate);
Mockito.when(restTemplate.exchange(anyString(), any(), any(), eq(AuthResponseDTO.class))).thenReturn(entity);
Mockito.when(mapper.writeValueAsString(any())).thenReturn("");
Mockito.when(mapper.readValue(anyString(), eq(IndividualIdDto.class))).thenReturn(individualIdDto);


WebClient.RequestBodyUriSpec requestBodyUriSpec = Mockito.mock(WebClient.RequestBodyUriSpec.class);
WebClient.RequestHeadersSpec requestHeadersSpec = Mockito.mock(WebClient.RequestHeadersSpec.class);
WebClient.ResponseSpec responseSpec = Mockito.mock(WebClient.ResponseSpec.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaledOu Can we remove these and define above with @mock annotation. Example:
@mock
private WebClient webClient;
@mock
private WebClient.RequestBodySpec requestBodySpec;
@mock
private WebClient.RequestBodyUriSpec requestBodyUriSpec;
@mock
private WebClient.ResponseSpec responseSpec;


Mockito.when(webClient.post()).thenReturn(requestBodyUriSpec);
Mockito.when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodyUriSpec);
Mockito.when(requestBodyUriSpec.headers(any())).thenReturn(requestBodyUriSpec);
Mockito.when(requestBodyUriSpec.bodyValue(any())).thenReturn(requestHeadersSpec);
Mockito.when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
AuthResponseDTO mockResponse = new AuthResponseDTO();
ResponseDTO responseDTO = new ResponseDTO();
responseDTO.setAuthStatus(true);
mockResponse.setResponse(responseDTO);

Mockito.when(responseSpec.bodyToMono(AuthResponseDTO.class))
.thenReturn(Mono.just(mockResponse));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaledOu Do we need to create the new object as it is already there as "authResponse" object. Can we check on this.

}

@Test
public void authenticateSuccessTest() throws Exception {

individualIdDto.setIndividualId("84953741281492");
response.setResponse(individualIdDto);
response.setErrors(null);
Mockito.when(restClientService.getApi(any(), any(), anyString(), anyString(), any())).thenReturn(response);
AuthResponseDTO result = internalAuthDelegateServiceImpl.authenticate(authRequestDTO, new HttpHeaders());
assertEquals(result.getResponse().isAuthStatus(), true);
}

@Test(expected = ApisResourceAccessException.class)
public void authenticateIndividualIdNotFoundFailureTest() throws Exception {

individualIdDto.setIndividualId("84953741281491");
response.setResponse(individualIdDto);
Mockito.when(restClientService.getApi(any(), any(), anyString(), anyString(), any())).thenReturn(response);
internalAuthDelegateServiceImpl.authenticate(authRequestDTO, new HttpHeaders());
}

@Test
public void getCertificateSuccessTest() throws Exception {

Optional<String> referenceId = Optional.of("PROCESSOR");
Mockito.when(restApiClient.getApi(any(), eq(Object.class))).thenReturn(new Object());
Object result = internalAuthDelegateServiceImpl.getCertificate("REGISTRATIONCLIENT",
referenceId, new HttpHeaders());
assertNotNull(result);
}

}