Skip to content

Commit

Permalink
Merge branch 'release/3.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-taman committed Apr 14, 2020
2 parents b673db6 + e89c43f commit bff4969
Show file tree
Hide file tree
Showing 38 changed files with 1,271 additions and 741 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
@@ -0,0 +1,49 @@
package com.siriusxi.ms.store.ps.api;

import com.siriusxi.ms.store.api.core.product.ProductEndpoint;
import com.siriusxi.ms.store.api.core.product.ProductService;
import com.siriusxi.ms.store.api.core.product.dto.Product;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RestController;

/**
* Class <code>ProductController</code> is the implementation of the main Product Endpoint API
* definition.
*
* @see ProductEndpoint
* @author mohamed.taman
* @version v1.0
* @since v3.0 codename Storm
*/
@RestController
@Log4j2
public class ProductController implements ProductEndpoint {

/** Product service business logic interface. */
private final ProductService prodService;

@Autowired
public ProductController(@Qualifier("ProductServiceImpl") ProductService prodService) {
this.prodService = prodService;
}

/** {@inheritDoc} */
@Override
public Product getProduct(int id) {
return prodService.getProduct(id);
}

/** {@inheritDoc} */
@Override
public Product createProduct(Product body) {
return prodService.createProduct(body);
}

/** {@inheritDoc} */
@Override
public void deleteProduct(int id) {
prodService.deleteProduct(id);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.siriusxi.ms.store.ps.controller;
package com.siriusxi.ms.store.ps.service;

import com.siriusxi.ms.store.api.core.product.Product;
import com.siriusxi.ms.store.api.core.product.dto.Product;
import com.siriusxi.ms.store.ps.persistence.ProductEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.siriusxi.ms.store.ps.service;

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 ProductRepository repository;

private final ProductMapper mapper;

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

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());
}
}

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

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

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

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);
}
}
5 changes: 3 additions & 2 deletions product-service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ spring:
host: localhost
port: 27017
database: product-db

auto-index-creation: true

server:
port: 9081

Expand Down Expand Up @@ -39,4 +40,4 @@ spring:
host: mongodb

server:
port: 8080
port: 8080
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.siriusxi.ms.store.ps;

import com.siriusxi.ms.store.api.core.product.Product;
import com.siriusxi.ms.store.ps.controller.ProductMapper;
import com.siriusxi.ms.store.api.core.product.dto.Product;
import com.siriusxi.ms.store.ps.service.ProductMapper;
import com.siriusxi.ms.store.ps.persistence.ProductEntity;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class MapperTests {

private final ProductMapper mapper = ProductMapper.INSTANCE;
private final ProductMapper mapper = ProductMapper.INSTANCE;

@Test
public void mapperTests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.siriusxi.ms.store.ps.persistence.ProductRepository;
import org.junit.jupiter.api.Assertions;
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.autoconfigure.data.mongo.DataMongoTest;
Expand Down Expand Up @@ -76,9 +75,7 @@ public void getByProductId() {
assertEqualsProduct(savedEntity, entity.get());
}

//FIXME error which is not thrown
@Test
@Disabled
public void duplicateError() {

Assertions.assertThrows(
Expand Down
Loading

0 comments on commit bff4969

Please sign in to comment.