Skip to content

Commit 47eef17

Browse files
committed
Agrega todos los tests de brand
1 parent a7c3805 commit 47eef17

File tree

5 files changed

+357
-0
lines changed

5 files changed

+357
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.outfitlab.project.domain.useCases.brand;
2+
3+
import com.outfitlab.project.domain.exceptions.BrandsNotFoundException;
4+
import com.outfitlab.project.domain.interfaces.repositories.BrandRepository;
5+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
6+
import com.outfitlab.project.domain.model.BrandModel;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.mockito.Mockito.*;
12+
13+
public class ActivateBrandTest {
14+
15+
private BrandRepository brandRepository = mock(BrandRepository.class);
16+
private UserRepository userRepository = mock(UserRepository.class);
17+
private ActivateBrand activateBrand;
18+
19+
private final String BRAND_CODE = "adidas";
20+
private final String USER_EMAIL = "[email protected]";
21+
private final String SUCCESS_MESSAGE = "Marca activada con éxito.";
22+
23+
@BeforeEach
24+
void setUp() {
25+
activateBrand = new ActivateBrand(brandRepository, userRepository);
26+
}
27+
28+
29+
@Test
30+
public void shouldActivateUserAndReturnSuccessMessageWhenBrandExists() throws BrandsNotFoundException {
31+
givenBrandExistsAndUserEmailIsAvailable(BRAND_CODE, USER_EMAIL);
32+
33+
String result = whenExecuteActivateBrand(BRAND_CODE);
34+
35+
thenResultIsSuccessMessage(result);
36+
thenUserWasActivated(USER_EMAIL);
37+
}
38+
39+
@Test
40+
public void shouldThrowBrandsNotFoundExceptionWhenBrandDoesNotExist() {
41+
givenBrandDoesNotExist(BRAND_CODE);
42+
43+
assertThrows(BrandsNotFoundException.class, () -> activateBrand.execute(BRAND_CODE));
44+
45+
thenActivationWasNeverCalled();
46+
}
47+
48+
49+
//privadosss
50+
private void givenBrandExistsAndUserEmailIsAvailable(String brandCode, String userEmail) {
51+
when(brandRepository.findByBrandCode(brandCode)).thenReturn(new BrandModel());
52+
when(userRepository.getEmailUserRelatedToBrandByBrandCode(brandCode)).thenReturn(userEmail);
53+
}
54+
55+
private void givenBrandDoesNotExist(String brandCode) {
56+
when(brandRepository.findByBrandCode(brandCode)).thenReturn(null);
57+
}
58+
59+
private String whenExecuteActivateBrand(String brandCode) {
60+
return activateBrand.execute(brandCode);
61+
}
62+
63+
private void thenResultIsSuccessMessage(String result) {
64+
assertEquals(SUCCESS_MESSAGE, result, "El mensaje de retorno debe ser de activación exitosa.");
65+
}
66+
67+
private void thenUserWasActivated(String userEmail) {
68+
verify(userRepository, times(1)).getEmailUserRelatedToBrandByBrandCode(BRAND_CODE);
69+
verify(userRepository, times(1)).activateUser(userEmail);
70+
}
71+
72+
private void thenActivationWasNeverCalled() {
73+
verify(userRepository, never()).activateUser(anyString());
74+
}
75+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.outfitlab.project.domain.useCases.brand;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.BrandRepository;
4+
import com.outfitlab.project.domain.model.BrandModel;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.ArgumentCaptor;
8+
import static org.junit.jupiter.api.Assertions.*;
9+
import static org.mockito.Mockito.*;
10+
11+
public class CreateBrandTest {
12+
13+
private BrandRepository brandRepository = mock(BrandRepository.class);
14+
private CreateBrand createBrand;
15+
16+
private final String BRAND_NAME = "Zara Shop";
17+
private final String LOGO_URL = "http://logo.com/zara-shop.png";
18+
private final String URL_SITE = "http://zarashop.com";
19+
private final String EXPECTED_BRAND_CODE = "zara_shop";
20+
private final String EXPECTED_RESPONSE = "brand-id-123";
21+
22+
@BeforeEach
23+
void setUp() {
24+
createBrand = new CreateBrand(brandRepository);
25+
}
26+
27+
28+
@Test
29+
public void shouldCreateBrandModelWithFormattedCodeAndCallRepository() {
30+
givenRepositoryReturnsExpectedResponse(EXPECTED_RESPONSE);
31+
32+
String result = whenExecuteCreateBrand(BRAND_NAME, LOGO_URL, URL_SITE);
33+
34+
thenRepositoryWasCalledWithCorrectBrandModel(BRAND_NAME, LOGO_URL, URL_SITE, EXPECTED_BRAND_CODE);
35+
thenResultIsExpected(result, EXPECTED_RESPONSE);
36+
}
37+
38+
39+
//privadoss
40+
private void givenRepositoryReturnsExpectedResponse(String response) {
41+
when(brandRepository.createBrand(any(BrandModel.class))).thenReturn(response);
42+
}
43+
44+
private String whenExecuteCreateBrand(String brandName, String logoUrl, String urlSite) {
45+
return createBrand.execute(brandName, logoUrl, urlSite);
46+
}
47+
48+
private void thenResultIsExpected(String actualResult, String expectedResponse) {
49+
assertEquals(expectedResponse, actualResult, "El resultado debe ser la respuesta del repositorio.");
50+
}
51+
52+
private void thenRepositoryWasCalledWithCorrectBrandModel(String expectedName, String expectedLogoUrl, String expectedUrlSite, String expectedCode) {
53+
ArgumentCaptor<BrandModel> captor = ArgumentCaptor.forClass(BrandModel.class);
54+
verify(brandRepository, times(1)).createBrand(captor.capture());
55+
56+
BrandModel capturedModel = captor.getValue();
57+
58+
assertEquals(expectedName, capturedModel.getNombre(), "El nombre de la marca debe coincidir.");
59+
assertEquals(expectedLogoUrl, capturedModel.getLogoUrl(), "La URL del logo debe coincidir.");
60+
assertEquals(expectedUrlSite, capturedModel.getUrlSite(), "La URL del sitio debe coincidir.");
61+
assertEquals(expectedCode, capturedModel.getCodigoMarca(), "El código de marca debe estar formateado.");
62+
}
63+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.outfitlab.project.domain.useCases.brand;
2+
3+
import com.outfitlab.project.domain.exceptions.BrandsNotFoundException;
4+
import com.outfitlab.project.domain.interfaces.repositories.BrandRepository;
5+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
6+
import com.outfitlab.project.domain.model.BrandModel;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
import static org.mockito.Mockito.*;
11+
12+
public class DesactivateBrandTest {
13+
14+
private BrandRepository brandRepository = mock(BrandRepository.class);
15+
private UserRepository userRepository = mock(UserRepository.class);
16+
private DesactivateBrand desactivateBrand;
17+
18+
private final String BRAND_CODE = "nike";
19+
private final String USER_EMAIL = "[email protected]";
20+
private final String SUCCESS_MESSAGE = "Marca desactivada con éxito.";
21+
22+
@BeforeEach
23+
void setUp() {
24+
desactivateBrand = new DesactivateBrand(brandRepository, userRepository);
25+
}
26+
27+
28+
@Test
29+
public void shouldDesactivateUserAndReturnSuccessMessageWhenBrandExists() throws BrandsNotFoundException {
30+
givenBrandExistsAndUserEmailIsAvailable(BRAND_CODE, USER_EMAIL);
31+
32+
String result = whenExecuteDesactivateBrand(BRAND_CODE);
33+
34+
thenResultIsSuccessMessage(result);
35+
thenUserWasDesactivated(USER_EMAIL);
36+
}
37+
38+
@Test
39+
public void shouldThrowBrandsNotFoundExceptionWhenBrandDoesNotExist() {
40+
givenBrandDoesNotExist(BRAND_CODE);
41+
42+
assertThrows(BrandsNotFoundException.class, () -> desactivateBrand.execute(BRAND_CODE));
43+
44+
thenDesactivationWasNeverCalled();
45+
}
46+
47+
48+
//privadoss
49+
private void givenBrandExistsAndUserEmailIsAvailable(String brandCode, String userEmail) {
50+
when(brandRepository.findByBrandCode(brandCode)).thenReturn(new BrandModel());
51+
when(userRepository.getEmailUserRelatedToBrandByBrandCode(brandCode)).thenReturn(userEmail);
52+
}
53+
54+
private void givenBrandDoesNotExist(String brandCode) {
55+
when(brandRepository.findByBrandCode(brandCode)).thenReturn(null);
56+
}
57+
58+
private String whenExecuteDesactivateBrand(String brandCode) {
59+
return desactivateBrand.execute(brandCode);
60+
}
61+
62+
private void thenResultIsSuccessMessage(String result) {
63+
assertEquals(SUCCESS_MESSAGE, result, "El mensaje de retorno debe ser de desactivación exitosa.");
64+
}
65+
66+
private void thenUserWasDesactivated(String userEmail) {
67+
verify(userRepository, times(1)).getEmailUserRelatedToBrandByBrandCode(BRAND_CODE);
68+
verify(userRepository, times(1)).desactivateUser(userEmail);
69+
}
70+
71+
private void thenDesactivationWasNeverCalled() {
72+
verify(userRepository, never()).desactivateUser(anyString());
73+
}
74+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.outfitlab.project.domain.useCases.brand;
2+
3+
import com.outfitlab.project.domain.exceptions.PageLessThanZeroException;
4+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
5+
import com.outfitlab.project.domain.model.dto.UserWithBrandsDTO;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.data.domain.Page;
9+
import org.springframework.data.domain.PageImpl;
10+
import java.util.Collections;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.mockito.Mockito.*;
13+
14+
public class GetAllBrandsWithRelatedUsersTest {
15+
16+
private UserRepository userRepository = mock(UserRepository.class);
17+
private GetAllBrandsWithRelatedUsers getAllBrandsWithRelatedUsers;
18+
19+
private final int VALID_PAGE = 5;
20+
private final int INVALID_PAGE = -1;
21+
22+
@BeforeEach
23+
void setUp() {
24+
getAllBrandsWithRelatedUsers = new GetAllBrandsWithRelatedUsers(userRepository);
25+
}
26+
27+
28+
@Test
29+
public void shouldReturnPageOfUsersWithBrandsWhenPageIsValid() {
30+
Page<UserWithBrandsDTO> expectedPage = givenRepositoryReturnsPage(VALID_PAGE);
31+
32+
Page<UserWithBrandsDTO> result = whenExecuteGetAllBrands(VALID_PAGE);
33+
34+
thenReturnedPageMatchesExpected(result, expectedPage);
35+
thenRepositoryWasCalledOnce(VALID_PAGE);
36+
}
37+
38+
@Test
39+
public void shouldThrowPageLessThanZeroExceptionWhenPageIsNegative() {
40+
assertThrows(PageLessThanZeroException.class,
41+
() -> whenExecuteGetAllBrands(INVALID_PAGE),
42+
"Se esperaba PageLessThanZeroException para una página negativa.");
43+
44+
thenRepositoryWasNeverCalled();
45+
}
46+
47+
48+
//privadoss
49+
private Page<UserWithBrandsDTO> givenRepositoryReturnsPage(int page) {
50+
Page<UserWithBrandsDTO> mockPage = new PageImpl<>(Collections.singletonList(new UserWithBrandsDTO()));
51+
52+
when(userRepository.getAllBrandsWithUserRelated(page)).thenReturn(mockPage);
53+
return mockPage;
54+
}
55+
56+
private Page<UserWithBrandsDTO> whenExecuteGetAllBrands(int page) {
57+
return getAllBrandsWithRelatedUsers.execute(page);
58+
}
59+
60+
private void thenReturnedPageMatchesExpected(Page<UserWithBrandsDTO> actual, Page<UserWithBrandsDTO> expected) {
61+
assertNotNull(actual, "La página resultante no debe ser nula.");
62+
assertEquals(expected, actual, "El resultado debe coincidir con la página simulada.");
63+
}
64+
65+
private void thenRepositoryWasCalledOnce(int page) {
66+
verify(userRepository, times(1)).getAllBrandsWithUserRelated(page);
67+
}
68+
69+
private void thenRepositoryWasNeverCalled() {
70+
verify(userRepository, never()).getAllBrandsWithUserRelated(anyInt());
71+
}
72+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.outfitlab.project.domain.useCases.brand;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
4+
import com.outfitlab.project.domain.model.dto.UserWithBrandsDTO;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
import static org.mockito.Mockito.*;
11+
12+
public class GetNotificationsNewBrandsTest {
13+
14+
private UserRepository userRepository = mock(UserRepository.class);
15+
private GetNotificationsNewBrands getNotificationsNewBrands;
16+
17+
private final int BRAND_COUNT = 3;
18+
19+
@BeforeEach
20+
void setUp() {
21+
getNotificationsNewBrands = new GetNotificationsNewBrands(userRepository);
22+
}
23+
24+
25+
@Test
26+
public void shouldReturnListOfNotApprovedBrandsWhenBrandsExist() {
27+
List<UserWithBrandsDTO> expectedList = givenRepositoryReturnsBrands(BRAND_COUNT);
28+
29+
List<UserWithBrandsDTO> result = whenExecuteGetNotifications();
30+
31+
thenResultListMatchesExpected(result, expectedList, BRAND_COUNT);
32+
thenRepositoryWasCalledOnce();
33+
}
34+
35+
@Test
36+
public void shouldReturnEmptyListWhenNoNotApprovedBrandsExist() {
37+
List<UserWithBrandsDTO> expectedEmptyList = givenRepositoryReturnsEmptyList();
38+
39+
List<UserWithBrandsDTO> result = whenExecuteGetNotifications();
40+
41+
thenResultListMatchesExpected(result, expectedEmptyList, 0);
42+
thenRepositoryWasCalledOnce();
43+
}
44+
45+
46+
//privadoss
47+
private List<UserWithBrandsDTO> givenRepositoryReturnsBrands(int count) {
48+
List<UserWithBrandsDTO> mockList = Collections.nCopies(count, new UserWithBrandsDTO());
49+
50+
when(userRepository.getNotApprovedBrands()).thenReturn(mockList);
51+
return mockList;
52+
}
53+
54+
private List<UserWithBrandsDTO> givenRepositoryReturnsEmptyList() {
55+
List<UserWithBrandsDTO> emptyList = Collections.emptyList();
56+
when(userRepository.getNotApprovedBrands()).thenReturn(emptyList);
57+
return emptyList;
58+
}
59+
60+
private List<UserWithBrandsDTO> whenExecuteGetNotifications() {
61+
return getNotificationsNewBrands.execute();
62+
}
63+
64+
private void thenResultListMatchesExpected(List<UserWithBrandsDTO> actual, List<UserWithBrandsDTO> expected, int expectedCount) {
65+
assertNotNull(actual, "La lista resultante no debe ser nula.");
66+
assertEquals(expectedCount, actual.size(), "El tamaño de la lista debe coincidir.");
67+
assertEquals(expected, actual, "El contenido de la lista debe coincidir con la lista simulada.");
68+
}
69+
70+
private void thenRepositoryWasCalledOnce() {
71+
verify(userRepository, times(1)).getNotApprovedBrands();
72+
}
73+
}

0 commit comments

Comments
 (0)