### Question:
SampleService extends BaseProjectRestService<Sample> { public void updateAvailableForGridMaking(@NonNull final Long sampleId, @NonNull final Boolean availabilityForGridMaking) { final Sample sample = repository.findOne(sampleId); if (sample == null) { LOGGER.info("[updateAvailableForGridMaking] Sample not found for ID: " + sampleId); throw new ResourceNotFoundException("Sample not found for ID: " + sampleId); } if (sample.getAvailableForGridMaking() == availabilityForGridMaking) { return; } sample.setAvailableForGridMaking(availabilityForGridMaking); repository.save(sample); } @Autowired  SampleService(final SampleRepository sampleRepository,
                  final ProjectRepository projectRepository,
                  @NotNull final ProteinRepository proteinRepository,
                  @NotNull final LigandRepository ligandRepository
    ); @NonNull Collection<Project> getItemProjects(@NonNull final String slug); @Override Collection<Project> getItemProjects(@NonNull final Long id); @Transactional @NonNull Sample createItem(@NonNull final Sample input, @NonNull final Long projectId); @Transactional @NonNull Sample createItem(@NonNull final Sample input,
                             @NonNull @ApiIgnore final String projectSlug); Collection<Sample> findSamplesAvailableForGridMaking(@NonNull final Long projectId); Collection<Sample> findSamplesAvailableForGridMaking(@NonNull final String projectSlug); @Transactional @NonNull @Override Sample updateItem(@NonNull final Sample input); void updateAvailableForGridMaking(@NonNull final Long sampleId, @NonNull final Boolean availabilityForGridMaking);  }### Answer:
@Test public void shouldThrowErrorWhenSampleIsNotAvailable() { final Long INVALID_SAMPLE_ID = -10L; given(sampleRepository.findOne(INVALID_SAMPLE_ID)).willReturn(null); try { tested.updateAvailableForGridMaking(INVALID_SAMPLE_ID, false); fail("It failed to throw exception when sample is not available."); } catch (ResourceNotFoundException e) { } }
@Test public void shouldMakeSampleUnavailableForGridMaking() { final Long SAMPLE_ID = 1L; final Sample sample = Sample.builder() .id(SAMPLE_ID) .availableForGridMaking(true) .build(); given(sampleRepository.findOne(sample.getId())).willReturn(sample); tested.updateAvailableForGridMaking(sample.getId(), false); final ArgumentCaptor<Sample> savedSample = ArgumentCaptor.forClass(Sample.class); then(sampleRepository).should(times(1)) .save(savedSample.capture()); assertEquals(SAMPLE_ID, savedSample.getValue() .getId()); assertEquals(false, savedSample.getValue() .getAvailableForGridMaking()); }
@Test public void shouldMakeSampleAvailableForGridMaking() { final Long SAMPLE_ID = 1L; final Sample sample = Sample.builder() .id(SAMPLE_ID) .availableForGridMaking(false) .build(); given(sampleRepository.findOne(sample.getId())).willReturn(sample); tested.updateAvailableForGridMaking(sample.getId(), true); final ArgumentCaptor<Sample> savedSample = ArgumentCaptor.forClass(Sample.class); then(sampleRepository).should(times(1)) .save(savedSample.capture()); assertEquals(SAMPLE_ID, savedSample.getValue() .getId()); assertEquals(true, savedSample.getValue() .getAvailableForGridMaking()); }
@Test public void shouldLeaveSampleAvailableForGridMakingAsItWasBefore() { final long SAMPLE_ID = 1L; final Sample sample = Sample.builder() .id(SAMPLE_ID) .availableForGridMaking(true) .build(); given(sampleRepository.findOne(sample.getId())).willReturn(sample); tested.updateAvailableForGridMaking(sample.getId(), true); then(sampleRepository).should(times(0)) .save(any(Sample.class)); }