Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkhanh-axonivy committed Jul 22, 2024
1 parent aa91583 commit 2b8a409
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void validateUserOrganization(String accessToken, String organization) th
+ "-User must be a member of the Axon Ivy Market Organization");
}

private List<Map<String, Object>> getUserOrganizations(String accessToken) throws UnauthorizedException {
public List<Map<String, Object>> getUserOrganizations(String accessToken) throws UnauthorizedException {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
HttpEntity<String> entity = new HttpEntity<>(headers);
Expand All @@ -160,10 +160,10 @@ private List<Map<String, Object>> getUserOrganizations(String accessToken) throw
HttpMethod.GET, entity, new ParameterizedTypeReference<>() {
});
return response.getBody();
}
catch (HttpClientErrorException exception) {
} catch (HttpClientErrorException exception) {
throw new UnauthorizedException(ErrorCode.GITHUB_USER_UNAUTHORIZED.getCode(),
ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText() + "-" + GitHubUtils.extractMessageFromExceptionMessage(exception.getMessage()));
ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText() + "-" + GitHubUtils.extractMessageFromExceptionMessage(
exception.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.axonivy.market.controller;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.axonivy.market.assembler.ProductModelAssembler;
import com.axonivy.market.entity.Product;
import com.axonivy.market.enums.ErrorCode;
import com.axonivy.market.enums.Language;
import com.axonivy.market.enums.SortOption;
import com.axonivy.market.enums.TypeOption;
import com.axonivy.market.exceptions.model.UnauthorizedException;
import com.axonivy.market.github.service.GitHubService;
import com.axonivy.market.service.ProductService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -25,21 +25,25 @@
import org.springframework.hateoas.PagedModel.PageMetadata;
import org.springframework.http.HttpStatus;

import com.axonivy.market.assembler.ProductModelAssembler;
import com.axonivy.market.entity.Product;
import com.axonivy.market.enums.ErrorCode;
import com.axonivy.market.enums.Language;
import com.axonivy.market.enums.SortOption;
import com.axonivy.market.enums.TypeOption;
import com.axonivy.market.model.ProductRating;
import com.axonivy.market.service.ProductService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ProductControllerTest {
private static final String PRODUCT_NAME_SAMPLE = "Amazon Comprehend";
private static final String PRODUCT_NAME_DE_SAMPLE = "Amazon Comprehend DE";
private static final String PRODUCT_DESC_SAMPLE = "Amazon Comprehend is a AI service that uses machine learning to uncover information in unstructured data.";
private static final String PRODUCT_DESC_DE_SAMPLE = "Amazon Comprehend is a AI service that uses machine learning to uncover information in unstructured data. DE";
private static final String AUTHORIZATION_HEADER = "Bearer valid_token";
private static final String INVALID_AUTHORIZATION_HEADER = "Bearer invalid_token";

@Mock
private ProductService service;
Expand All @@ -53,6 +57,9 @@ class ProductControllerTest {
@Mock
private PagedModel<?> pagedProductModel;

@Mock
private GitHubService gitHubService;

@InjectMocks
private ProductController productController;

Expand Down Expand Up @@ -95,11 +102,41 @@ void testFindProducts() {
}

@Test
void testSyncProducts() {
var response = productController.syncProducts();
void testSyncProductsSuccess() {
when(service.syncLatestDataFromMarketRepo()).thenReturn(true);

var response = productController.syncProducts(AUTHORIZATION_HEADER, false);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.hasBody());
assertEquals(ErrorCode.SUCCESSFUL.getCode(), response.getBody().getHelpCode());
assertEquals("Data is already up to date, nothing to sync", response.getBody().getMessageDetails());
}

@Test
void testSyncProductsWithResetSuccess() {
// Mocking the dependencies for success case with resetSync = true
when(service.syncLatestDataFromMarketRepo()).thenReturn(false);

var response = productController.syncProducts(AUTHORIZATION_HEADER, false);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.hasBody());
assertEquals(ErrorCode.SUCCESSFUL.getCode(), response.getBody().getHelpCode());
assertTrue(response.getBody().getMessageDetails().contains("Finished sync data"));
}

@Test
void testSyncProductsInvalidToken() {
doThrow(new UnauthorizedException(ErrorCode.GITHUB_USER_UNAUTHORIZED.getCode(),
ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText())).when(gitHubService)
.validateUserOrganization(any(String.class), any(String.class));

UnauthorizedException exception = assertThrows(UnauthorizedException.class, () -> {
productController.syncProducts(INVALID_AUTHORIZATION_HEADER, false);
});

assertEquals(ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText(), exception.getMessage());
}

private Product createProductMock() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.axonivy.market.service;

import com.axonivy.market.github.service.impl.GitHubServiceImpl;
import com.axonivy.market.repository.UserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -23,13 +22,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


@ExtendWith(MockitoExtension.class)
class GitHubServiceImplTest {
private static final String DUMMY_API_URL = "https://api.github.com";

@Mock
GitHub gitHub;

@Mock
GHRepository ghRepository;

Expand All @@ -39,16 +36,12 @@ class GitHubServiceImplTest {
@Mock
private RestTemplate restTemplate;

@Mock
private UserRepository userRepository;

@InjectMocks
private GitHubServiceImpl gitHubService;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
// Use lenient stubbing
lenient().when(restTemplateBuilder.build()).thenReturn(restTemplate);
}

Expand Down

0 comments on commit 2b8a409

Please sign in to comment.