Skip to content

Commit

Permalink
Fix duplicated error test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-taman committed Apr 14, 2020
1 parent 766c50d commit e89c43f
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 291 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Mac files
.DS_Store

## Drawio source files ##
*.drawio

Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
package com.siriusxi.ms.store.ps.service;

import com.mongodb.DuplicateKeyException;
import com.siriusxi.ms.store.api.core.product.dto.Product;
import com.siriusxi.ms.store.api.core.product.ProductService;
import com.siriusxi.ms.store.api.core.product.dto.Product;
import com.siriusxi.ms.store.ps.persistence.ProductEntity;
import com.siriusxi.ms.store.ps.persistence.ProductRepository;
import com.siriusxi.ms.store.util.exceptions.InvalidInputException;
import com.siriusxi.ms.store.util.exceptions.NotFoundException;
import com.siriusxi.ms.store.util.http.ServiceUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

@Service("ProductServiceImpl")
@Log4j2
public class ProductServiceImpl implements ProductService {

private final ServiceUtil serviceUtil;
private final ServiceUtil serviceUtil;

private final ProductRepository repository;
private final ProductRepository repository;

private final ProductMapper mapper;
private final ProductMapper mapper;

@Autowired
public ProductServiceImpl(ProductRepository repository,
ProductMapper mapper,
ServiceUtil serviceUtil) {
this.repository = repository;
this.mapper = mapper;
this.serviceUtil = serviceUtil;
}
@Autowired
public ProductServiceImpl(
ProductRepository repository, ProductMapper mapper, ServiceUtil serviceUtil) {
this.repository = repository;
this.mapper = mapper;
this.serviceUtil = serviceUtil;
}

@Override
public Product createProduct(Product body) {
try {
ProductEntity entity = mapper.apiToEntity(body);
ProductEntity newEntity = repository.save(entity);
@Override
public Product createProduct(Product body) {
try {
ProductEntity entity = mapper.apiToEntity(body);
ProductEntity newEntity = repository.save(entity);

log.debug("createProduct: entity created for productId: {}", body.getProductId());
return mapper.entityToApi(newEntity);
log.debug("createProduct: entity created for productId: {}", body.getProductId());
return mapper.entityToApi(newEntity);

} catch (DuplicateKeyException dke) {
throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId());
}
} catch (DuplicateKeyException dke) {
throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId());
}
}

@Override
public Product getProduct(int productId) {
if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId);
@Override
public Product getProduct(int productId) {
if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId);

ProductEntity entity = repository.findByProductId(productId)
.orElseThrow(() -> new NotFoundException("No product found for productId: " + productId));
ProductEntity entity =
repository
.findByProductId(productId)
.orElseThrow(
() -> new NotFoundException("No product found for productId: " + productId));

Product response = mapper.entityToApi(entity);
response.setServiceAddress(serviceUtil.getServiceAddress());
Product response = mapper.entityToApi(entity);
response.setServiceAddress(serviceUtil.getServiceAddress());

log.debug("getProduct: found productId: {}", response.getProductId());
log.debug("getProduct: found productId: {}", response.getProductId());

return response;
}
return response;
}

/*
Implementation is idempotent, that is,
it will not report any failure if the entity is not found Always 200
*/
@Override
public void deleteProduct(int productId) {
log.debug("deleteProduct: tries to delete an entity with productId: {}", productId);
repository.findByProductId(productId).ifPresent(repository::delete);
}
/*
Implementation is idempotent, that is,
it will not report any failure if the entity is not found Always 200
*/
@Override
public void deleteProduct(int productId) {
log.debug("deleteProduct: tries to delete an entity with productId: {}", productId);
repository.findByProductId(productId).ifPresent(repository::delete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.siriusxi.ms.store.api.core.product.dto.Product;
import com.siriusxi.ms.store.ps.persistence.ProductRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -17,125 +16,140 @@
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;

@SpringBootTest(webEnvironment= RANDOM_PORT, properties = {"spring.data.mongodb.port: 0"})
@SpringBootTest(
webEnvironment = RANDOM_PORT,
properties = {"spring.data.mongodb.port: 0"})
class ProductServiceApplicationTests {

private final String BASE_URI = "/products/";
private final String BASE_URI = "/products/";

@Autowired
private WebTestClient client;
@Autowired private WebTestClient client;

@Autowired
private ProductRepository repository;
@Autowired private ProductRepository repository;

@BeforeEach
public void setupDb() {
repository.deleteAll();
}
@BeforeEach
public void setupDb() {
repository.deleteAll();
}

@Test
public void getProductById() {
@Test
public void getProductById() {

int productId = 1;
int productId = 1;

postAndVerifyProduct(productId, OK);

assertTrue(repository.findByProductId(productId).isPresent());

getAndVerifyProduct(productId, OK)
.jsonPath("$.productId").isEqualTo(productId);
}

@Test
@Disabled
public void duplicateError() {

int productId = 1;

postAndVerifyProduct(productId, OK);

assertTrue(repository.findByProductId(productId).isPresent());

postAndVerifyProduct(productId, UNPROCESSABLE_ENTITY)
.jsonPath("$.path").isEqualTo("BASE_RESOURCE_URI")
.jsonPath("$.message").isEqualTo("Duplicate key, Product Id: " + productId);
}

@Test
public void deleteProduct() {

int productId = 1;

postAndVerifyProduct(productId, OK);
assertTrue(repository.findByProductId(productId).isPresent());

deleteAndVerifyProduct(productId);
assertFalse(repository.findByProductId(productId).isPresent());

deleteAndVerifyProduct(productId);
}

@Test
public void getProductInvalidParameterString() {

getAndVerifyProduct(BASE_URI + "/no-integer", BAD_REQUEST)
.jsonPath("$.path").isEqualTo(BASE_URI + "no-integer")
.jsonPath("$.message").isEqualTo("Type mismatch.");
}

@Test
public void getProductNotFound() {

int productIdNotFound = 13;
getAndVerifyProduct(productIdNotFound, NOT_FOUND)
.jsonPath("$.path").isEqualTo(BASE_URI + productIdNotFound)
.jsonPath("$.message").isEqualTo("No product found for productId: " + productIdNotFound);
}

@Test
public void getProductInvalidParameterNegativeValue() {

int productIdInvalid = -1;

getAndVerifyProduct(productIdInvalid, UNPROCESSABLE_ENTITY)
.jsonPath("$.path").isEqualTo(BASE_URI + productIdInvalid)
.jsonPath("$.message").isEqualTo("Invalid productId: " + productIdInvalid);
}


private WebTestClient.BodyContentSpec getAndVerifyProduct(int productId, HttpStatus expectedStatus) {
return getAndVerifyProduct(BASE_URI + productId, expectedStatus);
}

private WebTestClient.BodyContentSpec getAndVerifyProduct(String productIdPath, HttpStatus expectedStatus) {
return client.get()
.uri(productIdPath)
.accept(APPLICATION_JSON)
.exchange()
.expectStatus().isEqualTo(expectedStatus)
.expectHeader().contentType(APPLICATION_JSON)
.expectBody();
}

private WebTestClient.BodyContentSpec postAndVerifyProduct(int productId, HttpStatus expectedStatus) {
Product product = new Product(productId, "Name " + productId, productId, "SA");
return client.post()
.uri(BASE_URI)
.body(Mono.just(product), Product.class)
.accept(APPLICATION_JSON)
.exchange()
.expectStatus().isEqualTo(expectedStatus)
.expectHeader().contentType(APPLICATION_JSON)
.expectBody();
}

private void deleteAndVerifyProduct(int productId) {
client.delete()
.uri(BASE_URI + productId)
.accept(APPLICATION_JSON)
.exchange()
.expectStatus().isEqualTo(OK)
.expectBody();
}
postAndVerifyProduct(productId, OK);

assertTrue(repository.findByProductId(productId).isPresent());

getAndVerifyProduct(productId, OK).jsonPath("$.productId").isEqualTo(productId);
}

@Test
public void duplicateError() {

int productId = 1;

postAndVerifyProduct(productId, OK);

assertTrue(repository.findByProductId(productId).isPresent());

postAndVerifyProduct(productId, UNPROCESSABLE_ENTITY)
.jsonPath("$.path")
.isEqualTo(BASE_URI)
.jsonPath("$.message")
.isEqualTo("Duplicate key, Product Id: " + productId);
}

@Test
public void deleteProduct() {

int productId = 1;

postAndVerifyProduct(productId, OK);
assertTrue(repository.findByProductId(productId).isPresent());

deleteAndVerifyProduct(productId);
assertFalse(repository.findByProductId(productId).isPresent());

deleteAndVerifyProduct(productId);
}

@Test
public void getProductInvalidParameterString() {

getAndVerifyProduct(BASE_URI + "/no-integer", BAD_REQUEST)
.jsonPath("$.path")
.isEqualTo(BASE_URI + "no-integer")
.jsonPath("$.message")
.isEqualTo("Type mismatch.");
}

@Test
public void getProductNotFound() {

int productIdNotFound = 13;
getAndVerifyProduct(productIdNotFound, NOT_FOUND)
.jsonPath("$.path")
.isEqualTo(BASE_URI + productIdNotFound)
.jsonPath("$.message")
.isEqualTo("No product found for productId: " + productIdNotFound);
}

@Test
public void getProductInvalidParameterNegativeValue() {

int productIdInvalid = -1;

getAndVerifyProduct(productIdInvalid, UNPROCESSABLE_ENTITY)
.jsonPath("$.path")
.isEqualTo(BASE_URI + productIdInvalid)
.jsonPath("$.message")
.isEqualTo("Invalid productId: " + productIdInvalid);
}

private WebTestClient.BodyContentSpec getAndVerifyProduct(
int productId, HttpStatus expectedStatus) {
return getAndVerifyProduct(BASE_URI + productId, expectedStatus);
}

private WebTestClient.BodyContentSpec getAndVerifyProduct(
String productIdPath, HttpStatus expectedStatus) {
return client
.get()
.uri(productIdPath)
.accept(APPLICATION_JSON)
.exchange()
.expectStatus()
.isEqualTo(expectedStatus)
.expectHeader()
.contentType(APPLICATION_JSON)
.expectBody();
}

private WebTestClient.BodyContentSpec postAndVerifyProduct(
int productId, HttpStatus expectedStatus) {
Product product = new Product(productId, "Name " + productId, productId, "SA");
return client
.post()
.uri(BASE_URI)
.body(Mono.just(product), Product.class)
.accept(APPLICATION_JSON)
.exchange()
.expectStatus()
.isEqualTo(expectedStatus)
.expectHeader()
.contentType(APPLICATION_JSON)
.expectBody();
}

private void deleteAndVerifyProduct(int productId) {
client
.delete()
.uri(BASE_URI + productId)
.accept(APPLICATION_JSON)
.exchange()
.expectStatus()
.isEqualTo(OK)
.expectBody();
}
}
Loading

0 comments on commit e89c43f

Please sign in to comment.