From e1df78018eee69160d424c1ce488448e49d49fb0 Mon Sep 17 00:00:00 2001 From: shibha Date: Thu, 6 Jun 2013 18:03:41 +0530 Subject: [PATCH] |#755|+Anshul|Server side changes for update of product_group and including productGroupId in product upload --- .../org/openlmis/core/domain/Product.java | 2 + .../repository/ProductGroupRepository.java | 6 ++- .../core/repository/ProductRepository.java | 27 ++++++++++-- .../repository/mapper/ProductGroupMapper.java | 10 ++++- .../core/repository/mapper/ProductMapper.java | 6 +-- .../core/service/ProductGroupService.java | 6 ++- .../openlmis/core/service/ProductService.java | 22 +++++++--- .../openlmis/core/builder/ProductBuilder.java | 9 ++-- .../ProductGroupRepositoryTest.java | 17 ++++++-- .../repository/ProductRepositoryTest.java | 42 ++++++++++++++----- .../mapper/ProductGroupMapperIT.java | 21 ++++++---- .../core/service/ProductGroupServiceTest.java | 14 ++++++- .../core/service/ProductServiceTest.java | 5 +-- ...ps.sql => V5_4__create_product_groups.sql} | 0 ...products.sql => V5_5__create_products.sql} | 1 + .../main/resources/seed/product_groups.sql | 3 ++ modules/db/src/main/resources/seed/seed.sql | 1 + .../src/main/resources/messages_en.properties | 3 +- 18 files changed, 145 insertions(+), 50 deletions(-) rename modules/db/src/main/resources/db/migration/{V40__create_product_groups.sql => V5_4__create_product_groups.sql} (100%) rename modules/db/src/main/resources/db/migration/{V5_4__create_products.sql => V5_5__create_products.sql} (97%) create mode 100644 modules/db/src/main/resources/seed/product_groups.sql diff --git a/modules/core/src/main/java/org/openlmis/core/domain/Product.java b/modules/core/src/main/java/org/openlmis/core/domain/Product.java index ca9201a63f..0e2bb1749b 100644 --- a/modules/core/src/main/java/org/openlmis/core/domain/Product.java +++ b/modules/core/src/main/java/org/openlmis/core/domain/Product.java @@ -72,6 +72,8 @@ public class Product extends BaseModel implements Importable { @ImportField(mandatory = true, type = "String", name = "Product Category", nested = "code") private ProductCategory category; + @ImportField(type = "String", name = "Product Group", nested = "code") + private ProductGroup productGroup; @ImportField(type = "String", name = "Dosage Units", nested = "code") private DosageUnit dosageUnit; diff --git a/modules/core/src/main/java/org/openlmis/core/repository/ProductGroupRepository.java b/modules/core/src/main/java/org/openlmis/core/repository/ProductGroupRepository.java index 509c19bffd..2f013a6630 100644 --- a/modules/core/src/main/java/org/openlmis/core/repository/ProductGroupRepository.java +++ b/modules/core/src/main/java/org/openlmis/core/repository/ProductGroupRepository.java @@ -27,7 +27,7 @@ public ProductGroupRepository(ProductGroupMapper mapper) { this.mapper = mapper; } - public void save(ProductGroup productGroup) { + public void insert(ProductGroup productGroup) { try { mapper.insert(productGroup); } catch (DuplicateKeyException duplicateKeyException) { @@ -45,4 +45,8 @@ public void save(ProductGroup productGroup) { public ProductGroup getByCode(String code) { return mapper.getByCode(code); } + + public void update(ProductGroup productGroup) { + mapper.update(productGroup); + } } diff --git a/modules/core/src/main/java/org/openlmis/core/repository/ProductRepository.java b/modules/core/src/main/java/org/openlmis/core/repository/ProductRepository.java index 15841cc8d7..8533dffc4f 100644 --- a/modules/core/src/main/java/org/openlmis/core/repository/ProductRepository.java +++ b/modules/core/src/main/java/org/openlmis/core/repository/ProductRepository.java @@ -10,7 +10,9 @@ import org.openlmis.core.domain.DosageUnit; import org.openlmis.core.domain.Product; import org.openlmis.core.domain.ProductForm; +import org.openlmis.core.domain.ProductGroup; import org.openlmis.core.exception.DataException; +import org.openlmis.core.repository.mapper.ProductGroupMapper; import org.openlmis.core.repository.mapper.ProductMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -23,15 +25,19 @@ public class ProductRepository { ProductMapper mapper; + ProductGroupMapper productGroupMapper; + @Autowired - public ProductRepository(ProductMapper mapper) { + public ProductRepository(ProductMapper mapper, ProductGroupMapper productGroupMapper) { this.mapper = mapper; + this.productGroupMapper = productGroupMapper; } public void insert(Product product) { try { validateAndSetDosageUnit(product); validateAndSetProductForm(product); + validateAndSetProductGroup(product); mapper.insert(product); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("Duplicate Product Code found"); @@ -45,6 +51,19 @@ public void insert(Product product) { } } + private void validateAndSetProductGroup(Product product) { + ProductGroup group = product.getProductGroup(); + if (group == null) return; + + String productGroupCode = group.getCode(); + if (productGroupCode == null || productGroupCode.isEmpty()) return; + + ProductGroup productGroup = productGroupMapper.getByCode(productGroupCode); + if (productGroup == null) throw new DataException("Invalid reference data 'Product Group'"); + + group.setId(productGroup.getId()); + } + public Long getIdByCode(String code) { Long productCode = mapper.getIdByCode(code); @@ -88,10 +107,12 @@ public Product getByCode(String code) { public void update(Product product) { mapper.update(product); } - public Long getDosageUnitIdForCode(String code){ + + public Long getDosageUnitIdForCode(String code) { return mapper.getDosageUnitIdForCode(code); } - public Long getProductFormIdForCode(String code){ + + public Long getProductFormIdForCode(String code) { return mapper.getProductFormIdForCode(code); } diff --git a/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductGroupMapper.java b/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductGroupMapper.java index e22b7c1aa2..bf678de323 100644 --- a/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductGroupMapper.java +++ b/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductGroupMapper.java @@ -7,16 +7,22 @@ package org.openlmis.core.repository.mapper; import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; import org.openlmis.core.domain.ProductGroup; import org.springframework.stereotype.Repository; @Repository public interface ProductGroupMapper { - @Insert("INSERT INTO product_groups(code, name) VALUES (#{code}, #{name})") + @Insert("INSERT INTO product_groups(code, name, modifiedDate, modifiedBy) VALUES (#{code}, #{name}, #{modifiedDate}, #{modifiedBy})") + @Options(useGeneratedKeys = true) public void insert(ProductGroup productGroup); - @Select("Select * from product_groups where code=#{code}") + @Select("SELECT * FROM product_groups WHERE code=#{code}") ProductGroup getByCode(String code); + + @Update("UPDATE product_groups SET code = #{code}, name = #{name}, modifiedDate = #{modifiedDate}, modifiedBy = #{modifiedBy} WHERE id = #{id}") + void update(ProductGroup productGroup); } diff --git a/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductMapper.java b/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductMapper.java index c2d497d084..0c75f264da 100644 --- a/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductMapper.java +++ b/modules/core/src/main/java/org/openlmis/core/repository/mapper/ProductMapper.java @@ -35,7 +35,7 @@ public interface ProductMapper { "expectedShelfLife," + "specialStorageInstructions," + "specialTransportInstructions," + "active," + "fullSupply," + "tracer," + "roundToZero," + "archived," + - "packRoundingThreshold, categoryId," + + "packRoundingThreshold, categoryId, productGroupId," + "modifiedBy, modifiedDate)" + "VALUES(" + "#{code}," + @@ -58,7 +58,7 @@ public interface ProductMapper { "#{expectedShelfLife}," + "#{specialStorageInstructions}," + "#{specialTransportInstructions}," + "#{active}," + "#{fullSupply}," + "#{tracer}," + "#{roundToZero}," + "#{archived}," + - "#{packRoundingThreshold}, #{category.id}, " + + "#{packRoundingThreshold}, #{category.id}, #{productGroup.id}," + "#{modifiedBy}, #{modifiedDate})") @Options(useGeneratedKeys = true) Integer insert(Product product); @@ -91,7 +91,7 @@ public interface ProductMapper { "expectedShelfLife=#{expectedShelfLife},", "specialStorageInstructions=#{specialStorageInstructions},specialTransportInstructions=#{specialTransportInstructions},", "active=#{active},fullSupply=#{fullSupply},tracer=#{tracer},roundToZero=#{roundToZero},archived=#{archived},", - "packRoundingThreshold=#{packRoundingThreshold}, categoryId=#{category.id},", + "packRoundingThreshold=#{packRoundingThreshold}, categoryId=#{category.id}, productGroupId = #{productGroup.id},", "modifiedBy=#{modifiedBy}, modifiedDate=#{modifiedDate} WHERE id=#{id}"}) void update(Product product); } diff --git a/modules/core/src/main/java/org/openlmis/core/service/ProductGroupService.java b/modules/core/src/main/java/org/openlmis/core/service/ProductGroupService.java index 546df540d2..a1eb7a54b4 100644 --- a/modules/core/src/main/java/org/openlmis/core/service/ProductGroupService.java +++ b/modules/core/src/main/java/org/openlmis/core/service/ProductGroupService.java @@ -24,10 +24,14 @@ public ProductGroupService(ProductGroupRepository productGroupRepository) { } public void save(ProductGroup productGroup) { - productGroupRepository.save(productGroup); + if(productGroup.getId() == null) { + productGroupRepository.insert(productGroup); + } + productGroupRepository.update(productGroup); } public ProductGroup getByCode(String code) { return productGroupRepository.getByCode(code); } + } diff --git a/modules/core/src/main/java/org/openlmis/core/service/ProductService.java b/modules/core/src/main/java/org/openlmis/core/service/ProductService.java index 4e80ffdc15..906312bc55 100644 --- a/modules/core/src/main/java/org/openlmis/core/service/ProductService.java +++ b/modules/core/src/main/java/org/openlmis/core/service/ProductService.java @@ -9,9 +9,10 @@ import lombok.NoArgsConstructor; import org.openlmis.core.domain.Product; import org.openlmis.core.domain.ProductCategory; +import org.openlmis.core.domain.ProductGroup; import org.openlmis.core.exception.DataException; +import org.openlmis.core.repository.ProductGroupRepository; import org.openlmis.core.repository.ProductRepository; -import org.openlmis.upload.Importable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -21,13 +22,16 @@ public class ProductService { private ProductRepository repository; + private ProductGroupRepository productGroupRepository; private ProductCategoryService categoryService; public static final String INVALID_PRODUCT_CATEGORY_CODE = "product.reference.category.invalid"; + private static final String INVALID_PRODUCT_GROUP_CODE = "product.reference.group.invalid"; @Autowired - public ProductService(ProductRepository repository, ProductCategoryService categoryService) { + public ProductService(ProductRepository repository, ProductCategoryService categoryService, ProductGroupRepository productGroupRepository) { this.repository = repository; this.categoryService = categoryService; + this.productGroupRepository = productGroupRepository; } public void save(Product product) { @@ -38,18 +42,24 @@ public void save(Product product) { return; } - setProductFormIdAndDosageUnitId(product); + setReferenceDataForProduct(product); repository.update(product); } - private void setProductFormIdAndDosageUnitId(Product product) { - if(product.getForm()!=null ) { + private void setReferenceDataForProduct(Product product) { + if (product.getForm() != null) { product.getForm().setId(repository.getProductFormIdForCode(product.getForm().getCode())); } - if(product.getDosageUnit()!=null) { + if (product.getDosageUnit() != null) { product.getDosageUnit().setId(repository.getDosageUnitIdForCode(product.getDosageUnit().getCode())); } + if (product.getProductGroup() != null) { + ProductGroup productGroup = productGroupRepository.getByCode(product.getProductGroup().getCode()); + if (productGroup == null) throw new DataException(INVALID_PRODUCT_GROUP_CODE); + product.getProductGroup().setId(productGroup.getId()); + + } } diff --git a/modules/core/src/test/java/org/openlmis/core/builder/ProductBuilder.java b/modules/core/src/test/java/org/openlmis/core/builder/ProductBuilder.java index a1d49bc262..e61eb96fcf 100644 --- a/modules/core/src/test/java/org/openlmis/core/builder/ProductBuilder.java +++ b/modules/core/src/test/java/org/openlmis/core/builder/ProductBuilder.java @@ -9,10 +9,7 @@ import com.natpryce.makeiteasy.Instantiator; import com.natpryce.makeiteasy.Property; import com.natpryce.makeiteasy.PropertyLookup; -import org.openlmis.core.domain.DosageUnit; -import org.openlmis.core.domain.Product; -import org.openlmis.core.domain.ProductCategory; -import org.openlmis.core.domain.ProductForm; +import org.openlmis.core.domain.*; import static com.natpryce.makeiteasy.Property.newProperty; @@ -68,6 +65,10 @@ public Product instantiate(PropertyLookup lookup) { form.setCode("Tablet"); form.setId(1L); product.setForm(form); + ProductGroup productGroup = new ProductGroup(); + productGroup.setCode("PG"); + productGroup.setId(1L); + product.setProductGroup(productGroup); ProductCategory category = new ProductCategory(); category.setCode(lookup.valueOf(productCategoryCode, CATEGORY_CODE)); diff --git a/modules/core/src/test/java/org/openlmis/core/repository/ProductGroupRepositoryTest.java b/modules/core/src/test/java/org/openlmis/core/repository/ProductGroupRepositoryTest.java index 9f4a03b158..768876e7c1 100644 --- a/modules/core/src/test/java/org/openlmis/core/repository/ProductGroupRepositoryTest.java +++ b/modules/core/src/test/java/org/openlmis/core/repository/ProductGroupRepositoryTest.java @@ -42,10 +42,19 @@ public void shouldSaveProductGroup() throws Exception { ProductGroup productGroup = new ProductGroup(); repository = new ProductGroupRepository(mapper); - repository.save(productGroup); + repository.insert(productGroup); verify(mapper).insert(productGroup); } + @Test + public void shouldUpdateProductGroup() throws Exception { + ProductGroup productGroup = new ProductGroup(); + repository = new ProductGroupRepository(mapper); + + repository.update(productGroup); + verify(mapper).update(productGroup); + } + @Test public void shouldThrowDuplicateKeyExceptionWhenDuplicateProductGroupCodeFound() throws Exception { expectedEx.expect(DataException.class); @@ -53,7 +62,7 @@ public void shouldThrowDuplicateKeyExceptionWhenDuplicateProductGroupCodeFound() repository = new ProductGroupRepository(mapper); ProductGroup productGroup = new ProductGroup(); doThrow(new DuplicateKeyException("")).when(mapper).insert(productGroup); - repository.save(productGroup); + repository.insert(productGroup); } @Test @@ -63,7 +72,7 @@ public void shouldThrowDataIntegrityViolationExceptionWhenMissingMandatoryData() repository = new ProductGroupRepository(mapper); ProductGroup productGroup = new ProductGroup(); doThrow(new DataIntegrityViolationException("violates not-null constraint")).when(mapper).insert(productGroup); - repository.save(productGroup); + repository.insert(productGroup); } @Test @@ -73,6 +82,6 @@ public void shouldThrowIncorrectDataLengthErrorWhenInvalidDataLength() throws Ex repository = new ProductGroupRepository(mapper); ProductGroup productGroup = new ProductGroup(); doThrow(new DataIntegrityViolationException("")).when(mapper).insert(productGroup); - repository.save(productGroup); + repository.insert(productGroup); } } diff --git a/modules/core/src/test/java/org/openlmis/core/repository/ProductRepositoryTest.java b/modules/core/src/test/java/org/openlmis/core/repository/ProductRepositoryTest.java index 3f7790ca1c..4c99d113f8 100644 --- a/modules/core/src/test/java/org/openlmis/core/repository/ProductRepositoryTest.java +++ b/modules/core/src/test/java/org/openlmis/core/repository/ProductRepositoryTest.java @@ -13,10 +13,13 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import org.openlmis.core.builder.ProductBuilder; import org.openlmis.core.domain.Product; +import org.openlmis.core.domain.ProductGroup; import org.openlmis.core.exception.DataException; +import org.openlmis.core.repository.mapper.ProductGroupMapper; import org.openlmis.core.repository.mapper.ProductMapper; import org.openlmis.db.categories.UnitTests; import org.springframework.dao.DataIntegrityViolationException; @@ -40,11 +43,17 @@ public class ProductRepositoryTest { @Mock ProductMapper mockedMapper; + @Mock + ProductGroupMapper mockedProductGroupMapper; + ProductRepository repository; + Product product; @Before public void setUp() { - repository = new ProductRepository(mockedMapper); + repository = new ProductRepository(mockedMapper, mockedProductGroupMapper); + product = make(a(ProductBuilder.defaultProduct)); + Mockito.when(mockedProductGroupMapper.getByCode(product.getProductGroup().getCode())).thenReturn(product.getProductGroup()); } @Test @@ -56,7 +65,7 @@ public void shouldInsertProduct() throws Exception { @Test public void shouldRaiseDuplicateProductCodeError() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); + expectedEx.expect(DataException.class); expectedEx.expectMessage("Duplicate Product Code found"); doThrow(new DuplicateKeyException("")).when(mockedMapper).insert(product); @@ -65,7 +74,6 @@ public void shouldRaiseDuplicateProductCodeError() throws Exception { @Test public void shouldRaiseIncorrectReferenceDataError() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); expectedEx.expect(DataException.class); expectedEx.expectMessage("Missing/Invalid Reference data"); doThrow(new DataIntegrityViolationException("foreign key")).when(mockedMapper).insert(product); @@ -74,7 +82,6 @@ public void shouldRaiseIncorrectReferenceDataError() throws Exception { @Test public void shouldRaiseMissingReferenceDataError() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); expectedEx.expect(DataException.class); expectedEx.expectMessage("Missing/Invalid Reference data"); doThrow(new DataIntegrityViolationException("violates not-null constraint")).when(mockedMapper).insert(product); @@ -83,7 +90,6 @@ public void shouldRaiseMissingReferenceDataError() throws Exception { @Test public void shouldRaiseIncorrectDataValueError() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); expectedEx.expect(DataException.class); expectedEx.expectMessage("Incorrect data length"); doThrow(new DataIntegrityViolationException("value too long")).when(mockedMapper).insert(product); @@ -92,7 +98,6 @@ public void shouldRaiseIncorrectDataValueError() throws Exception { @Test public void shouldRaiseInvalidReferenceDataDosageUnitError() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); product.getDosageUnit().setCode("invalid code"); when(mockedMapper.getDosageUnitIdForCode("invalid code")).thenReturn(null); @@ -103,7 +108,6 @@ public void shouldRaiseInvalidReferenceDataDosageUnitError() throws Exception { @Test public void shouldSetDataDosageUnitIdForCode() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); product.getDosageUnit().setCode("valid code"); when(mockedMapper.getDosageUnitIdForCode("valid code")).thenReturn(1L); @@ -113,7 +117,6 @@ public void shouldSetDataDosageUnitIdForCode() throws Exception { @Test public void shouldRaiseInvalidReferenceDataProductFormError() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); product.getForm().setCode("invalid code"); when(mockedMapper.getProductFormIdForCode("invalid code")).thenReturn(null); @@ -124,7 +127,6 @@ public void shouldRaiseInvalidReferenceDataProductFormError() throws Exception { @Test public void shouldSetProductFormIdForCode() throws Exception { - Product product = make(a(ProductBuilder.defaultProduct)); product.getForm().setCode("valid code"); when(mockedMapper.getProductFormIdForCode("valid code")).thenReturn(1L); @@ -148,6 +150,27 @@ public void shouldThrowExceptionWhenProductCodeDoesNotExistWhenTryingToFetchProg repository.getIdByCode("code"); } + @Test + public void shouldRaiseInvalidReferenceDataProductGroupError() throws Exception { + product.getProductGroup().setCode("invalid product group code"); + when(mockedProductGroupMapper.getByCode("invalid product group code")).thenReturn(null); + + expectedEx.expect(DataException.class); + expectedEx.expectMessage("Invalid reference data 'Product Group'"); + repository.insert(product); + } + + @Test + public void shouldSetProductGroupIdForCode() throws Exception { + product.getProductGroup().setCode("valid code"); + ProductGroup productGroup = new ProductGroup(); + productGroup.setId(1L); + when(mockedProductGroupMapper.getByCode("valid code")).thenReturn(productGroup); + + repository.insert(product); + assertThat(product.getProductGroup().getId(), is(productGroup.getId())); + } + @Test public void shouldReturnProductByCode() { Product product = new Product(); @@ -157,7 +180,6 @@ public void shouldReturnProductByCode() { assertThat(returnedProduct, is(product)); } - @Test public void shouldUpdateProduct() { Product product = new Product(); diff --git a/modules/core/src/test/java/org/openlmis/core/repository/mapper/ProductGroupMapperIT.java b/modules/core/src/test/java/org/openlmis/core/repository/mapper/ProductGroupMapperIT.java index fec6a19121..90ecd6598a 100644 --- a/modules/core/src/test/java/org/openlmis/core/repository/mapper/ProductGroupMapperIT.java +++ b/modules/core/src/test/java/org/openlmis/core/repository/mapper/ProductGroupMapperIT.java @@ -33,9 +33,6 @@ @TransactionConfiguration(defaultRollback = true, transactionManager = "openLmisTransactionManager") public class ProductGroupMapperIT { - @Autowired - QueryExecutor queryExecutor; - @Autowired ProductGroupMapper mapper; @@ -53,14 +50,21 @@ public void setUp() throws Exception { public void shouldInsertProductGroup() throws Exception { mapper.insert(productGroup); - ResultSet productGroupResultSet = queryExecutor.execute("Select * from product_groups where code=?", Arrays.asList(productGroup.getCode())); - - productGroupResultSet.next(); + ProductGroup returnedProductGroup = mapper.getByCode(productGroup.getCode()); - assertThat(productGroupResultSet.getString("name"), is("Vaccines")); + assertThat(returnedProductGroup.getName(), is("Vaccines")); } + @Test + public void shouldUpdateProductGroup() throws Exception { + mapper.insert(productGroup); + productGroup.setName("Medicines"); + mapper.update(productGroup); + ProductGroup returnedProductGroup = mapper.getByCode(productGroup.getCode()); + + assertThat(returnedProductGroup.getName(), is("Medicines")); + } @Test public void shouldGetProductGroupByCode() throws Exception { @@ -68,7 +72,6 @@ public void shouldGetProductGroupByCode() throws Exception { ProductGroup returnedProductGroup = mapper.getByCode(productGroup.getCode()); - - + assertThat(returnedProductGroup.getName(), is("Vaccines")); } } diff --git a/modules/core/src/test/java/org/openlmis/core/service/ProductGroupServiceTest.java b/modules/core/src/test/java/org/openlmis/core/service/ProductGroupServiceTest.java index 27c7b5a8c1..ae4c5576fc 100644 --- a/modules/core/src/test/java/org/openlmis/core/service/ProductGroupServiceTest.java +++ b/modules/core/src/test/java/org/openlmis/core/service/ProductGroupServiceTest.java @@ -12,7 +12,6 @@ import org.mockito.runners.MockitoJUnitRunner; import org.openlmis.core.domain.ProductGroup; import org.openlmis.core.repository.ProductGroupRepository; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.mockito.Mockito.verify; @@ -29,6 +28,17 @@ public void shouldSaveProductGroup() throws Exception { service.save(productGroup); - verify(productGroupRepository).save(productGroup); + verify(productGroupRepository).insert(productGroup); + } + + @Test + public void shouldUpdateProductGroup() throws Exception { + ProductGroupService service = new ProductGroupService(productGroupRepository); + ProductGroup productGroup = new ProductGroup(); + productGroup.setId(1L); + + service.save(productGroup); + + verify(productGroupRepository).update(productGroup); } } diff --git a/modules/core/src/test/java/org/openlmis/core/service/ProductServiceTest.java b/modules/core/src/test/java/org/openlmis/core/service/ProductServiceTest.java index a679f38022..cbdd8cac3b 100644 --- a/modules/core/src/test/java/org/openlmis/core/service/ProductServiceTest.java +++ b/modules/core/src/test/java/org/openlmis/core/service/ProductServiceTest.java @@ -20,9 +20,6 @@ import org.openlmis.core.repository.ProductRepository; import org.openlmis.db.categories.UnitTests; -import java.util.Calendar; -import java.util.Date; - import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.openlmis.core.service.ProductService.INVALID_PRODUCT_CATEGORY_CODE; @@ -45,7 +42,7 @@ public class ProductServiceTest { @Before public void setUp() throws Exception { - productService = new ProductService(productRepository, categoryService); + productService = new ProductService(productRepository, categoryService, null); } @Test diff --git a/modules/db/src/main/resources/db/migration/V40__create_product_groups.sql b/modules/db/src/main/resources/db/migration/V5_4__create_product_groups.sql similarity index 100% rename from modules/db/src/main/resources/db/migration/V40__create_product_groups.sql rename to modules/db/src/main/resources/db/migration/V5_4__create_product_groups.sql diff --git a/modules/db/src/main/resources/db/migration/V5_4__create_products.sql b/modules/db/src/main/resources/db/migration/V5_5__create_products.sql similarity index 97% rename from modules/db/src/main/resources/db/migration/V5_4__create_products.sql rename to modules/db/src/main/resources/db/migration/V5_5__create_products.sql index bbecd4df59..751eebf1d6 100644 --- a/modules/db/src/main/resources/db/migration/V5_4__create_products.sql +++ b/modules/db/src/main/resources/db/migration/V5_5__create_products.sql @@ -22,6 +22,7 @@ CREATE TABLE products ( formId INTEGER REFERENCES product_forms(id), dosageUnitId INTEGER REFERENCES dosage_units(id), categoryId INTEGER REFERENCES product_categories(id), + productGroupId INTEGER REFERENCES product_groups(id), dispensingUnit VARCHAR(20) NOT NULL, dosesPerDispensingUnit SMALLINT NOT NULL, packSize SMALLINT NOT NULL, diff --git a/modules/db/src/main/resources/seed/product_groups.sql b/modules/db/src/main/resources/seed/product_groups.sql new file mode 100644 index 0000000000..36bcd023fe --- /dev/null +++ b/modules/db/src/main/resources/seed/product_groups.sql @@ -0,0 +1,3 @@ +delete from product_groups; +INSERT INTO product_groups(code, name) VALUES +('PG', 'Product Group 1'); \ No newline at end of file diff --git a/modules/db/src/main/resources/seed/seed.sql b/modules/db/src/main/resources/seed/seed.sql index bfecbf2757..2a795303bd 100644 --- a/modules/db/src/main/resources/seed/seed.sql +++ b/modules/db/src/main/resources/seed/seed.sql @@ -7,4 +7,5 @@ \i src/main/resources/seed/geographic_levels.sql \i src/main/resources/seed/geographic_zones.sql \i src/main/resources/seed/product_forms.sql +\i src/main/resources/seed/product_groups.sql \i src/main/resources/seed/programs.sql \ No newline at end of file diff --git a/modules/openlmis-web/src/main/resources/messages_en.properties b/modules/openlmis-web/src/main/resources/messages_en.properties index 2523bde28e..5921a3005b 100644 --- a/modules/openlmis-web/src/main/resources/messages_en.properties +++ b/modules/openlmis-web/src/main/resources/messages_en.properties @@ -76,7 +76,7 @@ programProduct.invalid.current.price= Invalid Price per pack supported.programs.invalid = Start date is a must for Active program product.category.name.duplicate = Duplicate Product Category Name found product.reference.category.invalid = Invalid reference data 'Product Category' - +product.reference.group.invalid= Invalid reference data 'Product Group' title.manage.users = Manage Users - OpenLMIS user.data.length.incorrect = Incorrect data type or length @@ -382,3 +382,4 @@ message.facility.created.success=Facility '%s' created successfully message.facility.updated.success=Facility '%s' updated successfully +