-
Notifications
You must be signed in to change notification settings - Fork 177
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
kaledOu
wants to merge
4
commits into
mosip:develop
Choose a base branch
from
kaledOu:develop-MOSIP-39680
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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); | ||
|
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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;