### Question:
AgentServiceImpl implements AgentService { @Override @Transactional(readOnly = true) public File savePublication( @NonNull PublicationType publicationType, @NonNull String licenseKey, String codebaseFingerprint, int publicationSize, InputStream inputStream) throws LicenseViolationException, IOException { try (inputStream) { CustomerData customerData = customerService.getCustomerDataByLicenseKey(licenseKey); if (publicationType == PublicationType.CODEBASE) { customerService.assertPublicationSize(customerData, publicationSize); } return doSaveInputStream( publicationType, customerData.getCustomerId(), codebaseFingerprint, inputStream); } }  @Override @Transactional @Restartable GetConfigResponse1 getConfig(GetConfigRequest1 request); @Override @Transactional @Restartable GetConfigResponse2 getConfig(GetConfigRequest2 request); @Override @Transactional(readOnly = true) File savePublication(
      @NonNull PublicationType publicationType,
      @NonNull String licenseKey,
      String codebaseFingerprint,
      int publicationSize,
      InputStream inputStream); @Override File generatePublicationFile(
      PublicationType publicationType, Long customerId, String correlationId); @Override PublicationType getPublicationTypeFromPublicationFile(File publicationFile); @Override String getCorrelationIdFromPublicationFile(File publicationFile); static final String UNKNOWN_ENVIRONMENT; static final Pattern CORRELATION_ID_PATTERN; }### Answer:
@Test public void should_close_inputStream_after_throwing() throws Exception { int publicationSize = 4711; doThrow(new LicenseViolationException("stub")) .when(customerService) .assertPublicationSize(any(CustomerData.class), eq(publicationSize)); val inputStream = mock(InputStream.class); try { service.savePublication( PublicationType.CODEBASE, "key", "fingerprint", publicationSize, inputStream); fail("Expected a LicenseViolationException"); } catch (LicenseViolationException expected) { } finally { verify(inputStream).close(); } }
@Test public void should_close_inputStream_after_not_throwing() throws Exception { val inputStream = mock(InputStream.class); service.savePublication(PublicationType.CODEBASE, "key", "fingerprint", 4711, inputStream); verify(inputStream).close(); }
@Test public void should_save_uploaded_invocations() throws Exception { String contents = "Dummy Invocations Publication"; File resultingFile = service.savePublication( PublicationType.INVOCATIONS, "key", "fingerprint", 1000, new ByteArrayInputStream(contents.getBytes())); assertThat(resultingFile, notNullValue()); assertThat(resultingFile.getName(), startsWith("invocations-")); assertThat(resultingFile.getName(), endsWith(".ser")); assertThat(resultingFile.exists(), is(true)); assertThat(resultingFile.length(), is((long) contents.length())); }
@Test(expected = NullPointerException.class) public void should_reject_null_licenseKey() throws Exception { service.savePublication(PublicationType.CODEBASE, null, "fingerprint", 0, null); }