Skip to content

Commit

Permalink
Fix async implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sdikyarts committed May 25, 2024
1 parent 67ab8fb commit 37fe08e
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@RestController
Expand All @@ -29,26 +30,38 @@ public CompletableFuture<ResponseEntity<AdminSubscription>> create(@RequestBody

@GetMapping("/list")
public CompletableFuture<ResponseEntity<List<AdminDTO>>> findAll() {
return adminService.findAll().thenApply(ResponseEntity::ok)
.exceptionally(ex -> ResponseEntity.badRequest().build());
return adminService.findAll().thenApply(ResponseEntity::ok);
}

@GetMapping("/{subscriptionId}")
public CompletableFuture<ResponseEntity<Optional<AdminDTO>>> findById(@PathVariable String subscriptionId) {
public CompletableFuture<ResponseEntity<AdminDTO>> findById(@PathVariable String subscriptionId) {
try {
UUID.fromString(subscriptionId);
} catch (IllegalArgumentException e) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}
return adminService.findById(subscriptionId).thenApply(ResponseEntity::ok)
.exceptionally(ex -> ResponseEntity.badRequest().build());
.exceptionally(ex -> ResponseEntity.notFound().build());
}


@PutMapping("/update")
public CompletableFuture<ResponseEntity<AdminSubscription>> update(@RequestBody AdminDTO adminDTO) {
if (adminDTO.getSubscriptionId() == null) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}

return adminService.update(adminDTO).thenApply(ResponseEntity::ok)
.exceptionally(ex -> ResponseEntity.badRequest().build());
.exceptionally(ex -> ResponseEntity.notFound().build());
}

@DeleteMapping("/{subscriptionId}")
public CompletableFuture<ResponseEntity<String>> delete(@PathVariable String subscriptionId) {
return adminService.delete(subscriptionId).thenApply(result -> ResponseEntity.ok("DELETE SUCCESS"))
try {
UUID.fromString(subscriptionId);
} catch (IllegalArgumentException e) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}
return adminService.delete(subscriptionId).thenApply(deleted -> ResponseEntity.ok("DELETE SUCCESS"))
.exceptionally(ex -> ResponseEntity.notFound().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
public class DTOMapper {

public static AdminDTO convertModelToDto(AdminSubscription adminSubscription) {
return new AdminDTO(
adminSubscription.getSubscriptionId(),
adminSubscription.getUniqueCode(),
adminSubscription.getSubscriberName(),
adminSubscription.getSubscriberId(),
adminSubscription.getSubscriptionBoxId(),
adminSubscription.getSubscriptionStatus()
);
AdminDTO adminDTO = new AdminDTO();
adminDTO.setSubscriptionId(adminSubscription.getSubscriptionId());
adminDTO.setSubscriptionType(adminSubscription.getSubscriptionType());
adminDTO.setSubscriberName(adminSubscription.getSubscriberName());
adminDTO.setSubscriberId(adminSubscription.getSubscriberId());
adminDTO.setSubscriptionBoxId(adminSubscription.getSubscriptionBoxId());
adminDTO.setSubscriptionStatus(adminSubscription.getSubscriptionStatus());
adminDTO.setUniqueCode(adminSubscription.getUniqueCode());
return adminDTO;
}

public static AdminSubscription convertDTOtoModel(AdminDTO adminDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface AdminService {
CompletableFuture<AdminSubscription> create(AdminDTO adminDTO);
CompletableFuture<List<AdminDTO>> findAll();
CompletableFuture<Optional<AdminDTO>> findById(String subscriptionId);
CompletableFuture<AdminDTO> findById(String subscriptionId);
CompletableFuture<AdminSubscription> update(AdminDTO adminDTO);
CompletableFuture<Void> delete(String subscriptionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import org.springframework.scheduling.annotation.Async;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import snackscription.subscriptionadmin.dto.DTOMapper;


@Service
public class AdminServiceImpl implements AdminService{

Expand All @@ -30,23 +30,24 @@ public CompletableFuture<AdminSubscription> create(AdminDTO adminDTO) {
@Override
@Async
public CompletableFuture<List<AdminDTO>> findAll() {
return CompletableFuture.completedFuture(adminRepository.findAll()
.stream()
List<AdminSubscription> adminSubscriptions = adminRepository.findAll();
List<AdminDTO> adminDtos = adminSubscriptions.stream()
.map(DTOMapper::convertModelToDto)
.collect(Collectors.toList()));
.collect(Collectors.toList());
return CompletableFuture.completedFuture(adminDtos);
}

@Override
@Async
public CompletableFuture<Optional<AdminDTO>> findById(String subscriptionId) {
public CompletableFuture<AdminDTO> findById(String subscriptionId) {
if(subscriptionId == null || subscriptionId.isBlank()){
throw new IllegalArgumentException("ID cannot be null or empty");
}

AdminSubscription adminSubscription = adminRepository.findById(subscriptionId)
return adminRepository.findById(subscriptionId)
.map(DTOMapper::convertModelToDto)
.map(CompletableFuture::completedFuture)
.orElseThrow(() -> new IllegalArgumentException("Subscription not found"));

return CompletableFuture.completedFuture(Optional.of(DTOMapper.convertModelToDto(adminSubscription)));
}

@Override
Expand All @@ -56,11 +57,11 @@ public CompletableFuture<AdminSubscription> update(AdminDTO adminDTO) {
throw new IllegalArgumentException("AdminDTO cannot be null");
}

AdminSubscription adminSubscription = adminRepository.findById(adminDTO.getSubscriptionId())
.orElseThrow(() -> new IllegalArgumentException("Subscription not found"));

DTOMapper.updateAdminSubscription(adminSubscription, adminDTO);
return CompletableFuture.completedFuture(adminRepository.update(adminSubscription));
return adminRepository.findById(adminDTO.getSubscriptionId()).
map(adminSubscription -> {
DTOMapper.updateAdminSubscription(adminSubscription, adminDTO);
return CompletableFuture.completedFuture(adminRepository.update(adminSubscription));
}).orElseThrow(() -> new IllegalArgumentException("Subscription not found"));
}

@Override
Expand All @@ -70,6 +71,9 @@ public CompletableFuture<Void> delete(String subscriptionId) {
throw new IllegalArgumentException("ID cannot be null or empty");
}

if (adminRepository.findById(subscriptionId).isEmpty()) {
throw new IllegalArgumentException("Subscription not found");
}
adminRepository.delete(subscriptionId);
return CompletableFuture.completedFuture(null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/subscription-admin/
spring.datasource.url=jdbc:postgresql://postgres:5432/subscription-admin/
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create-drop
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.datasource.url=jdbc:postgresql://aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres?user=postgres.thnfvhxtvcbfxndlxdze&password=YouMakeStrayKidsStay
spring.datasource.url=jdbc:postgresql://aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres
spring.datasource.username=postgres.thnfvhxtvcbfxndlxdze
spring.datasource.password=YouMakeStrayKidsStay
spring.jpa.hibernate.ddl-auto=update
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.datasource.url=jdbc:postgresql://postgres:5432/subscription-admin
spring.datasource.url=jdbc:postgresql://postgres:5432/subscription-admin/
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package snackscription.subscriptionadmin.controller;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand Down Expand Up @@ -28,7 +28,7 @@ public class AdminControllerTest {
private AdminDTO adminDTO;
private AdminSubscription adminSubscription;

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

Expand All @@ -53,54 +53,51 @@ void setUp(){
void testCreate(){
when(adminService.create(adminDTO)).thenReturn(CompletableFuture.completedFuture(adminSubscription));

CompletableFuture<ResponseEntity<AdminSubscription>> result = adminController.create(adminDTO);

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(adminSubscription), result.join());
CompletableFuture<ResponseEntity<AdminSubscription>> response = adminController.create(adminDTO);
assertNotNull(response);
assertEquals(ResponseEntity.ok(adminSubscription), response.join());
}

@Test
void testFindAll(){
when(adminService.findAll()).thenReturn(CompletableFuture.completedFuture(Collections.singletonList(adminDTO)));
List<AdminDTO> adminDTOList = Collections.singletonList(adminDTO);
when(adminService.findAll()).thenReturn(CompletableFuture.completedFuture(adminDTOList));

CompletableFuture<ResponseEntity<List<AdminDTO>>> result = adminController.findAll();

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(Collections.singletonList(adminDTO)), result.join());
CompletableFuture<ResponseEntity<List<AdminDTO>>> response = adminController.findAll();
assertNotNull(response);
assertEquals(ResponseEntity.ok(adminDTOList), response.join());
}

@Test
void testFindById(){
when(adminService.findById("1")).thenReturn(CompletableFuture.completedFuture(Optional.of(adminDTO)));
String validUUID = "8a56e04b-d0c8-4e43-b2e0-fdf43e304d9e";
adminDTO.setSubscriptionId(validUUID);
adminSubscription.setSubscriptionId(validUUID);
when(adminService.findById(validUUID)).thenReturn(CompletableFuture.completedFuture(adminDTO));

CompletableFuture<ResponseEntity<Optional<AdminDTO>>> result = adminController.findById("1");
CompletableFuture<ResponseEntity<AdminDTO>> result = adminController.findById(validUUID);

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(Optional.of(adminDTO)), result.join());
assertEquals(ResponseEntity.ok(adminDTO), result.join());
}

@Test
void testUpdate(){
when(adminService.update(adminDTO)).thenReturn(CompletableFuture.completedFuture(adminSubscription));

CompletableFuture<ResponseEntity<AdminSubscription>> result = adminController.update(adminDTO);

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(adminSubscription), result.join());
CompletableFuture<ResponseEntity<AdminSubscription>> response = adminController.update(adminDTO);
assertNotNull(response);
assertEquals(ResponseEntity.ok(adminSubscription), response.join());
}

@Test
void testDelete(){
when(adminService.delete("1")).thenReturn(CompletableFuture.completedFuture(null));
String validUUID = "8a56e04b-d0c8-4e43-b2e0-fdf43e304d9e";
when(adminService.delete(validUUID)).thenReturn(CompletableFuture.completedFuture(null));

CompletableFuture<ResponseEntity<String>> result = adminController.delete("1");

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok("DELETE SUCCESS"), result.join());
CompletableFuture<ResponseEntity<String>> response = adminController.delete(validUUID);
assertNotNull(response);
assertEquals(ResponseEntity.ok("DELETE SUCCESS"), response.join());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;
import snackscription.subscriptionadmin.controller.AdminController;
import snackscription.subscriptionadmin.dto.AdminDTO;
Expand All @@ -19,6 +21,7 @@
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@ExtendWith(MockitoExtension.class)
public class AdminServiceImplTest {

@Mock
Expand Down Expand Up @@ -53,13 +56,12 @@ void setUp() {
adminSubscription.setSubscriberId("0325");
adminSubscription.setSubscriptionBoxId("143ily");
adminSubscription.setSubscriptionStatus("PENDING");

when(adminSubscriptionFactory.create(anyString(), anyString(), anyString(), anyString())).thenReturn(adminSubscription);
}


@Test
void testCreate() {
when(adminRepository.create(adminSubscription)).thenReturn(adminSubscription);
when(adminRepository.create(any(AdminSubscription.class))).thenReturn(adminSubscription);

CompletableFuture<AdminSubscription> result = adminService.create(adminDTO);

Expand All @@ -70,51 +72,49 @@ void testCreate() {

@Test
void testFindAll() {
List<AdminDTO> adminDTOList = List.of(adminDTO);

when(adminService.findAll()).thenReturn(CompletableFuture.completedFuture(adminDTOList));
List<AdminSubscription> adminSubscriptions = List.of(adminSubscription);
when(adminRepository.findAll()).thenReturn(adminSubscriptions);

CompletableFuture<ResponseEntity<List<AdminDTO>>> result = adminController.findAll();
CompletableFuture<List<AdminDTO>> result = adminService.findAll();

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(adminDTOList), result.join());
assertEquals(List.of(adminDTO), result.join());
}

@Test
void testFindById() {
Optional<AdminDTO> adminDTOOptional = Optional.of(adminDTO);
String id = "1";
when(adminRepository.findById(id)).thenReturn(Optional.of(adminSubscription));

when(adminRepository.findById(anyString())).thenReturn(Optional.of(adminSubscription));
when(adminService.findById(anyString())).thenReturn(CompletableFuture.completedFuture(adminDTOOptional));

CompletableFuture<ResponseEntity<Optional<AdminDTO>>> result = adminController.findById("1");
CompletableFuture<AdminDTO> result = adminService.findById(id);

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(adminDTOOptional), result.join());
assertEquals(adminDTO, result.join());
}

@Test
void testUpdate() {
when(adminService.findById(adminDTO.getSubscriptionId())).thenReturn(CompletableFuture.completedFuture(Optional.of(adminDTO)));
when(adminService.update(adminDTO)).thenReturn(CompletableFuture.completedFuture(adminSubscription));
when(adminRepository.findById(adminDTO.getSubscriptionId())).thenReturn(Optional.of(adminSubscription));
when(adminRepository.update(adminSubscription)).thenReturn(adminSubscription);

CompletableFuture<ResponseEntity<AdminSubscription>> result = adminController.update(adminDTO);
CompletableFuture<AdminSubscription> result = adminService.update(adminDTO);

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok(adminSubscription), result.join());
assertEquals(adminSubscription, result.join());
}

@Test
void testDelete() {
when(adminService.delete(anyString())).thenReturn(CompletableFuture.completedFuture(null));
String id = "1";
when(adminRepository.findById(id)).thenReturn(Optional.of(adminSubscription));

CompletableFuture<ResponseEntity<String>> result = adminController.delete("1");
CompletableFuture<Void> result = adminService.delete(id);

assertNotNull(result);
assertTrue(result.isDone());
assertEquals(ResponseEntity.ok("DELETE SUCCESS"), result.join());
assertNull(result.join());
}
}

0 comments on commit 37fe08e

Please sign in to comment.