@Before
  public void setUp() throws Exception {
    Product product = make(a(ProductBuilder.defaultProduct));
    productMapper.insert(product);

    Program program = make(a(ProgramBuilder.defaultProgram));
    programMapper.insert(program);

    ProductCategory category = new ProductCategory("C1", "Category 1", 1);
    productCategoryMapper.insert(category);

    ProgramProduct programProduct =
        new ProgramProduct(program, product, 30, true, new Money("12.5"));
    programProduct.setProductCategory(category);
    programProductMapper.insert(programProduct);

    Facility facility = make(a(FacilityBuilder.defaultFacility));
    facilityMapper.insert(facility);

    ProcessingSchedule processingSchedule =
        make(a(ProcessingScheduleBuilder.defaultProcessingSchedule));
    processingScheduleMapper.insert(processingSchedule);

    ProcessingPeriod processingPeriod =
        make(a(defaultProcessingPeriod, with(scheduleId, processingSchedule.getId())));
    processingPeriodMapper.insert(processingPeriod);

    requisition = new Rnr(facility, new Program(HIV), processingPeriod, false, MODIFIED_BY, 1L);
    requisition.setStatus(INITIATED);
    requisitionMapper.insert(requisition);

    user = new User();
    user.setId(MODIFIED_BY);
  }
  @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 shouldInsertProgramForAProduct() {
    Program program = new Program();
    program.setCode("P1");
    Product product = new Product();
    product.setCode("P2");
    ProgramProduct programProduct = new ProgramProduct(program, product, 10, true);
    programProduct.setModifiedDate(new Date());

    when(programProductMapper.getByProgramAndProductId(anyLong(), anyLong())).thenReturn(null);

    programProductRepository.save(programProduct);
    verify(programProductMapper).insert(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);
  }
 private FacilityProgramProduct getAllocationProduct(
     ProgramProduct programProduct, Long facilityId) {
   return new FacilityProgramProduct(
       programProduct,
       facilityId,
       repository.getOverriddenIsa(programProduct.getId(), facilityId));
 }
  @Before
  public void setUp() throws Exception {
    programProductRepository =
        new ProgramProductRepository(
            programRepository, programProductMapper, productRepository, programProductPriceMapper);
    programProduct = make(a(ProgramProductBuilder.defaultProgramProduct));
    programProduct.setModifiedDate(new Date());

    when(productRepository.getIdByCode("productCode")).thenReturn(1L);

    when(programProductMapper.getByProgramAndProductId(anyLong(), anyLong()))
        .thenReturn(programProduct);
  }
  @Test
  public void shouldCreateProgramProductDTOFromProgramProduct() {

    ProgramProduct programProduct = make(a(defaultProgramProduct));

    programProduct.getProduct().setCategory(new ProductCategory("P1", "Product Name", 1));

    ProgramProductDTO programProductDTO = new ProgramProductDTO(programProduct);

    assertThat(programProductDTO.getProductCode(), is(programProduct.getProduct().getCode()));
    assertThat(
        programProductDTO.getProductName(), is(programProduct.getProduct().getPrimaryName()));
    assertThat(programProductDTO.getProgramCode(), is(programProduct.getProgram().getCode()));
    assertThat(programProductDTO.getProgramName(), is(programProduct.getProgram().getName()));
    assertThat(
        programProductDTO.getCategory(), is(programProduct.getProduct().getCategory().getName()));
    assertThat(
        programProductDTO.getDescription(), is(programProduct.getProduct().getDescription()));
    assertThat(
        programProductDTO.getUnit(), is(programProduct.getProduct().getDosesPerDispensingUnit()));
  }