|
3 | 3 | import com.siriusxi.ms.store.api.core.product.dto.Product;
|
4 | 4 | import com.siriusxi.ms.store.ps.persistence.ProductRepository;
|
5 | 5 | import org.junit.jupiter.api.BeforeEach;
|
6 |
| -import org.junit.jupiter.api.Disabled; |
7 | 6 | import org.junit.jupiter.api.Test;
|
8 | 7 | import org.springframework.beans.factory.annotation.Autowired;
|
9 | 8 | import org.springframework.boot.test.context.SpringBootTest;
|
|
17 | 16 | import static org.springframework.http.HttpStatus.*;
|
18 | 17 | import static org.springframework.http.MediaType.APPLICATION_JSON;
|
19 | 18 |
|
20 |
| -@SpringBootTest(webEnvironment= RANDOM_PORT, properties = {"spring.data.mongodb.port: 0"}) |
| 19 | +@SpringBootTest( |
| 20 | + webEnvironment = RANDOM_PORT, |
| 21 | + properties = {"spring.data.mongodb.port: 0"}) |
21 | 22 | class ProductServiceApplicationTests {
|
22 | 23 |
|
23 |
| - private final String BASE_URI = "/products/"; |
| 24 | + private final String BASE_URI = "/products/"; |
24 | 25 |
|
25 |
| - @Autowired |
26 |
| - private WebTestClient client; |
| 26 | + @Autowired private WebTestClient client; |
27 | 27 |
|
28 |
| - @Autowired |
29 |
| - private ProductRepository repository; |
| 28 | + @Autowired private ProductRepository repository; |
30 | 29 |
|
31 |
| - @BeforeEach |
32 |
| - public void setupDb() { |
33 |
| - repository.deleteAll(); |
34 |
| - } |
| 30 | + @BeforeEach |
| 31 | + public void setupDb() { |
| 32 | + repository.deleteAll(); |
| 33 | + } |
35 | 34 |
|
36 |
| - @Test |
37 |
| - public void getProductById() { |
| 35 | + @Test |
| 36 | + public void getProductById() { |
38 | 37 |
|
39 |
| - int productId = 1; |
| 38 | + int productId = 1; |
40 | 39 |
|
41 |
| - postAndVerifyProduct(productId, OK); |
42 |
| - |
43 |
| - assertTrue(repository.findByProductId(productId).isPresent()); |
44 |
| - |
45 |
| - getAndVerifyProduct(productId, OK) |
46 |
| - .jsonPath("$.productId").isEqualTo(productId); |
47 |
| - } |
48 |
| - |
49 |
| - @Test |
50 |
| - @Disabled |
51 |
| - public void duplicateError() { |
52 |
| - |
53 |
| - int productId = 1; |
54 |
| - |
55 |
| - postAndVerifyProduct(productId, OK); |
56 |
| - |
57 |
| - assertTrue(repository.findByProductId(productId).isPresent()); |
58 |
| - |
59 |
| - postAndVerifyProduct(productId, UNPROCESSABLE_ENTITY) |
60 |
| - .jsonPath("$.path").isEqualTo("BASE_RESOURCE_URI") |
61 |
| - .jsonPath("$.message").isEqualTo("Duplicate key, Product Id: " + productId); |
62 |
| - } |
63 |
| - |
64 |
| - @Test |
65 |
| - public void deleteProduct() { |
66 |
| - |
67 |
| - int productId = 1; |
68 |
| - |
69 |
| - postAndVerifyProduct(productId, OK); |
70 |
| - assertTrue(repository.findByProductId(productId).isPresent()); |
71 |
| - |
72 |
| - deleteAndVerifyProduct(productId); |
73 |
| - assertFalse(repository.findByProductId(productId).isPresent()); |
74 |
| - |
75 |
| - deleteAndVerifyProduct(productId); |
76 |
| - } |
77 |
| - |
78 |
| - @Test |
79 |
| - public void getProductInvalidParameterString() { |
80 |
| - |
81 |
| - getAndVerifyProduct(BASE_URI + "/no-integer", BAD_REQUEST) |
82 |
| - .jsonPath("$.path").isEqualTo(BASE_URI + "no-integer") |
83 |
| - .jsonPath("$.message").isEqualTo("Type mismatch."); |
84 |
| - } |
85 |
| - |
86 |
| - @Test |
87 |
| - public void getProductNotFound() { |
88 |
| - |
89 |
| - int productIdNotFound = 13; |
90 |
| - getAndVerifyProduct(productIdNotFound, NOT_FOUND) |
91 |
| - .jsonPath("$.path").isEqualTo(BASE_URI + productIdNotFound) |
92 |
| - .jsonPath("$.message").isEqualTo("No product found for productId: " + productIdNotFound); |
93 |
| - } |
94 |
| - |
95 |
| - @Test |
96 |
| - public void getProductInvalidParameterNegativeValue() { |
97 |
| - |
98 |
| - int productIdInvalid = -1; |
99 |
| - |
100 |
| - getAndVerifyProduct(productIdInvalid, UNPROCESSABLE_ENTITY) |
101 |
| - .jsonPath("$.path").isEqualTo(BASE_URI + productIdInvalid) |
102 |
| - .jsonPath("$.message").isEqualTo("Invalid productId: " + productIdInvalid); |
103 |
| - } |
104 |
| - |
105 |
| - |
106 |
| - private WebTestClient.BodyContentSpec getAndVerifyProduct(int productId, HttpStatus expectedStatus) { |
107 |
| - return getAndVerifyProduct(BASE_URI + productId, expectedStatus); |
108 |
| - } |
109 |
| - |
110 |
| - private WebTestClient.BodyContentSpec getAndVerifyProduct(String productIdPath, HttpStatus expectedStatus) { |
111 |
| - return client.get() |
112 |
| - .uri(productIdPath) |
113 |
| - .accept(APPLICATION_JSON) |
114 |
| - .exchange() |
115 |
| - .expectStatus().isEqualTo(expectedStatus) |
116 |
| - .expectHeader().contentType(APPLICATION_JSON) |
117 |
| - .expectBody(); |
118 |
| - } |
119 |
| - |
120 |
| - private WebTestClient.BodyContentSpec postAndVerifyProduct(int productId, HttpStatus expectedStatus) { |
121 |
| - Product product = new Product(productId, "Name " + productId, productId, "SA"); |
122 |
| - return client.post() |
123 |
| - .uri(BASE_URI) |
124 |
| - .body(Mono.just(product), Product.class) |
125 |
| - .accept(APPLICATION_JSON) |
126 |
| - .exchange() |
127 |
| - .expectStatus().isEqualTo(expectedStatus) |
128 |
| - .expectHeader().contentType(APPLICATION_JSON) |
129 |
| - .expectBody(); |
130 |
| - } |
131 |
| - |
132 |
| - private void deleteAndVerifyProduct(int productId) { |
133 |
| - client.delete() |
134 |
| - .uri(BASE_URI + productId) |
135 |
| - .accept(APPLICATION_JSON) |
136 |
| - .exchange() |
137 |
| - .expectStatus().isEqualTo(OK) |
138 |
| - .expectBody(); |
139 |
| - } |
| 40 | + postAndVerifyProduct(productId, OK); |
140 | 41 |
|
| 42 | + assertTrue(repository.findByProductId(productId).isPresent()); |
| 43 | + |
| 44 | + getAndVerifyProduct(productId, OK).jsonPath("$.productId").isEqualTo(productId); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void duplicateError() { |
| 49 | + |
| 50 | + int productId = 1; |
| 51 | + |
| 52 | + postAndVerifyProduct(productId, OK); |
| 53 | + |
| 54 | + assertTrue(repository.findByProductId(productId).isPresent()); |
| 55 | + |
| 56 | + postAndVerifyProduct(productId, UNPROCESSABLE_ENTITY) |
| 57 | + .jsonPath("$.path") |
| 58 | + .isEqualTo(BASE_URI) |
| 59 | + .jsonPath("$.message") |
| 60 | + .isEqualTo("Duplicate key, Product Id: " + productId); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void deleteProduct() { |
| 65 | + |
| 66 | + int productId = 1; |
| 67 | + |
| 68 | + postAndVerifyProduct(productId, OK); |
| 69 | + assertTrue(repository.findByProductId(productId).isPresent()); |
| 70 | + |
| 71 | + deleteAndVerifyProduct(productId); |
| 72 | + assertFalse(repository.findByProductId(productId).isPresent()); |
| 73 | + |
| 74 | + deleteAndVerifyProduct(productId); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void getProductInvalidParameterString() { |
| 79 | + |
| 80 | + getAndVerifyProduct(BASE_URI + "/no-integer", BAD_REQUEST) |
| 81 | + .jsonPath("$.path") |
| 82 | + .isEqualTo(BASE_URI + "no-integer") |
| 83 | + .jsonPath("$.message") |
| 84 | + .isEqualTo("Type mismatch."); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void getProductNotFound() { |
| 89 | + |
| 90 | + int productIdNotFound = 13; |
| 91 | + getAndVerifyProduct(productIdNotFound, NOT_FOUND) |
| 92 | + .jsonPath("$.path") |
| 93 | + .isEqualTo(BASE_URI + productIdNotFound) |
| 94 | + .jsonPath("$.message") |
| 95 | + .isEqualTo("No product found for productId: " + productIdNotFound); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void getProductInvalidParameterNegativeValue() { |
| 100 | + |
| 101 | + int productIdInvalid = -1; |
| 102 | + |
| 103 | + getAndVerifyProduct(productIdInvalid, UNPROCESSABLE_ENTITY) |
| 104 | + .jsonPath("$.path") |
| 105 | + .isEqualTo(BASE_URI + productIdInvalid) |
| 106 | + .jsonPath("$.message") |
| 107 | + .isEqualTo("Invalid productId: " + productIdInvalid); |
| 108 | + } |
| 109 | + |
| 110 | + private WebTestClient.BodyContentSpec getAndVerifyProduct( |
| 111 | + int productId, HttpStatus expectedStatus) { |
| 112 | + return getAndVerifyProduct(BASE_URI + productId, expectedStatus); |
| 113 | + } |
| 114 | + |
| 115 | + private WebTestClient.BodyContentSpec getAndVerifyProduct( |
| 116 | + String productIdPath, HttpStatus expectedStatus) { |
| 117 | + return client |
| 118 | + .get() |
| 119 | + .uri(productIdPath) |
| 120 | + .accept(APPLICATION_JSON) |
| 121 | + .exchange() |
| 122 | + .expectStatus() |
| 123 | + .isEqualTo(expectedStatus) |
| 124 | + .expectHeader() |
| 125 | + .contentType(APPLICATION_JSON) |
| 126 | + .expectBody(); |
| 127 | + } |
| 128 | + |
| 129 | + private WebTestClient.BodyContentSpec postAndVerifyProduct( |
| 130 | + int productId, HttpStatus expectedStatus) { |
| 131 | + Product product = new Product(productId, "Name " + productId, productId, "SA"); |
| 132 | + return client |
| 133 | + .post() |
| 134 | + .uri(BASE_URI) |
| 135 | + .body(Mono.just(product), Product.class) |
| 136 | + .accept(APPLICATION_JSON) |
| 137 | + .exchange() |
| 138 | + .expectStatus() |
| 139 | + .isEqualTo(expectedStatus) |
| 140 | + .expectHeader() |
| 141 | + .contentType(APPLICATION_JSON) |
| 142 | + .expectBody(); |
| 143 | + } |
| 144 | + |
| 145 | + private void deleteAndVerifyProduct(int productId) { |
| 146 | + client |
| 147 | + .delete() |
| 148 | + .uri(BASE_URI + productId) |
| 149 | + .accept(APPLICATION_JSON) |
| 150 | + .exchange() |
| 151 | + .expectStatus() |
| 152 | + .isEqualTo(OK) |
| 153 | + .expectBody(); |
| 154 | + } |
141 | 155 | }
|
0 commit comments