1616
1717class BrandControllerTest {
1818
19- private GetAllBrands getAllBrands = mock (GetAllBrands .class );;
19+ private GetAllBrands getAllBrands = mock (GetAllBrands .class );
2020 private GetBrandAndGarmentsByBrandCode getBrandAndGarmentsByBrandCode = mock (GetBrandAndGarmentsByBrandCode .class );
2121 private ActivateBrand activateBrand = mock (ActivateBrand .class );
2222 private DesactivateBrand desactivateBrand = mock (DesactivateBrand .class );
2323 private GetAllBrandsWithRelatedUsers getAllBrandsWithRelatedUsers = mock (GetAllBrandsWithRelatedUsers .class );
2424 private GetNotificationsNewBrands getNotificationsNewBrands = mock (GetNotificationsNewBrands .class );
25- private BrandController brandController = new BrandController (getAllBrands , getBrandAndGarmentsByBrandCode , activateBrand , desactivateBrand , getAllBrandsWithRelatedUsers , getNotificationsNewBrands );
25+
26+ private BrandController controller = new BrandController (
27+ getAllBrands ,
28+ getBrandAndGarmentsByBrandCode ,
29+ activateBrand ,
30+ desactivateBrand ,
31+ getAllBrandsWithRelatedUsers ,
32+ getNotificationsNewBrands );
33+
34+ @ Test
35+ void givenValidPageWhenGetMarcasThenReturnOk () throws Exception {
36+ givenBrandsExist ();
37+ ResponseEntity <?> response = whenCallGetMarcas (0 );
38+ thenResponseOkWithBrands (response );
39+ thenGetAllBrandsCalledOnce ();
40+ }
41+
42+ @ Test
43+ void givenNoBrandsWhenGetMarcasThenReturn404 () throws Exception {
44+ givenBrandsNotFound ();
45+ ResponseEntity <?> response = whenCallGetMarcas (0 );
46+ thenResponseNotFound (response , "No se encontraron marcas" );
47+ }
48+
49+ @ Test
50+ void givenNegativePageWhenGetMarcasThenReturn404 () throws Exception {
51+ givenNegativePageError ();
52+ ResponseEntity <?> response = whenCallGetMarcas (-1 );
53+ thenResponseNotFound (response , "Página menor que cero" );
54+ }
55+
56+ @ Test
57+ void givenNonExistingBrandWhenGetBrandGarmentsThenReturn404 () throws Exception {
58+ givenBrandNotFoundForGetBrandGarments ();
59+ ResponseEntity <?> response = whenCallGetBrandAndGarments ("puma" , 0 );
60+ thenResponseNotFound (response , "Marca no encontrada" );
61+ }
2662
2763 @ Test
28- public void givenValidPageWhenGetMarcasThenReturnsOkWithContent () throws Exception , BrandsNotFoundException {
29- BrandDTO brand1 = new BrandDTO ("nike" , "Nike" , "https://logos.com/logaso.png" );
30- BrandDTO brand2 = new BrandDTO ("adidas" , "Adidas" , "https://logos.com/logaso.png" );
64+ void givenNegativePageWhenGetBrandGarmentsThenReturn404 () throws Exception {
65+ givenNegativePageErrorForBrandGarments ();
66+ ResponseEntity <?> response = whenCallGetBrandAndGarments ("nike" , -1 );
67+ thenResponseNotFound (response , "Página menor que cero" );
68+ }
69+
70+ // privadosss ----
71+ private void givenBrandsExist () throws Exception {
72+ BrandDTO brand1 = new BrandDTO ("nike" , "Nike" , "https://logos.com/logaso.png" );
73+ BrandDTO brand2 = new BrandDTO ("adidas" , "Adidas" , "https://logos.com/logaso.png" );
3174
3275 Page <BrandDTO > pageResult = new PageImpl <>(
3376 List .of (brand1 , brand2 ),
@@ -36,57 +79,51 @@ public void givenValidPageWhenGetMarcasThenReturnsOkWithContent() throws Excepti
3679 );
3780
3881 when (getAllBrands .execute (0 )).thenReturn (pageResult );
39- ResponseEntity <?> response = brandController .getMarcas (0 );
40-
41-
42- assertEquals (200 , response .getStatusCode ().value ());
43- Map <?, ?> body = (Map <?, ?>) response .getBody ();
44- assertNotNull (body );
45- assertEquals (2L , body .get ("totalElements" ));
46- assertEquals (0 , body .get ("page" ));
47- assertNotNull (body .get ("content" ));
48-
49- verify (getAllBrands , times (1 )).execute (0 );
5082 }
5183
52- @ Test
53- public void givenNoBrandsFoundWhenGetMarcasThenReturns404 () throws Exception , BrandsNotFoundException {
54- when (getAllBrands .execute (0 )).thenThrow (new BrandsNotFoundException ("No se encontraron marcas" ));
55-
56- ResponseEntity <?> response = brandController .getMarcas (0 );
57-
58- assertEquals (404 , response .getStatusCode ().value ());
59- assertEquals ("No se encontraron marcas" , response .getBody ());
84+ private void givenBrandsNotFound () throws Exception {
85+ when (getAllBrands .execute (0 ))
86+ .thenThrow (new BrandsNotFoundException ("No se encontraron marcas" ));
6087 }
6188
62- @ Test
63- public void givenNegativePageWhenGetMarcasThenReturns404 () throws Exception , BrandsNotFoundException {
64- when (getAllBrands .execute (-1 )).thenThrow (new PageLessThanZeroException ("Página menor que cero" ));
65-
66- ResponseEntity <?> response = brandController .getMarcas (-1 );
89+ private void givenNegativePageError () throws Exception {
90+ when (getAllBrands .execute (-1 ))
91+ .thenThrow (new PageLessThanZeroException ("Página menor que cero" ));
92+ }
6793
68- assertEquals (404 , response .getStatusCode ().value ());
69- assertEquals ("Página menor que cero" , response .getBody ());
94+ private void givenBrandNotFoundForGetBrandGarments () throws Exception {
95+ when (getBrandAndGarmentsByBrandCode .execute ("puma" , 0 ))
96+ .thenThrow (new BrandsNotFoundException ("Marca no encontrada" ));
7097 }
7198
72- @ Test
73- public void givenNoExistingBrandWhenGetBrandAndGarmentsByBrandCodeThenReturns404 () throws Exception , BrandsNotFoundException {
74- when (getBrandAndGarmentsByBrandCode .execute ("puma" , 0 )).thenThrow (new BrandsNotFoundException ("Marca no encontrada" ));
99+ private void givenNegativePageErrorForBrandGarments () throws Exception {
100+ when (getBrandAndGarmentsByBrandCode .execute ("nike" , -1 ))
101+ .thenThrow (new PageLessThanZeroException ("Página menor que cero" ));
102+ }
75103
76- ResponseEntity <?> response = brandController .getBrandAndGarmentsByBrandCode ("puma" , 0 );
104+ private ResponseEntity <?> whenCallGetMarcas (int page ) {
105+ return controller .getMarcas (page );
106+ }
77107
78- assertEquals ( 404 , response . getStatusCode (). value ());
79- assertEquals ( "Marca no encontrada" , response . getBody () );
108+ private ResponseEntity <?> whenCallGetBrandAndGarments ( String brand , int page ) {
109+ return controller . getBrandAndGarmentsByBrandCode ( brand , page );
80110 }
81111
82- @ Test
83- public void givenNegativePageWhenGetBrandAndGarmentsByBrandCodeThenReturns404 () throws Exception , BrandsNotFoundException {
84- when (getBrandAndGarmentsByBrandCode .execute ("nike" , -1 )).thenThrow (new PageLessThanZeroException ("Página menor que cero" ));
112+ private void thenResponseOkWithBrands (ResponseEntity <?> response ) {
113+ assertEquals (200 , response .getStatusCode ().value ());
114+ Map <?, ?> body = (Map <?, ?>) response .getBody ();
115+ assertNotNull (body );
116+ assertEquals (2L , body .get ("totalElements" ));
117+ assertEquals (0 , body .get ("page" ));
118+ assertNotNull (body .get ("content" ));
119+ }
85120
86- ResponseEntity <?> response = brandController .getBrandAndGarmentsByBrandCode ("nike" , -1 );
121+ private void thenGetAllBrandsCalledOnce () {
122+ verify (getAllBrands , times (1 )).execute (0 );
123+ }
87124
125+ private void thenResponseNotFound (ResponseEntity <?> response , String expectedMessage ) {
88126 assertEquals (404 , response .getStatusCode ().value ());
89- assertEquals ("Página menor que cero" , response .getBody ());
127+ assertEquals (expectedMessage , response .getBody ());
90128 }
91129}
92-
0 commit comments