/** ******************************************************************************** */ private void assertContentItem(String data, String mimeTypeRawData, String expectedFileSuffix) throws Exception { // Simulates what ContentFrameworkImpl would do String uuid = UUID.randomUUID().toString().replaceAll("-", ""); ContentItem contentItem = new IncomingContentItem(uuid, IOUtils.toInputStream(data), mimeTypeRawData); CreateRequest createRequest = new CreateRequestImpl(contentItem, null); CreateResponse createResponse = provider.create(createRequest); ContentItem createdContentItem = createResponse.getCreatedContentItem(); assertNotNull(createdContentItem); String id = createdContentItem.getId(); assertNotNull(id); assertThat(id, equalTo(uuid)); String contentUri = createdContentItem.getUri(); LOGGER.debug("contentUri = {}", contentUri); assertNotNull(contentUri); String expectedContentUri = FileSystemProvider.CONTENT_URI_PREFIX + uuid; assertThat(contentUri, equalTo(expectedContentUri)); File file = createdContentItem.getFile(); assertNotNull(file); assertTrue(file.exists()); assertTrue(createdContentItem.getSize() > 0); assertEquals(mimeTypeRawData, createdContentItem.getMimeTypeRawData()); assertEquals(data, IOUtils.toString(createdContentItem.getInputStream())); }
protected ContentItem getMockGoodContentItem( String content, String fileName, String contentId, Long size, String mimeType) throws IOException, MimeTypeParseException { ContentItem item = mock(ContentItem.class); when(item.getInputStream()).thenReturn(new ByteArrayInputStream(content.getBytes())); when(item.getFilename()).thenReturn(fileName); when(item.getId()).thenReturn(contentId); if (size != null) { when(item.getSize()).thenReturn(size); } else { when(item.getSize()).thenThrow(new IOException("IOException")); } when(item.getMimeTypeRawData()).thenReturn(mimeType); if (mimeType != null) { when(item.getMimeType()).thenReturn(new MimeType(mimeType)); } return item; }
@Test public void testRead() throws Exception { CreateResponse createResponse = storeContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME); String id = createResponse.getCreatedContentItem().getId(); ReadRequest readRequest = new ReadRequestImpl(id, null); ReadResponse readResponse = provider.read(readRequest); ContentItem item = readResponse.getContentItem(); LOGGER.debug("Item retrieved: {}", item); assertEquals(id, item.getId()); assertEquals(NITF_MIME_TYPE, item.getMimeTypeRawData()); String expectedFilePath = BASE_DIR + File.separator + id + File.separator + item.getFilename(); assertThat(item.getFile().getAbsolutePath(), endsWith(expectedFilePath)); assertTrue(item.getSize() > 0); assertTrue(item.getFile().exists()); }
@Test public void testUpdate() throws Exception { CreateResponse createResponse = storeContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME); String id = createResponse.getCreatedContentItem().getId(); ContentItem updateItem = new IncomingContentItem(id, IOUtils.toInputStream("Updated NITF"), NITF_MIME_TYPE); UpdateRequest updateRequest = new UpdateRequestImpl(updateItem); UpdateResponse updateResponse = provider.update(updateRequest); ContentItem item = updateResponse.getUpdatedContentItem(); LOGGER.debug("Item retrieved: {}", item); assertEquals(id, item.getId()); assertEquals(NITF_MIME_TYPE, item.getMimeTypeRawData()); String expectedFilePath = BASE_DIR + File.separator + id + File.separator + item.getFilename(); assertThat(item.getFile().getAbsolutePath(), endsWith(expectedFilePath)); assertTrue(item.getSize() > 0); assertTrue(item.getFile().exists()); String updatedFileContents = getFileContentsAsString(item.getFile().getAbsolutePath()); assertEquals("Updated NITF", updatedFileContents); }