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

11 tk 018 create rest endpoint to receive otp at obs #40

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions obs/obs-rest-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
</dependency>

<!-- Swagger Annotations -->

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations-jakarta</artifactId>
</dependency>

<!-- Spring Boot Starter for Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>

<!-- Spring Boot Starter for Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.adorsys.webank.obs.resource;

import com.adorsys.webank.obs.dto.OtpRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "OTP", description = "Operations related to OTP processing")
@RequestMapping("/api/otp")
public interface OtpRestApi {

@Operation(summary = "Receive OTP", description = "Receives OTP and processes it")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OTP successfully captured"),
@ApiResponse(responseCode = "400", description = "OTP capture failed")
})
@PostMapping(value = "/receive", consumes = "application/json", produces = "application/json")
ResponseEntity<?> receiveOtp(@RequestBody OtpRequest otpRequest);
}

10 changes: 8 additions & 2 deletions obs/obs-rest-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.adorsys.webank</groupId>
Expand All @@ -52,7 +51,14 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>

<!-- Other existing dependencies -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.adorsys.webank.obs.resource;

import com.adorsys.webank.obs.dto.OtpRequest;
import com.adorsys.webank.obs.service.OtpServiceApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/otp")
public class OtpRestServer implements OtpRestApi {

private final OtpServiceApi otpServiceApi;

@Autowired
public OtpRestServer(OtpServiceApi otpServiceApi) {
this.otpServiceApi = otpServiceApi;
}

@Override
public ResponseEntity<?> receiveOtp(@RequestBody OtpRequest otpRequest) {
try {
// Delegate OTP validation and processing to the service
String responseMessage = otpServiceApi.receiveOtp(otpRequest.getOtp());

return ResponseEntity.ok(Map.of("message", responseMessage));
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.adorsys.webank.obs.resource;

import com.adorsys.webank.obs.dto.OtpRequest;
import com.adorsys.webank.obs.service.OtpServiceApi;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

public class OtpRestServerTest {

@InjectMocks
private OtpRestServer otpRestServer; // Class under test

@Mock
private OtpServiceApi otpServiceApi; // Mocked dependency

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void receiveOtp_ShouldReturnSuccessfulResponse() {
// Arrange
OtpRequest otpRequest = new OtpRequest();
otpRequest.setOtp("123456");

String expectedResponse = "OTP is valid";
when(otpServiceApi.receiveOtp(otpRequest.getOtp())).thenReturn(expectedResponse);

// Act
ResponseEntity<?> responseEntity = otpRestServer.receiveOtp(otpRequest);

// Assert
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());

assertEquals(Map.of("message", "OTP is valid"), responseEntity.getBody());
}

@Test
void receiveOtp_ShouldReturnBadRequestForInvalidOtp() {
// Arrange
OtpRequest otpRequest = new OtpRequest();
otpRequest.setOtp("invalid");

String expectedErrorMessage = "Invalid OTP";
when(otpServiceApi.receiveOtp(otpRequest.getOtp())).thenThrow(new IllegalArgumentException(expectedErrorMessage));

// Act
ResponseEntity<?> responseEntity = otpRestServer.receiveOtp(otpRequest);

// Assert
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
assertEquals(Map.of("message", expectedErrorMessage), responseEntity.getBody());
}
}

10 changes: 8 additions & 2 deletions obs/obs-service-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -35,8 +34,15 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.adorsys.webank.obs.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public class OtpRequest {
@JsonProperty("otp")
private String otp;

public String getOtp() {
return otp;
}

public void setOtp(String otp) {
this.otp = otp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.adorsys.webank.obs.service;

import jakarta.ws.rs.Consumes;

@Consumes("application/json")
public interface OtpServiceApi {
/**
* Method to receive and process the OTP.
*
* @param otp The OTP string received.
*/
String receiveOtp(String otp);
}

1 change: 0 additions & 1 deletion obs/obs-service-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.adorsys.webank.obs.serviceimpl;

import com.adorsys.webank.obs.service.OtpServiceApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class OtpServiceImpl implements OtpServiceApi {
private static final Logger logger = LoggerFactory.getLogger(OtpServiceImpl.class);

@Override
public String receiveOtp(String otp) {
if (otp == null || otp.length() != 6) {
logger.error("Invalid OTP format received.");
throw new IllegalArgumentException("Invalid OTP format");
}

// Add actual OTP processing logic here
logger.info("OTP successfully processed: {}", otp);
return "OTP successfully received";
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.adorsys.webank.obs.serviceimpl;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

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

class OtpServiceImplTest {

@InjectMocks
private OtpServiceImpl otpService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void testReceiveOTP_valid() {
String otp = "123456";

// Call the method
String result = otpService.receiveOtp(otp);

// Assertions
assertNotNull(result);
assertEquals("OTP successfully received", result);
}

@Test
void testReceiveOTP_invalid() {
String otp = null;

// Call the method
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
otpService.receiveOtp(otp);
});

// Verify exception message
assertEquals("Invalid OTP format", exception.getMessage());
}
}

5 changes: 0 additions & 5 deletions online-banking-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>



<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -81,7 +77,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,17 @@


<!-- swagger -->

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-ui.version}</version>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations-jakarta</artifactId>
<version>2.2.22</version>
<scope>compile</scope>
</dependency>

<!-- logging dependencies -->
Expand Down