-
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.
- Loading branch information
1 parent
07eb4d3
commit 7a8c2ab
Showing
8 changed files
with
151 additions
and
22 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
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/dev/waltercrdz/api/ecommerce/orders/infrastructure/utils/ProductMother.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 dev.waltercrdz.api.ecommerce.orders.infrastructure.utils; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.UUID; | ||
|
||
import dev.waltercrdz.api.ecommerce.products.infrastructure.in.dto.ProductResponse; | ||
|
||
public class ProductMother { | ||
|
||
public static final String INVALID_PRODUCT_ID = "invalid_product_id"; | ||
public static final UUID NON_EXISTENT_PRODUCT_ID = UUID.fromString("f7b3b3b4-3b1b-4b3b-8b3b-3b1b3b1b3b1f"); | ||
public static final UUID PRODUCT_ID_1 = UUID.fromString("f7b3b3b4-3b1b-4b3b-8b3b-3b1b3b1b3b1b"); | ||
|
||
// ('f7b3b3b4-3b1b-4b3b-8b3b-3b1b3b1b3b1b', 'Product 1', 'Description 1', 10.00, 10), | ||
// ('f7b3b3b4-3b1b-4b3b-8b3b-3b1b3b1b3b1c', 'Product 2', 'Description 2', 20.00, 0), | ||
// ('f7b3b3b4-3b1b-4b3b-8b3b-3b1b3b1b3b1d', 'Product 3', 'Description 3', 30.00, 5) | ||
|
||
public static ProductResponse createProduct1() { | ||
return new ProductResponse( | ||
PRODUCT_ID_1, | ||
"Product 1", | ||
"Description 1", | ||
BigDecimal.valueOf(10.00), | ||
10 | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...waltercrdz/api/ecommerce/products/infrastructure/in/controller/ProductControllerTest.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,66 @@ | ||
package dev.waltercrdz.api.ecommerce.products.infrastructure.in.controller; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.springframework.http.MediaType; | ||
|
||
import dev.waltercrdz.api.ecommerce.orders.infrastructure.configuration.TestConfig; | ||
import dev.waltercrdz.api.ecommerce.orders.infrastructure.utils.ProductMother; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@AutoConfigureMockMvc | ||
@Import(TestConfig.class) | ||
public class ProductControllerTest { | ||
|
||
private static final String URI_PRODUCT = "/products"; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
void shouldReturnAllProducts() throws Exception { | ||
mockMvc.perform(get(URI_PRODUCT) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$").isArray()) | ||
.andExpect(jsonPath("$.length()").value(3)); | ||
} | ||
|
||
@Test | ||
void shouldReturnProduct_whenValidProductIdIsProvided() throws Exception { | ||
final var expectedProductResult = ProductMother.createProduct1(); | ||
mockMvc.perform(get(URI_PRODUCT + "/" + expectedProductResult.id().toString()) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id").value(expectedProductResult.id().toString())) | ||
.andExpect(jsonPath("$.name").value(expectedProductResult.name())) | ||
.andExpect(jsonPath("$.description").value(expectedProductResult.description())) | ||
.andExpect(jsonPath("$.price").value(expectedProductResult.price().toString())) | ||
.andExpect(jsonPath("$.stock").value(expectedProductResult.stock())); | ||
} | ||
|
||
@Test | ||
void shouldReturnNotFoundError_whenNonExistentProductIdIsProvided() throws Exception { | ||
mockMvc.perform(get(URI_PRODUCT + "/" + ProductMother.NON_EXISTENT_PRODUCT_ID.toString()) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void shouldReturnBadRequestError_whenInvalidProductIdIsProvided() throws Exception { | ||
mockMvc.perform(get(URI_PRODUCT + "/" + ProductMother.INVALID_PRODUCT_ID) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
} |