@Test public void shouldThrowErrorWhenInsertingInvalidProductForAProgram() { Program program = make(a(defaultProgram)); when(programRepository.getIdByCode(program.getCode())).thenReturn(1L); ProgramProduct programProduct = new ProgramProduct(program, new Product(), 10, true); expectedEx.expect(Matchers.dataExceptionMatcher("product.code.invalid")); programProductRepository.save(programProduct); }
@Test public void shouldThrowErrorWhenInsertingProductForInvalidProgram() { Product product = make(a(defaultProduct)); Program program = make(a(defaultProgram)); ProgramProduct programProduct = new ProgramProduct(program, product, 10, true); when(programRepository.getIdByCode(programProduct.getProgram().getCode())) .thenThrow(new DataException("exception")); expectedEx.expect(DataException.class); expectedEx.expectMessage("exception"); programProductRepository.save(programProduct); }
@Test public void shouldUpdateProgramProductIfExist() throws Exception { Long programId = 88L; Long productId = 99L; programProduct.setId(1L); when(programRepository.getIdByCode(anyString())).thenReturn(programId); when(productRepository.getIdByCode(anyString())).thenReturn(productId); programProductRepository.save(programProduct); assertThat(programProduct.getProgram().getId(), is(88L)); assertThat(programProduct.getProduct().getId(), is(99L)); verify(programProductMapper).update(programProduct); }
@Test public void shouldGetProgramProductByProgramAndProductCodes() throws Exception { ProgramProduct programProduct = make(a(ProgramProductBuilder.defaultProgramProduct)); final Long programId = 123L; when(programRepository.getIdByCode(PROGRAM_CODE)).thenReturn(programId); final Long productId = 12L; when(productRepository.getIdByCode(PRODUCT_CODE)).thenReturn(productId); ProgramProduct expectedProgramProduct = new ProgramProduct(); when(programProductMapper.getByProgramAndProductId(programId, productId)) .thenReturn(expectedProgramProduct); ProgramProduct result = programProductRepository.getByProgramAndProductCode(programProduct); verify(programRepository).getIdByCode(PROGRAM_CODE); verify(productRepository).getIdByCode(PRODUCT_CODE); verify(programProductMapper).getByProgramAndProductId(programId, productId); assertThat(result, is(expectedProgramProduct)); }