/*
 * This program was produced for the U.S. Agency for International Development. It was prepared by the USAID | DELIVER PROJECT, Task Order 4. It is part of a project which utilizes code originally licensed under the terms of the Mozilla Public License (MPL) v2 and therefore is licensed under MPL v2 or later.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License as published by the Mozilla Foundation, either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License along with this program. If not, see http://www.mozilla.org/MPL/
 */

package org.openlmis.vaccine.service;

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static com.natpryce.makeiteasy.MakeItEasy.a;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

import org.mockito.runners.MockitoJUnitRunner;
import org.openlmis.core.builder.ProductBuilder;
import org.openlmis.core.builder.ProgramProductBuilder;
import org.openlmis.core.domain.Product;
import org.openlmis.core.domain.ProgramProduct;
import org.openlmis.core.repository.ProgramProductRepository;
import org.openlmis.db.categories.UnitTests;
import org.openlmis.vaccine.domain.VaccineProductDose;
import org.openlmis.vaccine.dto.ProductDoseDTO;
import org.openlmis.vaccine.dto.VaccineServiceConfigDTO;
import org.openlmis.vaccine.repository.ProductDoseRepository;

import java.util.ArrayList;
import java.util.List;


@Category(UnitTests.class)
@RunWith(MockitoJUnitRunner.class)
public class VaccineProductDoseServiceTest {

  @Mock
  private ProductDoseRepository repository;

  @Mock
  private ProgramProductRepository programProductRepository;

  @InjectMocks
  private VaccineProductDoseService service;

  @Test
  public void shouldGetProductDoseForProgram() throws Exception {
    when(programProductRepository.getActiveByProgram(2L)).thenReturn(new ArrayList<ProgramProduct>());
    when(repository.getAllDoses()).thenReturn(null);

    VaccineServiceConfigDTO dto  = service.getProductDoseForProgram(2L);

    verify(programProductRepository).getActiveByProgram(2L);
    verify(repository, never()).getDosesForProduct(anyLong(), anyLong());
    verify(repository).getAllDoses();
  }

  @Test
  public void shouldGetProductDoseForProgramWithProducts() throws Exception {
    Product product = make(a(ProductBuilder.defaultProduct));
    ProgramProduct programProduct = make(a(ProgramProductBuilder.defaultProgramProduct));
    programProduct.setProduct(product);
    when(programProductRepository.getActiveByProgram(2L)).thenReturn(asList(programProduct));
    VaccineProductDose vpd = new VaccineProductDose();
    when(repository.getDosesForProduct(2L, product.getId())).thenReturn(asList(vpd));
    when(repository.getAllDoses()).thenReturn(null);

    VaccineServiceConfigDTO dto  = service.getProductDoseForProgram(2L);

    verify(programProductRepository).getActiveByProgram(2L);
    verify(repository, atLeastOnce()).getDosesForProduct(2L, product.getId());
    verify(repository).getAllDoses();

    assertThat(dto.getProtocols().get(0).getProductName(), is(product.getName()));
  }

  @Test
  public void shouldGetForProgram() throws Exception {
    service.getForProgram(2L);
    verify(repository).getProgramProductDoses(2L);
  }

  @Test
  public void shouldSave() throws Exception {
    List<ProductDoseDTO> protocols = new ArrayList<>();
    ProductDoseDTO dto = new ProductDoseDTO();
    dto.setDoses(new ArrayList<VaccineProductDose>());
    VaccineProductDose dose = new VaccineProductDose();
    dose.setProgramId(1L);
    dto.getDoses().add(dose);

    protocols.add(dto);

    doNothing().when(repository).deleteAllByProgram(1L);
    service.save(protocols);
    verify(repository).deleteAllByProgram(1L);
    verify(repository).insert(any(VaccineProductDose.class));
  }
}
@@ -1122,326 +1122,8 @@
 ck;%0A
-%0Aimport static com.natpryce.makeiteasy.MakeItEasy.a;%0Aimport static com.natpryce.makeiteasy.MakeItEasy.make;%0Aimport static java.util.Arrays.asList;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.*;%0A%0A
 impo
@@ -1741,16 +1741,333 @@
 .List;%0A%0A
+import static com.natpryce.makeiteasy.MakeItEasy.a;%0Aimport static com.natpryce.makeiteasy.MakeItEasy.make;%0Aimport static java.util.Arrays.asList;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.*;%0A%0A
 %0A@Catego
@@ -3727,16 +3727,23 @@
 duct.get
+Primary
 Name()))

Fix unit test for product.name to product.primaryName change