Skip to content

Commit e89c43f

Browse files
committed
Fix duplicated error test cases.
1 parent 766c50d commit e89c43f

File tree

5 files changed

+332
-291
lines changed

5 files changed

+332
-291
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Mac files
2+
.DS_Store
3+
14
## Drawio source files ##
25
*.drawio
36

Original file line numberDiff line numberDiff line change
@@ -1,72 +1,74 @@
11
package com.siriusxi.ms.store.ps.service;
22

3-
import com.mongodb.DuplicateKeyException;
4-
import com.siriusxi.ms.store.api.core.product.dto.Product;
53
import com.siriusxi.ms.store.api.core.product.ProductService;
4+
import com.siriusxi.ms.store.api.core.product.dto.Product;
65
import com.siriusxi.ms.store.ps.persistence.ProductEntity;
76
import com.siriusxi.ms.store.ps.persistence.ProductRepository;
87
import com.siriusxi.ms.store.util.exceptions.InvalidInputException;
98
import com.siriusxi.ms.store.util.exceptions.NotFoundException;
109
import com.siriusxi.ms.store.util.http.ServiceUtil;
1110
import lombok.extern.log4j.Log4j2;
1211
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.dao.DuplicateKeyException;
1313
import org.springframework.stereotype.Service;
1414

1515
@Service("ProductServiceImpl")
1616
@Log4j2
1717
public class ProductServiceImpl implements ProductService {
1818

19-
private final ServiceUtil serviceUtil;
19+
private final ServiceUtil serviceUtil;
2020

21-
private final ProductRepository repository;
21+
private final ProductRepository repository;
2222

23-
private final ProductMapper mapper;
23+
private final ProductMapper mapper;
2424

25-
@Autowired
26-
public ProductServiceImpl(ProductRepository repository,
27-
ProductMapper mapper,
28-
ServiceUtil serviceUtil) {
29-
this.repository = repository;
30-
this.mapper = mapper;
31-
this.serviceUtil = serviceUtil;
32-
}
25+
@Autowired
26+
public ProductServiceImpl(
27+
ProductRepository repository, ProductMapper mapper, ServiceUtil serviceUtil) {
28+
this.repository = repository;
29+
this.mapper = mapper;
30+
this.serviceUtil = serviceUtil;
31+
}
3332

34-
@Override
35-
public Product createProduct(Product body) {
36-
try {
37-
ProductEntity entity = mapper.apiToEntity(body);
38-
ProductEntity newEntity = repository.save(entity);
33+
@Override
34+
public Product createProduct(Product body) {
35+
try {
36+
ProductEntity entity = mapper.apiToEntity(body);
37+
ProductEntity newEntity = repository.save(entity);
3938

40-
log.debug("createProduct: entity created for productId: {}", body.getProductId());
41-
return mapper.entityToApi(newEntity);
39+
log.debug("createProduct: entity created for productId: {}", body.getProductId());
40+
return mapper.entityToApi(newEntity);
4241

43-
} catch (DuplicateKeyException dke) {
44-
throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId());
45-
}
42+
} catch (DuplicateKeyException dke) {
43+
throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId());
4644
}
45+
}
4746

48-
@Override
49-
public Product getProduct(int productId) {
50-
if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId);
47+
@Override
48+
public Product getProduct(int productId) {
49+
if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId);
5150

52-
ProductEntity entity = repository.findByProductId(productId)
53-
.orElseThrow(() -> new NotFoundException("No product found for productId: " + productId));
51+
ProductEntity entity =
52+
repository
53+
.findByProductId(productId)
54+
.orElseThrow(
55+
() -> new NotFoundException("No product found for productId: " + productId));
5456

55-
Product response = mapper.entityToApi(entity);
56-
response.setServiceAddress(serviceUtil.getServiceAddress());
57+
Product response = mapper.entityToApi(entity);
58+
response.setServiceAddress(serviceUtil.getServiceAddress());
5759

58-
log.debug("getProduct: found productId: {}", response.getProductId());
60+
log.debug("getProduct: found productId: {}", response.getProductId());
5961

60-
return response;
61-
}
62+
return response;
63+
}
6264

63-
/*
64-
Implementation is idempotent, that is,
65-
it will not report any failure if the entity is not found Always 200
66-
*/
67-
@Override
68-
public void deleteProduct(int productId) {
69-
log.debug("deleteProduct: tries to delete an entity with productId: {}", productId);
70-
repository.findByProductId(productId).ifPresent(repository::delete);
71-
}
65+
/*
66+
Implementation is idempotent, that is,
67+
it will not report any failure if the entity is not found Always 200
68+
*/
69+
@Override
70+
public void deleteProduct(int productId) {
71+
log.debug("deleteProduct: tries to delete an entity with productId: {}", productId);
72+
repository.findByProductId(productId).ifPresent(repository::delete);
73+
}
7274
}

product-service/src/test/java/com/siriusxi/ms/store/ps/ProductServiceApplicationTests.java

+127-113
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.siriusxi.ms.store.api.core.product.dto.Product;
44
import com.siriusxi.ms.store.ps.persistence.ProductRepository;
55
import org.junit.jupiter.api.BeforeEach;
6-
import org.junit.jupiter.api.Disabled;
76
import org.junit.jupiter.api.Test;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.boot.test.context.SpringBootTest;
@@ -17,125 +16,140 @@
1716
import static org.springframework.http.HttpStatus.*;
1817
import static org.springframework.http.MediaType.APPLICATION_JSON;
1918

20-
@SpringBootTest(webEnvironment= RANDOM_PORT, properties = {"spring.data.mongodb.port: 0"})
19+
@SpringBootTest(
20+
webEnvironment = RANDOM_PORT,
21+
properties = {"spring.data.mongodb.port: 0"})
2122
class ProductServiceApplicationTests {
2223

23-
private final String BASE_URI = "/products/";
24+
private final String BASE_URI = "/products/";
2425

25-
@Autowired
26-
private WebTestClient client;
26+
@Autowired private WebTestClient client;
2727

28-
@Autowired
29-
private ProductRepository repository;
28+
@Autowired private ProductRepository repository;
3029

31-
@BeforeEach
32-
public void setupDb() {
33-
repository.deleteAll();
34-
}
30+
@BeforeEach
31+
public void setupDb() {
32+
repository.deleteAll();
33+
}
3534

36-
@Test
37-
public void getProductById() {
35+
@Test
36+
public void getProductById() {
3837

39-
int productId = 1;
38+
int productId = 1;
4039

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);
14041

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+
}
141155
}

0 commit comments

Comments
 (0)