-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR]added more tests, trying to integrate sonar cloud again
- Loading branch information
Showing
16 changed files
with
527 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/config/AsyncConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.config; | ||
|
||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.task.AsyncTaskExecutor; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
public AsyncTaskExecutor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(5); | ||
executor.setMaxPoolSize(10); | ||
executor.setQueueCapacity(100); | ||
executor.setThreadNamePrefix("Async-Executor-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/config/AsyncConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.config; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.task.AsyncTaskExecutor; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
|
||
class AsyncConfigTest { | ||
|
||
@Test | ||
void testAsyncExecutor() { | ||
|
||
AsyncConfig asyncConfig = new AsyncConfig(); | ||
|
||
AsyncTaskExecutor executor = asyncConfig.getAsyncExecutor(); | ||
|
||
assertNotNull(executor); | ||
|
||
assertEquals(5, ((ThreadPoolTaskExecutor) executor).getCorePoolSize()); | ||
assertEquals(10, ((ThreadPoolTaskExecutor) executor).getMaxPoolSize()); | ||
assertEquals(100, ((ThreadPoolTaskExecutor) executor).getQueueCapacity()); | ||
assertEquals("Async-Executor-", ((ThreadPoolTaskExecutor) executor).getThreadNamePrefix()); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...est/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/config/JWTAuthFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.config; | ||
|
||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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.security.core.context.SecurityContextHolder; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils.JWTUtils; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
class JWTAuthFilterTest { | ||
|
||
@Mock | ||
private JWTUtils jwtUtils; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@Mock | ||
private HttpServletResponse response; | ||
|
||
@Mock | ||
private FilterChain filterChain; | ||
|
||
@InjectMocks | ||
private JWTAuthFilter jwtAuthFilter; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
SecurityContextHolder.clearContext(); | ||
} | ||
|
||
@Test | ||
void doFilterInternal_MissingAuthHeader() throws ServletException, IOException { | ||
when(request.getHeader("Authorization")).thenReturn(null); | ||
|
||
jwtAuthFilter.doFilterInternal(request, response, filterChain); | ||
|
||
verify(filterChain, times(1)).doFilter(request, response); | ||
assertNull(SecurityContextHolder.getContext().getAuthentication()); | ||
} | ||
|
||
@Test | ||
void doFilterInternal_BlankAuthHeader() throws ServletException, IOException { | ||
when(request.getHeader("Authorization")).thenReturn(""); | ||
|
||
jwtAuthFilter.doFilterInternal(request, response, filterChain); | ||
|
||
verify(filterChain, times(1)).doFilter(request, response); | ||
assertNull(SecurityContextHolder.getContext().getAuthentication()); | ||
} | ||
|
||
@Test | ||
void doFilterInternal_InvalidToken() throws ServletException, IOException { | ||
String invalidToken = "Bearer invalidToken"; | ||
when(request.getHeader("Authorization")).thenReturn(invalidToken); | ||
when(jwtUtils.isTokenValid(anyString())).thenReturn(false); | ||
|
||
jwtAuthFilter.doFilterInternal(request, response, filterChain); | ||
|
||
verify(filterChain, times(1)).doFilter(request, response); | ||
assertNull(SecurityContextHolder.getContext().getAuthentication()); | ||
} | ||
|
||
@Test | ||
void doFilterInternal_ValidToken() throws ServletException, IOException { | ||
String validToken = "Bearer validToken"; | ||
when(request.getHeader("Authorization")).thenReturn(validToken); | ||
when(jwtUtils.isTokenValid("validToken")).thenReturn(true); | ||
when(jwtUtils.extractRole("validToken")).thenReturn("USER"); | ||
|
||
jwtAuthFilter.doFilterInternal(request, response, filterChain); | ||
|
||
verify(filterChain, times(1)).doFilter(request, response); | ||
assertEquals("USER", SecurityContextHolder.getContext().getAuthentication().getAuthorities().iterator().next().getAuthority()); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/DTOMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto; | ||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory.SubscriptionBoxFactory; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DTOMapperTest { | ||
|
||
@Test | ||
void testConvertModelToDto() { | ||
Item item1 = new Item("1", "Item 1", 10); | ||
Item item2 = new Item("2", "Item 2", 20); | ||
List<Item> items = List.of(item1, item2); | ||
|
||
SubscriptionBox model = new SubscriptionBox( "Basic", "Monthly", 100, items, "Description"); | ||
SubscriptionBoxDTO dto = DTOMapper.convertModelToDto(model); | ||
|
||
assertThat(dto.getId()).isEqualTo(model.getId()); | ||
assertThat(dto.getName()).isEqualTo("Basic"); | ||
assertThat(dto.getType()).isEqualTo("MONTHLY"); | ||
assertThat(dto.getPrice()).isEqualTo(100); | ||
assertThat(dto.getItems()).hasSize(2); | ||
assertThat(dto.getDescription()).isEqualTo("Description"); | ||
} | ||
|
||
@Test | ||
void testConvertDTOtoModel() { | ||
ItemDTO item1 = new ItemDTO("1", "Item 1", 10); | ||
ItemDTO item2 = new ItemDTO("2", "Item 2", 20); | ||
List<ItemDTO> items = List.of(item1, item2); | ||
|
||
SubscriptionBoxDTO dto = new SubscriptionBoxDTO("1","Basic", "Monthly", 100, items, "Description"); | ||
SubscriptionBox model = DTOMapper.convertDTOtoModel(dto); | ||
assertThat(model.getName()).isEqualTo("Basic"); | ||
assertThat(model.getType()).isEqualTo("MONTHLY"); | ||
assertThat(model.getPrice()).isEqualTo(100); | ||
assertThat(model.getItems()).hasSize(2); | ||
assertThat(model.getDescription()).isEqualTo("Description"); | ||
} | ||
|
||
@Test | ||
void testUpdateSubscriptionBox() { | ||
SubscriptionBox model = new SubscriptionBox("1", "Monthly", 100, null, "Old Description"); | ||
ItemDTO item1 = new ItemDTO("1", "Item 1", 10); | ||
List<ItemDTO> items = List.of(item1); | ||
|
||
SubscriptionBoxDTO dto = new SubscriptionBoxDTO("1", "Basic", "Monthly", 150, items, "New Description"); | ||
SubscriptionBox updatedModel = DTOMapper.updateSubscriptionBox(model, dto); | ||
|
||
assertThat(updatedModel.getPrice()).isEqualTo(150); | ||
assertThat(updatedModel.getDescription()).isEqualTo("New Description"); | ||
assertThat(updatedModel.getItems()).hasSize(1); | ||
} | ||
|
||
@Test | ||
void testConvertItemToDto() { | ||
Item item = new Item("1", "Item 1", 10); | ||
ItemDTO dto = DTOMapper.convertItemToDto(item); | ||
|
||
assertThat(dto.getId()).isEqualTo("1"); | ||
assertThat(dto.getName()).isEqualTo("Item 1"); | ||
assertThat(dto.getQuantity()).isEqualTo(10); | ||
} | ||
|
||
@Test | ||
void testConvertDtoToItem() { | ||
ItemDTO dto = new ItemDTO("1", "Item 1", 10); | ||
Item item = DTOMapper.convertDtoToItem(dto); | ||
|
||
assertThat(item.getId()).isEqualTo("1"); | ||
assertThat(item.getName()).isEqualTo("Item 1"); | ||
assertThat(item.getQuantity()).isEqualTo(10); | ||
} | ||
|
||
@Test | ||
void testUpdateItem() { | ||
Item item = new Item("1", "Old Item", 5); | ||
ItemDTO dto = new ItemDTO("1", "New Item", 10); | ||
Item updatedItem = DTOMapper.updateItem(item, dto); | ||
|
||
assertThat(updatedItem.getName()).isEqualTo("New Item"); | ||
assertThat(updatedItem.getQuantity()).isEqualTo(10); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...t/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/SubscriptionBoxDTOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class SubscriptionBoxDTOTest { | ||
|
||
@Test | ||
void testNoArgsConstructor() { | ||
SubscriptionBoxDTO dto = new SubscriptionBoxDTO(); | ||
assertThat(dto).isNotNull(); | ||
} | ||
|
||
@Test | ||
void testAllArgsConstructor() { | ||
ItemDTO item1 = new ItemDTO("1", "Item 1", 10); | ||
ItemDTO item2 = new ItemDTO("2", "Item 2", 20); | ||
List<ItemDTO> items = List.of(item1, item2); | ||
|
||
SubscriptionBoxDTO dto = new SubscriptionBoxDTO("1", "Basic", "Monthly", 100, items, "Description"); | ||
|
||
assertThat(dto.getId()).isEqualTo("1"); | ||
assertThat(dto.getName()).isEqualTo("Basic"); | ||
assertThat(dto.getType()).isEqualTo("Monthly"); | ||
assertThat(dto.getPrice()).isEqualTo(100); | ||
assertThat(dto.getItems()).isEqualTo(items); | ||
assertThat(dto.getDescription()).isEqualTo("Description"); | ||
} | ||
|
||
@Test | ||
void testSettersAndGetters() { | ||
SubscriptionBoxDTO dto = new SubscriptionBoxDTO(); | ||
|
||
dto.setId("1"); | ||
dto.setName("Basic"); | ||
dto.setType("Monthly"); | ||
dto.setPrice(100); | ||
ItemDTO item = new ItemDTO("1", "Item 1", 10); | ||
dto.setItems(List.of(item)); | ||
dto.setDescription("Description"); | ||
|
||
assertThat(dto.getId()).isEqualTo("1"); | ||
assertThat(dto.getName()).isEqualTo("Basic"); | ||
assertThat(dto.getType()).isEqualTo("Monthly"); | ||
assertThat(dto.getPrice()).isEqualTo(100); | ||
assertThat(dto.getItems()).containsExactly(item); | ||
assertThat(dto.getDescription()).isEqualTo("Description"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.