Skip to content

Commit

Permalink
Fixed Null pointer exception in OtpManager and RestRequestFactory (#842)
Browse files Browse the repository at this point in the history
* - Updated IdServiceImpl
- Updated IdServiceImplTest

* - Updated IdServiceImpl
- Updated IdServiceImplTest

* - Updated IdServiceImpl
- Updated IdServiceImplTest

* - Updated IdServiceImpl
- Updated IdServiceImplTest

* - Updated IdServiceImpl
- Updated IdServiceImplTest

* - Fixed Null pointer exception
headers.getContentType() can be null in line 88

* - Fixed Null pointer exception
headers.getContentType() can be null in line 88

* - Fixed Null pointer exception
headers.getContentType() can be null in line 88

* - Fixed Null pointer exception
headers.getContentType() can be null in line 88

* - Fixed Null pointer exception
response can be null in line 135

* - Fixed Null pointer exception
response can be null in line 135

* - Fixed Null pointer exception
response can be null in line 135

* - Fixed Null pointer exception
response can be null in line 135 and 140

* - Fixed Null pointer exception:
response can be null in line 135 and 140

* - Updated fix for null pointer exception in OtpManager and RestRequestFactory

Co-authored-by: Vipul Dhurve <[email protected]>
  • Loading branch information
vipuldhurve02 and vipuldhurve authored Feb 18, 2022
1 parent b427a12 commit e5ea7ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.AbstractEnvironment;
Expand Down Expand Up @@ -84,18 +85,19 @@ public RestRequestDTO buildRequest(RestServicesConstants restService, Object req
checkHttpMethod(request, httpMethod);

if (requestBody != null) {
if (!headers.getContentType().includes(MediaType.MULTIPART_FORM_DATA)) {
request.setRequestBody(requestBody);
} else {
if (requestBody instanceof MultiValueMap) {
request.setRequestBody(requestBody);
} else {
throw new IDDataValidationException(
IdAuthenticationErrorConstants.INVALID_INPUT_PARAMETER.getErrorCode(),
String.format(IdAuthenticationErrorConstants.INVALID_INPUT_PARAMETER.getErrorMessage(),
"requestBody"));
}
}
if ( headers.getContentType()!=null && !Objects.requireNonNull(headers.getContentType()).includes(MediaType.MULTIPART_FORM_DATA)) {
request.setRequestBody(requestBody);
} else {
if (requestBody instanceof MultiValueMap) {
request.setRequestBody(requestBody);
} else {
throw new IDDataValidationException(
IdAuthenticationErrorConstants.INVALID_INPUT_PARAMETER.getErrorCode(),
String.format(IdAuthenticationErrorConstants.INVALID_INPUT_PARAMETER.getErrorMessage(),
"requestBody"));
}
}

}

checkReturnType(returnType, request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.mosip.authentication.common.service.integration;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -135,12 +132,17 @@ private String generateOTP(String uin) throws IdAuthUncheckedException {
RestRequestDTO restRequest = restRequestFactory.buildRequest(RestServicesConstants.OTP_GENERATE_SERVICE,
reqWrapper, ResponseWrapper.class);
ResponseWrapper<Map<String, String>> response = restHelper.requestSync(restRequest);
if (response != null && response.getResponse().get("status").equals(USER_BLOCKED)) {
if ( response!=null && response.getResponse()!=null && response.getResponse().get("status")!=null && response.getResponse().get("status").equals(USER_BLOCKED)) {
logger.error(IdAuthCommonConstants.SESSION_ID, this.getClass().getSimpleName(),
IdAuthenticationErrorConstants.BLOCKED_OTP_VALIDATE.getErrorCode(), USER_BLOCKED);
throw new IdAuthUncheckedException(IdAuthenticationErrorConstants.BLOCKED_OTP_VALIDATE);
}
return response.getResponse().get("otp");
if(response !=null && response.getResponse()!=null){
return response.getResponse().get("otp");
}else{
throw new IdAuthUncheckedException(IdAuthenticationErrorConstants.OTP_GENERATION_FAILED);
}

} catch (IDDataValidationException e) {
logger.error(IdAuthCommonConstants.SESSION_ID, this.getClass().getSimpleName(), "generateOTP",
e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,26 @@ public void sendOtpTest() throws RestServiceException, IdAuthenticationBusinessE
boolean sendOtpResponse = otpManager.sendOtp(otpRequestDTO, "426789089018", "UIN", valueMap, templateLanguages);
assertEquals(sendOtpResponse, true);
}


@Test(expected = IdAuthUncheckedException.class)
public void sendOtpNullResponseExceptionTest() throws RestServiceException, IdAuthenticationBusinessException {
OtpGeneratorRequestDto otpGeneratorRequestDto = getOtpGeneratorRequestDto();
ResponseWrapper<Map> otpGeneratorResponsetDto = new ResponseWrapper<>();
Map<String, Object> response = new HashMap<>();
response.put("status", "success");
otpGeneratorResponsetDto.setResponse(response);
RestRequestDTO restRequestDTO = getRestRequestDTO();
Mockito.when(restRequestFactory.buildRequest(RestServicesConstants.OTP_GENERATE_SERVICE, otpGeneratorRequestDto,
OtpGeneratorResponseDto.class)).thenReturn(restRequestDTO);
Mockito.when(restHelper.requestSync(Mockito.any())).thenReturn(null);
OtpRequestDTO otpRequestDTO = getOtpRequestDto();
Map<String, String> valueMap = new HashMap<>();
valueMap.put("namePri", "Name in PrimaryLang");
valueMap.put("nameSec", "Name in SecondaryLang");
boolean sendOtpResponse = otpManager.sendOtp(otpRequestDTO, "426789089018", "UIN", valueMap, templateLanguages);
assertEquals(sendOtpResponse, true);
}

@SuppressWarnings("rawtypes")
@Test
public void sendOtpTest_existingEntry() throws RestServiceException, IdAuthenticationBusinessException {
Expand Down

0 comments on commit e5ea7ca

Please sign in to comment.