Skip to content

Commit

Permalink
[FIX] fix error on getmapping methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pesolosep committed May 26, 2024
1 parent 3cf0326 commit b84088e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.DTOMapper;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.LogAdmin;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils.JWTUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -59,7 +60,7 @@ public CompletableFuture<ResponseEntity<SubscriptionBox>> createSubscriptionBox(
}

@GetMapping("/list")
public CompletableFuture<ResponseEntity<List<SubscriptionBox>>> findAll(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException {
public CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> findAll(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException {
validateToken(token);
return subscriptionBoxService.findAll()
.thenApply(ResponseEntity::ok);
Expand Down Expand Up @@ -94,7 +95,8 @@ public CompletableFuture<ResponseEntity<SubscriptionBoxDTO>> findById(@RequestHe

return subscriptionBoxService.findById(id)
.thenApply(optionalSubscriptionBox ->
optionalSubscriptionBox.map(ResponseEntity::ok)
optionalSubscriptionBox
.map(subscriptionBox -> ResponseEntity.ok(DTOMapper.convertModelToDto(subscriptionBox)))
.orElse(ResponseEntity.notFound().build()));
}

Expand All @@ -119,7 +121,6 @@ public CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> findByPriceLe
.thenApply(ResponseEntity::ok);
}


@GetMapping("/price/greater-than/{price}")
public CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> findByPriceGreaterThan(@RequestHeader(value = "Authorization") String token, @PathVariable int price) throws IllegalAccessException {
validateToken(token);
Expand All @@ -142,6 +143,7 @@ public CompletableFuture<ResponseEntity<Optional<List<SubscriptionBoxDTO>>>> fin
.thenApply(ResponseEntity::ok);
}


@GetMapping("/distinct-names")
public CompletableFuture<ResponseEntity<Optional<List<String>>>> findDistinctNames(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException {
validateToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ private boolean existsByNameAndType(String name, String type) {
}

@Transactional
public Optional<SubscriptionBox> findById(String id){
SubscriptionBox subscriptionBox = entityManager.find(SubscriptionBox.class, id);
return Optional.ofNullable(subscriptionBox);
public Optional<SubscriptionBox> findById(String id) {
String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id";
TypedQuery<SubscriptionBox> query = entityManager.createQuery(jpql, SubscriptionBox.class);
query.setParameter("id", id);
return query.getResultStream().findFirst();
}

@Transactional
public List<SubscriptionBox> findAll(){
String jpql = "SELECT sb FROM SubscriptionBox sb";
public List<SubscriptionBox> findAll() {
String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items";
TypedQuery<SubscriptionBox> query = entityManager.createQuery(jpql, SubscriptionBox.class);
return query.getResultList();
}

@Transactional
public SubscriptionBox update(SubscriptionBox subscriptionBox){
public SubscriptionBox update(SubscriptionBox subscriptionBox) {
return entityManager.merge(subscriptionBox);
}

@Transactional
public void delete(String id) {
SubscriptionBox subscriptionBox = findById(id)
Expand All @@ -95,31 +96,31 @@ public void delete(String id) {

@Transactional
public List<SubscriptionBox> findByPriceLessThan(int price) {
String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price";
String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price < :price";
TypedQuery<SubscriptionBox> query = entityManager.createQuery(jpql, SubscriptionBox.class);
query.setParameter("price", price);
return query.getResultList();
}

@Transactional
public List<SubscriptionBox> findByPriceGreaterThan(int price) {
String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price";
String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price > :price";
TypedQuery<SubscriptionBox> query = entityManager.createQuery(jpql, SubscriptionBox.class);
query.setParameter("price", price);
return query.getResultList();
}

@Transactional
public List<SubscriptionBox> findByPriceEquals(int price) {
String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price";
String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price = :price";
TypedQuery<SubscriptionBox> query = entityManager.createQuery(jpql, SubscriptionBox.class);
query.setParameter("price", price);
return query.getResultList();
}

@Transactional
public Optional<List<SubscriptionBox>> findByName(String name) {
String jpql = "SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)";
String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE LOWER(sb.name) LIKE LOWER(:name)";
TypedQuery<SubscriptionBox> query = entityManager.createQuery(jpql, SubscriptionBox.class);
query.setParameter("name", "%" + name.toLowerCase() + "%"); // Convert input name to lowercase
List<SubscriptionBox> result = query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
public interface SubscriptionBoxService {
CompletableFuture<SubscriptionBox> save(SubscriptionBoxDTO subscriptionBoxDTO);

CompletableFuture<Optional<SubscriptionBoxDTO>> findById(String id);
CompletableFuture<Optional<SubscriptionBox>> findById(String id);

CompletableFuture<List<SubscriptionBox>> findAll();
CompletableFuture<List<SubscriptionBoxDTO>> findAll();

CompletableFuture<SubscriptionBox> update(SubscriptionBoxDTO subscriptionBoxDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,36 @@ public CompletableFuture<SubscriptionBox> save(SubscriptionBoxDTO subscriptionBo

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

return subscriptionBoxRepository.findById(id)
.map(subscriptionBox -> CompletableFuture.completedFuture(Optional.of(DTOMapper.convertModelToDto(subscriptionBox))))
.orElse(CompletableFuture.completedFuture(Optional.empty()));

public CompletableFuture<Optional<SubscriptionBox>> findById(String id) {
Optional<SubscriptionBox> subscriptionBox = subscriptionBoxRepository.findById(id);
return CompletableFuture.completedFuture(subscriptionBox);
}


@Override
@Async
public CompletableFuture<List<SubscriptionBox>> findAll() {
public CompletableFuture<List<SubscriptionBoxDTO>> findAll() {
List<SubscriptionBox> subscriptionBoxes = subscriptionBoxRepository.findAll();
return CompletableFuture.completedFuture(subscriptionBoxes);
List<SubscriptionBoxDTO> dtos = subscriptionBoxes.stream()
.map(DTOMapper::convertModelToDto)
.collect(Collectors.toList());
return CompletableFuture.completedFuture(dtos);
}

@Override
@Async
public CompletableFuture<SubscriptionBox> update(SubscriptionBoxDTO subscriptionBoxDTO) {

if (subscriptionBoxDTO == null) {
throw new IllegalArgumentException("Subscription cannot be null");
}
CompletableFuture.runAsync(() -> logUpdateStatus(subscriptionBoxDTO.getId(), "UPDATE"));

return subscriptionBoxRepository.findById(subscriptionBoxDTO.getId())
.map(subscriptionBox -> {
DTOMapper.updateSubscriptionBox(subscriptionBox, subscriptionBoxDTO);
return CompletableFuture.completedFuture(subscriptionBoxRepository.update(subscriptionBox));
subscriptionBox = subscriptionBoxRepository.update(subscriptionBox);
return CompletableFuture.completedFuture(subscriptionBox);
})
.orElseThrow(() -> new IllegalArgumentException("Subscription isn't found"));

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ class SubscriptionBoxControllerTest {

@InjectMocks
private SubscriptionBoxController subscriptionBoxController;

@Mock
private JWTUtils jwtUtils;

private SubscriptionBox subscriptionBox;
private SubscriptionBoxDTO subscriptionBoxDTO;

private final String validToken = "eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiQURNSU4iLCJzdWIiOiJhZG1pbkBnbWFpbC5jb20iLCJpYXQiOjE3MTY2MjAzOTksImV4cCI6MTcxNjcwNjc5OX0.dFmE18NL6H1my8Dki1Lp4DlwGIRbTTpgj3qUFKBoBoo";

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
Expand All @@ -48,12 +51,13 @@ void setUp() {

// Convert to DTO
subscriptionBoxDTO = DTOMapper.convertModelToDto(subscriptionBox);

when(jwtUtils.isTokenValid(validToken)).thenReturn(true);
when(jwtUtils.extractRole(any(String.class))).thenReturn("admin");
}

@Test
void testCreateSubscriptionBox_HappyPath()throws IllegalAccessException {
void testCreateSubscriptionBox_HappyPath() throws IllegalAccessException {
when(subscriptionBoxService.save(any(SubscriptionBoxDTO.class)))
.thenReturn(CompletableFuture.completedFuture(subscriptionBox));

Expand All @@ -65,7 +69,7 @@ void testCreateSubscriptionBox_HappyPath()throws IllegalAccessException {
}

@Test
void testCreateSubscriptionBox_UnhappyPath()throws IllegalAccessException {
void testCreateSubscriptionBox_UnhappyPath() throws IllegalAccessException {
when(subscriptionBoxService.save(any(SubscriptionBoxDTO.class)))
.thenReturn(CompletableFuture.failedFuture(new RuntimeException("Error saving subscription box")));

Expand All @@ -78,23 +82,22 @@ void testCreateSubscriptionBox_UnhappyPath()throws IllegalAccessException {

@Test
void testFindAll_HappyPath() throws IllegalAccessException {
List<SubscriptionBox> subscriptionBoxes = Collections.singletonList(subscriptionBox);
List<SubscriptionBoxDTO> subscriptionBoxes = Collections.singletonList(subscriptionBoxDTO);

when(subscriptionBoxService.findAll())
.thenReturn(CompletableFuture.completedFuture(subscriptionBoxes));

CompletableFuture<ResponseEntity<List<SubscriptionBox>>> result = subscriptionBoxController.findAll(validToken);
CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> result = subscriptionBoxController.findAll(validToken);

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


@Test
void testUpdateSubscriptionBox_HappyPath() throws IllegalAccessException {
when(subscriptionBoxService.findById(subscriptionBoxDTO.getId()))
.thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBoxDTO)));
.thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBox)));
when(subscriptionBoxService.update(any(SubscriptionBoxDTO.class)))
.thenReturn(CompletableFuture.completedFuture(subscriptionBox));

Expand All @@ -117,7 +120,7 @@ void testUpdateSubscriptionBox_UnhappyPath() throws IllegalAccessException {
}

@Test
void testUpdateSubscriptionBox_NotFound()throws IllegalAccessException {
void testUpdateSubscriptionBox_NotFound() throws IllegalAccessException {
when(subscriptionBoxService.findById(subscriptionBoxDTO.getId()))
.thenReturn(CompletableFuture.completedFuture(Optional.empty()));

Expand All @@ -131,17 +134,16 @@ void testUpdateSubscriptionBox_NotFound()throws IllegalAccessException {
@Test
void testFindById_HappyPath() throws IllegalAccessException {
when(subscriptionBoxService.findById(subscriptionBox.getId()))
.thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBoxDTO)));
.thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBox)));

CompletableFuture<ResponseEntity<SubscriptionBoxDTO>> result = subscriptionBoxController.findById(validToken, subscriptionBox.getId());

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

@Test
void testFindById_UnhappyPath()throws IllegalAccessException {
void testFindById_UnhappyPath() throws IllegalAccessException {
String invalidId = "invalid-uuid";
CompletableFuture<ResponseEntity<SubscriptionBoxDTO>> result = subscriptionBoxController.findById(validToken, invalidId);

Expand Down Expand Up @@ -172,7 +174,6 @@ void testDeleteSubscriptionBox_UnhappyPath() throws IllegalAccessException {
assertEquals(expectedResult.join(), result.join());
}


@Test
void testFindByPriceLessThan_HappyPath() throws IllegalAccessException {
List<SubscriptionBoxDTO> expectedDTOs = Collections.singletonList(subscriptionBoxDTO);
Expand Down Expand Up @@ -243,7 +244,6 @@ void testFindByPriceEquals_UnhappyPath() throws IllegalAccessException {

assertNotNull(result);
assertTrue(result.isDone());

}

@Test
Expand Down
Loading

0 comments on commit b84088e

Please sign in to comment.