From 8ee0fbb20e9166b916fa8b38b5416c5f6c7a797d Mon Sep 17 00:00:00 2001 From: Menkene Koufan Date: Wed, 23 Oct 2024 14:28:09 +0100 Subject: [PATCH 1/5] chore: created end point on OBS to receive OTP --- .../webank/obs/resource/OtpRestApi.java | 31 +++++ .../webank/obs/resource/OtpRestServer.java | 31 +++++ .../resource/RegistrationResourceTest.java | 122 +++++++++--------- .../webank/obs/service/OtpServiceApi.java | 11 ++ .../obs/serviceimpl/OtpServiceImpl.java | 27 ++++ 5 files changed, 161 insertions(+), 61 deletions(-) create mode 100644 obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java create mode 100644 obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java create mode 100644 obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java create mode 100644 obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java diff --git a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java new file mode 100644 index 0000000..730d461 --- /dev/null +++ b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java @@ -0,0 +1,31 @@ +package com.adorsys.webank.obs.resource; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +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/v1/otp") +public interface OtpRestApi { + + /** + * Endpoint to receive the OTP. + * + * @param otp The OTP string. + * @return ResponseEntity indicating the result of the operation. + */ + @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("/receive") + ResponseEntity receiveOtp( + @Parameter(required = true) + @RequestParam("OTP") String otp + ); +} diff --git a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java new file mode 100644 index 0000000..0c964e5 --- /dev/null +++ b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java @@ -0,0 +1,31 @@ +package com.adorsys.webank.obs.resource; + +import com.adorsys.webank.obs.service.OtpServiceApi; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class OtpRestServer implements OtpRestApi { + + private final OtpServiceApi otpServiceApi; + + @Autowired + public OtpRestServer(OtpServiceApi otpServiceApi) { + this.otpServiceApi = otpServiceApi; + } + + @Override + public ResponseEntity receiveOtp(String otp) { + try { + otpServiceApi.receiveOtp(otp); + return new ResponseEntity<>("OTP received and processed successfully.", HttpStatus.OK); + } catch (IllegalArgumentException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } catch (Exception e) { + return new ResponseEntity<>("Failed to process OTP.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } +} + diff --git a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java index b398a7b..165733d 100644 --- a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java +++ b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java @@ -1,63 +1,63 @@ -package com.adorsys.webank.obs.resource; - -import com.adorsys.webank.obs.service.RegistrationServiceApi; -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.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; - -import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -public class RegistrationResourceTest { - - @InjectMocks - private RegistrationResource registrationResource; - - @Mock - private RegistrationServiceApi registrationService; - - private MockMvc mockMvc; - - @BeforeEach - public void setUp() { - MockitoAnnotations.openMocks(this); - mockMvc = MockMvcBuilders.standaloneSetup(registrationResource).build(); - } - - @Test - public void testRegisterAccountSuccess() throws Exception { - // Arrange - String phoneNumber = "1234567890"; - String publicKey = "dummyPublicKey"; - String expectedResponse = "Registration successful"; // Adjust according to your service response - when(registrationService.registerAccount(phoneNumber, publicKey)).thenReturn(expectedResponse); - - // Act & Assert - mockMvc.perform(post("/api/registration") - .param("phoneNumber", phoneNumber) - .param("publicKey", publicKey)) - .andExpect(status().isOk()) - .andExpect(content().string(expectedResponse)); - } - - @Test - public void testRegisterAccountServiceThrowsException() throws Exception { - // Arrange - String phoneNumber = "1234567890"; - String publicKey = "dummyPublicKey"; - when(registrationService.registerAccount(phoneNumber, publicKey)).thenThrow(new RuntimeException("Service error")); - - // Act & Assert - mockMvc.perform(post("/api/registration") - .param("phoneNumber", phoneNumber) - .param("publicKey", publicKey)) - .andExpect(status().isInternalServerError()); - } +//package com.adorsys.webank.obs.resource; +// +//import com.adorsys.webank.obs.service.RegistrationServiceApi; +//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.test.web.servlet.MockMvc; +//import org.springframework.test.web.servlet.setup.MockMvcBuilders; +// +//import static org.mockito.Mockito.*; +//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +// +//public class RegistrationResourceTest { +// +// @InjectMocks +// private RegistrationResource registrationResource; +// +// @Mock +// private RegistrationServiceApi registrationService; +// +// private MockMvc mockMvc; +// +// @BeforeEach +// public void setUp() { +// MockitoAnnotations.openMocks(this); +// mockMvc = MockMvcBuilders.standaloneSetup(registrationResource).build(); +// } +// +// @Test +// public void testRegisterAccountSuccess() throws Exception { +// // Arrange +// String phoneNumber = "1234567890"; +// String publicKey = "dummyPublicKey"; +// String expectedResponse = "Registration successful"; // Adjust according to your service response +// when(registrationService.registerAccount(phoneNumber, publicKey)).thenReturn(expectedResponse); +// +// // Act & Assert +// mockMvc.perform(post("/api/registration") +// .param("phoneNumber", phoneNumber) +// .param("publicKey", publicKey)) +// .andExpect(status().isOk()) +// .andExpect(content().string(expectedResponse)); +// } +// +// @Test +// public void testRegisterAccountServiceThrowsException() throws Exception { +// // Arrange +// String phoneNumber = "1234567890"; +// String publicKey = "dummyPublicKey"; +// when(registrationService.registerAccount(phoneNumber, publicKey)).thenThrow(new RuntimeException("Service error")); +// +// // Act & Assert +// mockMvc.perform(post("/api/registration") +// .param("phoneNumber", phoneNumber) +// .param("publicKey", publicKey)) +// .andExpect(status().isInternalServerError()); +// } // @Test // public void testRegisterAccountInvalidInput() throws Exception { @@ -73,4 +73,4 @@ public void testRegisterAccountServiceThrowsException() throws Exception { // .andExpect(status().isBadRequest()) // .andExpect(content().string("Registration failed: Invalid input")); // You might need to adjust this // } -} +//} diff --git a/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java new file mode 100644 index 0000000..db7fdb0 --- /dev/null +++ b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java @@ -0,0 +1,11 @@ +package com.adorsys.webank.obs.service; + +public interface OtpServiceApi { + /** + * Method to receive and process the OTP. + * + * @param otp The OTP string received. + */ + void receiveOtp(String otp); +} + diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java new file mode 100644 index 0000000..0ffb657 --- /dev/null +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java @@ -0,0 +1,27 @@ +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 void receiveOtp(String otp) { + // Add the business logic here for handling the OTP + logger.info("Received OTP: {}", otp); + + // Example of processing the OTP (replace this with actual logic) + if (otp != null && otp.length() == 6) { + logger.info("OTP format is valid and being processed."); + // Additional processing logic here + } else { + logger.error("Invalid OTP format received."); + throw new IllegalArgumentException("Invalid OTP format"); + } + } +} + From 60c9b7aca54269074acf20c5123facd02a7a380a Mon Sep 17 00:00:00 2001 From: Menkene Koufan Date: Thu, 24 Oct 2024 12:58:50 +0100 Subject: [PATCH 2/5] chore: added test for otp endpoints --- .../webank/obs/resource/OtpRequest.java | 16 ++++ .../webank/obs/resource/OtpRestApi.java | 19 ++--- .../obs/resource/RegistrationResourceApi.java | 32 -------- obs/obs-rest-server/pom.xml | 9 +++ .../webank/obs/resource/OtpRestServer.java | 20 +++-- .../obs/resource/RegistrationResource.java | 30 -------- .../resource/RegistrationResourceTest.java | 76 ------------------- obs/obs-service-api/pom.xml | 5 ++ .../webank/obs/service/OtpServiceApi.java | 5 +- .../obs/service/RegistrationServiceApi.java | 10 --- .../obs/serviceimpl/OtpServiceImpl.java | 16 ++-- .../serviceimpl/RegistrationServiceImpl.java | 20 ----- .../obs/serviceimpl/OtpServiceImplTest.java | 45 +++++++++++ 13 files changed, 103 insertions(+), 200 deletions(-) create mode 100644 obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java delete mode 100644 obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/RegistrationResourceApi.java delete mode 100644 obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/RegistrationResource.java delete mode 100644 obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java delete mode 100644 obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/RegistrationServiceApi.java delete mode 100644 obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/RegistrationServiceImpl.java create mode 100644 obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImplTest.java diff --git a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java new file mode 100644 index 0000000..9998fef --- /dev/null +++ b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java @@ -0,0 +1,16 @@ +package com.adorsys.webank.obs.resource; + +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; + } +} \ No newline at end of file diff --git a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java index 730d461..3997aca 100644 --- a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java +++ b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java @@ -1,7 +1,6 @@ package com.adorsys.webank.obs.resource; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; @@ -9,23 +8,15 @@ import org.springframework.web.bind.annotation.*; @Tag(name = "OTP", description = "Operations related to OTP processing") -@RequestMapping("/api/v1/otp") +@RequestMapping("/api/otp") public interface OtpRestApi { - /** - * Endpoint to receive the OTP. - * - * @param otp The OTP string. - * @return ResponseEntity indicating the result of the operation. - */ - @Operation(summary = "Receive OTP", description = "receives OTP and processes it") + @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("/receive") - ResponseEntity receiveOtp( - @Parameter(required = true) - @RequestParam("OTP") String otp - ); + @PostMapping(value = "/receive", consumes = "application/json", produces = "application/json") + ResponseEntity receiveOtp(@RequestBody OtpRequest otpRequest); } + diff --git a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/RegistrationResourceApi.java b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/RegistrationResourceApi.java deleted file mode 100644 index 43ae36e..0000000 --- a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/RegistrationResourceApi.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2018-2024 adorsys GmbH and Co. KG - * All rights are reserved. - */ - -package com.adorsys.webank.obs.resource; - -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -@RestController -@RequestMapping("/api/registration") -public interface RegistrationResourceApi { - - @Operation(summary = "Register a new bank account", description = "Accepts a phone number and public key for registration") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "Registration successful"), - @ApiResponse(responseCode = "400", description = "Invalid input") - }) - @PostMapping - ResponseEntity registerAccount( - @Parameter(required = true) - @RequestParam("phoneNumber") String phoneNumber, - - @Parameter(required = true) - @RequestParam("publicKey") String publicKey - ); -} diff --git a/obs/obs-rest-server/pom.xml b/obs/obs-rest-server/pom.xml index 3543b9f..f699b1c 100644 --- a/obs/obs-rest-server/pom.xml +++ b/obs/obs-rest-server/pom.xml @@ -55,6 +55,15 @@ spring-boot-starter-test test + + org.springframework + spring-context + test + + + org.jetbrains + annotations + diff --git a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java index 0c964e5..9bd8d01 100644 --- a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java +++ b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java @@ -1,12 +1,17 @@ package com.adorsys.webank.obs.resource; import com.adorsys.webank.obs.service.OtpServiceApi; +import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; 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; @@ -17,15 +22,14 @@ public OtpRestServer(OtpServiceApi otpServiceApi) { } @Override - public ResponseEntity receiveOtp(String otp) { + public ResponseEntity receiveOtp(@RequestBody @NotNull OtpRequest otpRequest) { try { - otpServiceApi.receiveOtp(otp); - return new ResponseEntity<>("OTP received and processed successfully.", HttpStatus.OK); + // 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 new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); - } catch (Exception e) { - return new ResponseEntity<>("Failed to process OTP.", HttpStatus.INTERNAL_SERVER_ERROR); + return ResponseEntity.badRequest().body(Map.of("message", e.getMessage())); } } } - diff --git a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/RegistrationResource.java b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/RegistrationResource.java deleted file mode 100644 index de82044..0000000 --- a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/RegistrationResource.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2018-2024 adorsys GmbH and Co. KG - * All rights are reserved. - */ - -package com.adorsys.webank.obs.resource; - -import com.adorsys.webank.obs.service.RegistrationServiceApi; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class RegistrationResource implements RegistrationResourceApi { - - private final RegistrationServiceApi RegistrationService; - - @Autowired - public RegistrationResource(RegistrationServiceApi RegistrationService) { - this.RegistrationService = RegistrationService; - } - - @Override - public ResponseEntity registerAccount(String phoneNumber, String publicKey) { - // Call the RegistrationServiceApi to handle the registration logic - String response = RegistrationService.registerAccount(phoneNumber, publicKey); - - return ResponseEntity.ok(response); - } -} diff --git a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java deleted file mode 100644 index 165733d..0000000 --- a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/RegistrationResourceTest.java +++ /dev/null @@ -1,76 +0,0 @@ -//package com.adorsys.webank.obs.resource; -// -//import com.adorsys.webank.obs.service.RegistrationServiceApi; -//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.test.web.servlet.MockMvc; -//import org.springframework.test.web.servlet.setup.MockMvcBuilders; -// -//import static org.mockito.Mockito.*; -//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -// -//public class RegistrationResourceTest { -// -// @InjectMocks -// private RegistrationResource registrationResource; -// -// @Mock -// private RegistrationServiceApi registrationService; -// -// private MockMvc mockMvc; -// -// @BeforeEach -// public void setUp() { -// MockitoAnnotations.openMocks(this); -// mockMvc = MockMvcBuilders.standaloneSetup(registrationResource).build(); -// } -// -// @Test -// public void testRegisterAccountSuccess() throws Exception { -// // Arrange -// String phoneNumber = "1234567890"; -// String publicKey = "dummyPublicKey"; -// String expectedResponse = "Registration successful"; // Adjust according to your service response -// when(registrationService.registerAccount(phoneNumber, publicKey)).thenReturn(expectedResponse); -// -// // Act & Assert -// mockMvc.perform(post("/api/registration") -// .param("phoneNumber", phoneNumber) -// .param("publicKey", publicKey)) -// .andExpect(status().isOk()) -// .andExpect(content().string(expectedResponse)); -// } -// -// @Test -// public void testRegisterAccountServiceThrowsException() throws Exception { -// // Arrange -// String phoneNumber = "1234567890"; -// String publicKey = "dummyPublicKey"; -// when(registrationService.registerAccount(phoneNumber, publicKey)).thenThrow(new RuntimeException("Service error")); -// -// // Act & Assert -// mockMvc.perform(post("/api/registration") -// .param("phoneNumber", phoneNumber) -// .param("publicKey", publicKey)) -// .andExpect(status().isInternalServerError()); -// } - -// @Test -// public void testRegisterAccountInvalidInput() throws Exception { -// // Arrange -// String phoneNumber = ""; // Invalid input (empty phone number) -// String publicKey = "dummyPublicKey"; -// when(registrationService.registerAccount(phoneNumber, publicKey)).thenThrow(new InvalidInputException("Invalid input")); -// -// // Act & Assert -// mockMvc.perform(post("/api/registration") -// .param("phoneNumber", phoneNumber) -// .param("publicKey", publicKey)) -// .andExpect(status().isBadRequest()) -// .andExpect(content().string("Registration failed: Invalid input")); // You might need to adjust this -// } -//} diff --git a/obs/obs-service-api/pom.xml b/obs/obs-service-api/pom.xml index 8293b67..1f08613 100644 --- a/obs/obs-service-api/pom.xml +++ b/obs/obs-service-api/pom.xml @@ -36,6 +36,11 @@ spring-cloud-starter-openfeign + + jakarta.ws.rs + jakarta.ws.rs-api + 3.1.0 + diff --git a/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java index db7fdb0..30104b3 100644 --- a/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java +++ b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/OtpServiceApi.java @@ -1,11 +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. */ - void receiveOtp(String otp); + String receiveOtp(String otp); } diff --git a/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/RegistrationServiceApi.java b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/RegistrationServiceApi.java deleted file mode 100644 index fe96a95..0000000 --- a/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/service/RegistrationServiceApi.java +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (c) 2018-2024 adorsys GmbH and Co. KG - * All rights are reserved. - */ - -package com.adorsys.webank.obs.service; - -public interface RegistrationServiceApi { - String registerAccount(String phoneNumber, String publicKey); -} diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java index 0ffb657..5431494 100644 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImpl.java @@ -10,18 +10,16 @@ public class OtpServiceImpl implements OtpServiceApi { private static final Logger logger = LoggerFactory.getLogger(OtpServiceImpl.class); @Override - public void receiveOtp(String otp) { - // Add the business logic here for handling the OTP - logger.info("Received OTP: {}", otp); - - // Example of processing the OTP (replace this with actual logic) - if (otp != null && otp.length() == 6) { - logger.info("OTP format is valid and being processed."); - // Additional processing logic here - } else { + 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"; } } + diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/RegistrationServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/RegistrationServiceImpl.java deleted file mode 100644 index b09bf56..0000000 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/RegistrationServiceImpl.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2018-2024 adorsys GmbH and Co. KG - * All rights are reserved. - */ - -package com.adorsys.webank.obs.serviceimpl; - -import com.adorsys.webank.obs.service.RegistrationServiceApi; -import org.springframework.stereotype.Service; - -@Service -public class RegistrationServiceImpl implements RegistrationServiceApi { - - @Override - public String registerAccount(String phoneNumber, String publicKey) { - // For now, we just return a successful account creation, - //Have to implement the logic to foward the request to the module and get back a respond - return "Successfully registered account for phone number: " + phoneNumber; - } -} diff --git a/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImplTest.java b/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImplTest.java new file mode 100644 index 0000000..2054743 --- /dev/null +++ b/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/OtpServiceImplTest.java @@ -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()); + } +} + From 77295f3457f67cedada93cfdb0bc4cf1c05fbd89 Mon Sep 17 00:00:00 2001 From: Menkene Koufan Date: Fri, 25 Oct 2024 09:44:13 +0100 Subject: [PATCH 3/5] fix: test for otp endpoint --- .../obs/resource/OtpRestServerTest.java | 4 +++ .../webank/OnlineBankingApplicationTests.java | 26 +++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java diff --git a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java new file mode 100644 index 0000000..be03a75 --- /dev/null +++ b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java @@ -0,0 +1,4 @@ +package com.adorsys.webank.obs.resource; + +public class OtpRestServerTest { +} diff --git a/online-banking-app/src/test/java/com/adorsys/webank/OnlineBankingApplicationTests.java b/online-banking-app/src/test/java/com/adorsys/webank/OnlineBankingApplicationTests.java index 83a993e..32368ba 100644 --- a/online-banking-app/src/test/java/com/adorsys/webank/OnlineBankingApplicationTests.java +++ b/online-banking-app/src/test/java/com/adorsys/webank/OnlineBankingApplicationTests.java @@ -1,13 +1,13 @@ -//package com.adorsys.webank; -// -//import org.junit.jupiter.api.Test; -//import org.springframework.boot.test.context.SpringBootTest; -// -//@SpringBootTest -//class OnlineBankingApplicationTests { -// -// @Test -// void contextLoads() { -// // This test will pass if the application context loads successfully -// } -//} +package com.adorsys.webank; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class OnlineBankingApplicationTests { + + @Test + void contextLoads() { + // This test will pass if the application context loads successfully + } +} From 01192e9f89035e76815fd6be8821896a0297ec9f Mon Sep 17 00:00:00 2001 From: Menkene Koufan Date: Fri, 25 Oct 2024 09:46:22 +0100 Subject: [PATCH 4/5] (update)fix: test for otp endpoint --- obs/obs-rest-api/pom.xml | 6 +- .../webank/obs/resource/OtpRestApi.java | 1 + obs/obs-rest-server/pom.xml | 1 - .../webank/obs/resource/OtpRestServer.java | 4 +- .../obs/resource/OtpRestServerTest.java | 62 +++++++++++++++++++ obs/obs-service-api/pom.xml | 7 ++- .../adorsys/webank/obs/dto}/OtpRequest.java | 2 +- pom.xml | 7 +++ 8 files changed, 80 insertions(+), 10 deletions(-) rename obs/{obs-rest-api/src/main/java/com/adorsys/webank/obs/resource => obs-service-api/src/main/java/com/adorsys/webank/obs/dto}/OtpRequest.java (86%) diff --git a/obs/obs-rest-api/pom.xml b/obs/obs-rest-api/pom.xml index 77db176..74a828b 100644 --- a/obs/obs-rest-api/pom.xml +++ b/obs/obs-rest-api/pom.xml @@ -33,7 +33,6 @@ io.swagger.core.v3 swagger-annotations-jakarta - 2.2.22 compile @@ -41,7 +40,6 @@ org.springframework.boot spring-boot-starter-web - 3.1.4 compile @@ -49,14 +47,14 @@ org.springframework.boot spring-boot-starter-test - 3.1.4 test + org.springdoc springdoc-openapi-starter-webmvc-ui - 2.1.0 + diff --git a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java index 3997aca..9608456 100644 --- a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java +++ b/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRestApi.java @@ -1,5 +1,6 @@ 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; diff --git a/obs/obs-rest-server/pom.xml b/obs/obs-rest-server/pom.xml index f699b1c..fa5e00b 100644 --- a/obs/obs-rest-server/pom.xml +++ b/obs/obs-rest-server/pom.xml @@ -35,7 +35,6 @@ org.projectlombok lombok - 1.18.24 provided diff --git a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java index 9bd8d01..a39246d 100644 --- a/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java +++ b/obs/obs-rest-server/src/main/java/com/adorsys/webank/obs/resource/OtpRestServer.java @@ -1,7 +1,7 @@ package com.adorsys.webank.obs.resource; +import com.adorsys.webank.obs.dto.OtpRequest; import com.adorsys.webank.obs.service.OtpServiceApi; -import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; @@ -22,7 +22,7 @@ public OtpRestServer(OtpServiceApi otpServiceApi) { } @Override - public ResponseEntity receiveOtp(@RequestBody @NotNull OtpRequest otpRequest) { + public ResponseEntity receiveOtp(@RequestBody OtpRequest otpRequest) { try { // Delegate OTP validation and processing to the service String responseMessage = otpServiceApi.receiveOtp(otpRequest.getOtp()); diff --git a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java index be03a75..822b937 100644 --- a/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java +++ b/obs/obs-rest-server/src/test/java/com/adorsys/webank/obs/resource/OtpRestServerTest.java @@ -1,4 +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()); + } } + diff --git a/obs/obs-service-api/pom.xml b/obs/obs-service-api/pom.xml index 1f08613..92fb833 100644 --- a/obs/obs-service-api/pom.xml +++ b/obs/obs-service-api/pom.xml @@ -25,7 +25,6 @@ - org.springframework.boot spring-boot-starter-data-jpa @@ -39,7 +38,11 @@ jakarta.ws.rs jakarta.ws.rs-api - 3.1.0 + + + + com.fasterxml.jackson.core + jackson-annotations diff --git a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/dto/OtpRequest.java similarity index 86% rename from obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java rename to obs/obs-service-api/src/main/java/com/adorsys/webank/obs/dto/OtpRequest.java index 9998fef..51ebbac 100644 --- a/obs/obs-rest-api/src/main/java/com/adorsys/webank/obs/resource/OtpRequest.java +++ b/obs/obs-service-api/src/main/java/com/adorsys/webank/obs/dto/OtpRequest.java @@ -1,4 +1,4 @@ -package com.adorsys.webank.obs.resource; +package com.adorsys.webank.obs.dto; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/pom.xml b/pom.xml index 1cebc39..fadbc33 100644 --- a/pom.xml +++ b/pom.xml @@ -219,6 +219,13 @@ ${springdoc-openapi-ui.version} + + io.swagger.core.v3 + swagger-annotations-jakarta + 2.2.22 + compile + + From bf54a745a9f1479fcab6b50e52415b07a79a64fb Mon Sep 17 00:00:00 2001 From: Menkene Koufan Date: Fri, 25 Oct 2024 13:15:25 +0100 Subject: [PATCH 5/5] (update)fix: test for otp endpoint --- obs/obs-rest-api/pom.xml | 3 --- obs/obs-rest-server/pom.xml | 3 --- obs/obs-service-impl/pom.xml | 1 - online-banking-app/pom.xml | 4 ---- pom.xml | 3 +-- 5 files changed, 1 insertion(+), 13 deletions(-) diff --git a/obs/obs-rest-api/pom.xml b/obs/obs-rest-api/pom.xml index 74a828b..01561b5 100644 --- a/obs/obs-rest-api/pom.xml +++ b/obs/obs-rest-api/pom.xml @@ -33,21 +33,18 @@ io.swagger.core.v3 swagger-annotations-jakarta - compile org.springframework.boot spring-boot-starter-web - compile org.springframework.boot spring-boot-starter-test - test diff --git a/obs/obs-rest-server/pom.xml b/obs/obs-rest-server/pom.xml index fa5e00b..772970c 100644 --- a/obs/obs-rest-server/pom.xml +++ b/obs/obs-rest-server/pom.xml @@ -35,7 +35,6 @@ org.projectlombok lombok - provided com.adorsys.webank @@ -52,12 +51,10 @@ org.springframework.boot spring-boot-starter-test - test org.springframework spring-context - test org.jetbrains diff --git a/obs/obs-service-impl/pom.xml b/obs/obs-service-impl/pom.xml index c5b2995..b998637 100644 --- a/obs/obs-service-impl/pom.xml +++ b/obs/obs-service-impl/pom.xml @@ -38,7 +38,6 @@ org.springframework spring-test - test diff --git a/online-banking-app/pom.xml b/online-banking-app/pom.xml index ce66e2c..313603d 100644 --- a/online-banking-app/pom.xml +++ b/online-banking-app/pom.xml @@ -70,11 +70,8 @@ com.h2database h2 - runtime - - org.projectlombok lombok @@ -83,7 +80,6 @@ org.springframework.boot spring-boot-starter-test - test diff --git a/pom.xml b/pom.xml index fadbc33..6ea259d 100644 --- a/pom.xml +++ b/pom.xml @@ -212,11 +212,10 @@ - org.springdoc springdoc-openapi-starter-webmvc-ui - ${springdoc-openapi-ui.version} + 2.1.0