@Test public void shouldCreateStockCardIfItDoesNotExist() { when(mapper.getByFacilityAndProduct(defaultFacility.getId(), defaultProduct.getCode())).thenReturn(null); when(facilityRepository.getById(defaultFacility.getId())).thenReturn(defaultFacility); when(productRepository.getByCode(defaultProduct.getCode())).thenReturn(defaultProduct); StockCard stockCard = stockCardRepository.getOrCreateStockCard(defaultFacility.getId(), defaultProduct.getCode()); verify(mapper).insert(stockCard); assertThat(stockCard.getFacility(), is(defaultFacility)); assertThat(stockCard.getProduct(), is(defaultProduct)); }
public StockCard getOrCreateStockCard(long facilityId, String productCode) { StockCard card = mapper.getByFacilityAndProduct(facilityId, productCode); if (null == card) { Facility facility = facilityRepository.getById(facilityId); Product product = productRepository.getByCode(productCode); Objects.requireNonNull(facility); Objects.requireNonNull(product); card = StockCard.createZeroedStockCard(facility, product); mapper.insert(card); } Objects.requireNonNull(card); return card; }
StockCardRepository { public StockCard getOrCreateStockCard(long facilityId, String productCode) { StockCard card = mapper.getByFacilityAndProduct(facilityId, productCode); if (null == card) { Facility facility = facilityRepository.getById(facilityId); Product product = productRepository.getByCode(productCode); Objects.requireNonNull(facility); Objects.requireNonNull(product); card = StockCard.createZeroedStockCard(facility, product); mapper.insert(card); } Objects.requireNonNull(card); return card; } }
StockCardRepository { public StockCard getOrCreateStockCard(long facilityId, String productCode) { StockCard card = mapper.getByFacilityAndProduct(facilityId, productCode); if (null == card) { Facility facility = facilityRepository.getById(facilityId); Product product = productRepository.getByCode(productCode); Objects.requireNonNull(facility); Objects.requireNonNull(product); card = StockCard.createZeroedStockCard(facility, product); mapper.insert(card); } Objects.requireNonNull(card); return card; }  }
StockCardRepository { public StockCard getOrCreateStockCard(long facilityId, String productCode) { StockCard card = mapper.getByFacilityAndProduct(facilityId, productCode); if (null == card) { Facility facility = facilityRepository.getById(facilityId); Product product = productRepository.getByCode(productCode); Objects.requireNonNull(facility); Objects.requireNonNull(product); card = StockCard.createZeroedStockCard(facility, product); mapper.insert(card); } Objects.requireNonNull(card); return card; }  StockCard getOrCreateStockCard(long facilityId, String productCode); StockCard getStockCardByFacilityAndProduct(long facilityId, String productCode); StockCard getStockCardById(Long facilityId, Long id); List<StockCard> getStockCards(Long facilityId); void persistStockCardEntry(StockCardEntry entry); void updateStockCard(StockCard card); Product getProductByStockCardId(Long stockCardId); }
StockCardRepository { public StockCard getOrCreateStockCard(long facilityId, String productCode) { StockCard card = mapper.getByFacilityAndProduct(facilityId, productCode); if (null == card) { Facility facility = facilityRepository.getById(facilityId); Product product = productRepository.getByCode(productCode); Objects.requireNonNull(facility); Objects.requireNonNull(product); card = StockCard.createZeroedStockCard(facility, product); mapper.insert(card); } Objects.requireNonNull(card); return card; }  StockCard getOrCreateStockCard(long facilityId, String productCode); StockCard getStockCardByFacilityAndProduct(long facilityId, String productCode); StockCard getStockCardById(Long facilityId, Long id); List<StockCard> getStockCards(Long facilityId); void persistStockCardEntry(StockCardEntry entry); void updateStockCard(StockCard card); Product getProductByStockCardId(Long stockCardId);  }