CatalogOutputAdapter { @SuppressWarnings("unused") public NitfSegmentsFlow getNitfSegmentsFlow(ResourceResponse resourceResponse) throws NitfFormatException, IOException { notNull(resourceResponse, "resourceResponse must be non-null"); notNull(resourceResponse.getResource(), "resourceResponse resource must be non-null"); NitfSegmentsFlow nitfSegmentsFlow = null; try (final InputStream inputStream = resourceResponse.getResource().getInputStream()) { lock.acquire(); try { nitfSegmentsFlow = getNitfSegmentsFlow(inputStream); } finally { lock.release(); } } catch (InterruptedException e) { LOGGER.debug("Interrupt received while doing image processing.", e); Thread.currentThread().interrupt(); } return nitfSegmentsFlow; } CatalogOutputAdapter(Semaphore lock); @SuppressWarnings("WeakerAccess") BufferedImage getImage(ResourceResponse resourceResponse); @SuppressWarnings("WeakerAccess") BinaryContent getBinaryContent(BufferedImage image); @SuppressWarnings("unused") NitfSegmentsFlow getNitfSegmentsFlow(ResourceResponse resourceResponse); @SuppressWarnings("unused") BinaryContent getNitfBinaryContent(
      BufferedImage chip, NitfSegmentsFlow nitfSegmentsFlow, int sourceX, int sourceY); @SuppressWarnings("unused") void wrapException(Exception exception);  }
@Test public void testGetNitfSegmentsFlow() throws IOException, NitfFormatException, InterruptedException { Resource resource = mock(Resource.class); when(resource.getInputStream()).thenReturn(getInputStream("/i_3001a.ntf")); ResourceResponse resourceResponse = mock(ResourceResponse.class); when(resourceResponse.getResource()).thenReturn(resource); NitfSegmentsFlow nitfSegmentsFlow = catalogOutputAdapter.getNitfSegmentsFlow(resourceResponse); assertThat(nitfSegmentsFlow, notNullValue()); verify(lock).acquire(); verify(lock).release(); }
@Test public void testGetNitfSegmentsFlowTFBOSThrows() throws IOException, NitfFormatException { TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class); doThrow(IOException.class).when(tfbos).write(anyObject(), anyInt(), anyInt()); catalogOutputAdapter = new CatalogOutputAdapter(new Semaphore(3, true)) { @Override protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() { return tfbos; } }; try { catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] {(byte) 0})); fail("expected an exception, shouldn't reach this line"); } catch (IOException e) { assertThat(e, notNullValue()); } verify(tfbos).close(); }
@SuppressWarnings("unchecked") @Test public void testGetNitfSegmentsFlowTFBOSThrowsDuringRead() throws IOException, NitfFormatException { InputStream inputStream = mock(InputStream.class); when(inputStream.read()).thenThrow(IOException.class); when(inputStream.read(anyObject())).thenThrow(IOException.class); when(inputStream.read(anyObject(), anyInt(), anyInt())).thenThrow(IOException.class); ByteSource byteSource = mock(ByteSource.class); when(byteSource.openBufferedStream()).thenReturn(inputStream); TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class); when(tfbos.asByteSource()).thenReturn(byteSource); catalogOutputAdapter = new CatalogOutputAdapter(new Semaphore(3, true)) { @Override protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() { return tfbos; } }; try { catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] {(byte) 0})); fail("expected an exception, shouldn't reach this line"); } catch (IOException | NitfFormatException e) { assertThat(e, notNullValue()); } verify(tfbos).close(); verify(inputStream).close(); }
@SuppressWarnings("unchecked") @Test public void testGetNitfSegmentsFlowTFBOSAsBytesourceThrows() throws IOException, NitfFormatException { ByteSource byteSource = mock(ByteSource.class); when(byteSource.openBufferedStream()).thenThrow(IOException.class); TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class); when(tfbos.asByteSource()).thenReturn(byteSource); catalogOutputAdapter = new CatalogOutputAdapter(new Semaphore(3, true)) { @Override protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() { return tfbos; } }; try { catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] {(byte) 0})); fail("expected an exception, shouldn't reach this line"); } catch (IOException | NitfFormatException e) { assertThat(e, notNullValue()); } verify(tfbos).close(); }