Skip to content

Commit

Permalink
ES904 (#808)
Browse files Browse the repository at this point in the history
Signed-off-by: ase-101 <sunkadaeanusha@gmail.com>
  • Loading branch information
ase-101 authored Jul 11, 2024
1 parent bd9666c commit bb85a58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.mosip.esignet.core.config;

import io.mosip.esignet.core.util.CaptchaHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class SharedComponentConfig {

@Autowired
private RestTemplate restTemplate;

@Bean
public CaptchaHelper captchaHelper(@Value("${mosip.esignet.captcha.validator-url}") String validatorUrl,
@Value("${mosip.esignet.captcha.module-name}") String moduleName) {
return new CaptchaHelper(restTemplate, validatorUrl, moduleName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
import io.mosip.esignet.core.dto.RequestWrapper;
import io.mosip.esignet.core.dto.ResponseWrapper;
import io.mosip.esignet.core.exception.EsignetException;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
Expand All @@ -25,19 +22,19 @@

import static io.mosip.esignet.core.constants.Constants.UTC_DATETIME_PATTERN;

@Component
@Slf4j
public class CaptchaHelper {

@Autowired
private RestTemplate restTemplate;

@Value("${mosip.esignet.captcha.module-name}")
private String moduleName;

@Value("${mosip.esignet.captcha.validator-url}")
private String validatorUrl;

public CaptchaHelper(RestTemplate restTemplate, String validatorUrl, String moduleName) {
this.restTemplate = restTemplate;
this.validatorUrl = validatorUrl;
this.moduleName = moduleName;
}

public boolean validateCaptcha(String captchaToken) {

if (captchaToken == null || captchaToken.isBlank()) {
Expand All @@ -61,18 +58,17 @@ public boolean validateCaptcha(String captchaToken) {

ResponseEntity<ResponseWrapper> responseEntity = restTemplate.exchange(
requestEntity,
ResponseWrapper.class
);
ResponseWrapper.class);

if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.getBody() != null) {
ResponseWrapper<?> responseWrapper = (ResponseWrapper<?>) responseEntity.getBody();
if (responseWrapper != null && responseWrapper.getResponse() != null) {
if (responseWrapper != null && responseWrapper.getResponse() != null &&
CollectionUtils.isEmpty(responseWrapper.getErrors())) {
log.info("Captcha Validation Successful");
return true;
}
log.error("Errors received from CaptchaService: {}", responseWrapper.getErrors()); //NOSONAR responseWrapper is already evaluated to be not null
log.error("Errors received from CaptchaService: {}", responseEntity.getBody());
}
log.error("Errors received from CaptchaService: {}", responseEntity.getStatusCode());
} catch (Exception e) {
log.error("Failed to validate captcha", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;

import static org.mockito.ArgumentMatchers.any;

Expand All @@ -29,36 +30,32 @@ public class CaptchaHelperTest {
@Mock
RestTemplate restTemplate;

@InjectMocks
CaptchaHelper captchaHelper;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(captchaHelper, "validatorUrl", "https://api-internal.camdgc-dev1.mosip.net/v1/captcha/validatecaptcha");
ReflectionTestUtils.setField(captchaHelper,"moduleName","esignet");
captchaHelper = new CaptchaHelper(restTemplate, "https://api-internal.camdgc-dev1.mosip.net/v1/captcha/validatecaptcha",
"esignet");
}

@Test
public void validateCaptcha_WithNullCaptchaToken_ThrowsException() {
CaptchaHelper captchaHelper=new CaptchaHelper();
public void validateCaptcha_withNullCaptchaToken_thenFail() {
Assert.assertThrows(EsignetException.class,()->captchaHelper.validateCaptcha(null));
}

@Test
public void validateCaptcha_WithCaptchaToken_ThrowsException() {
CaptchaHelper captchaHelper=new CaptchaHelper();
public void validateCaptcha_withEmptyCaptchaToken_thenFail() {
Assert.assertThrows(EsignetException.class,()->captchaHelper.validateCaptcha(""));
}

@Test
public void validateCaptcha_WithNullResponse_ThrowsException() {
public void validateCaptcha_withNullResponse_thenFail() {
Mockito.when(restTemplate.exchange((RequestEntity<?>) any(), (Class<Object>) any())).thenReturn(null);
Assert.assertThrows(EsignetException.class,()->captchaHelper.validateCaptcha("captchaToken"));
}

@Test
public void validateCaptcha_SuccessfulResponse() {
public void validateCaptcha_validData_thenPass() {
ResponseWrapper responseWrapper = new ResponseWrapper();
responseWrapper.setResponse("success");
ResponseEntity<ResponseWrapper> responseEntity = ResponseEntity.ok(responseWrapper);
Expand All @@ -69,17 +66,17 @@ public void validateCaptcha_SuccessfulResponse() {
}

@Test
public void validateCaptcha_UnsuccessfulValidation_ThrowsEsignetException() {
public void validateCaptcha_unsuccessfulValidation_thenFail() {
ResponseWrapper responseWrapper = new ResponseWrapper();
responseWrapper.setErrors(new ArrayList<>());
responseWrapper.setErrors(Arrays.asList("server_unavailable"));
ResponseEntity<ResponseWrapper> responseEntity = ResponseEntity.ok(responseWrapper);
Mockito.when(restTemplate.exchange(Mockito.any(RequestEntity.class), Mockito.eq(ResponseWrapper.class)))
.thenReturn(responseEntity);
Assert.assertThrows(EsignetException.class, () -> captchaHelper.validateCaptcha("captchaToken"));
}

@Test
public void validateCaptcha_RequestException_ThrowsEsignetException() {
public void validateCaptcha_withRequestException_thenFail() {
Mockito.when(restTemplate.exchange(Mockito.any(RequestEntity.class), Mockito.eq(ResponseWrapper.class)))
.thenThrow(new RestClientException("Request failed"));
Assert.assertThrows(EsignetException.class, () -> captchaHelper.validateCaptcha("captchaToken"));
Expand Down

0 comments on commit bb85a58

Please sign in to comment.