public String storeDocument(String documentPayload, String marshallingType) { logger.debug("About to unmarshal document payload '{}' with marshaling type {}", documentPayload, marshallingType); DocumentInstance documentInstance = marshallerHelper.unmarshal(documentPayload, marshallingType, DocumentInstance.class); logger.debug("Document created from payload {}", documentInstance); Document document = documentStorageService.buildDocument(documentInstance.getName(), documentInstance.getSize(), documentInstance.getLastModified(), new HashMap<String, String>()); logger.debug("Document created by the service {}", document); documentStorageService.saveDocument(document, documentInstance.getContent()); logger.debug("Document {} stored successfully", document); return document.getIdentifier(); }
public Builder id(String id) { documentInstance.setIdentifier(id); return this; }
public Builder content(byte[] content) { documentInstance.setContent(content); return this; } }
response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + documentInstance.getName() + "\""); response.getOutputStream().write(documentInstance.getContent()); } else { logger.error(MessageFormat.format(INVALID_DOCUMENT,
@Test public void testUpdateDocument() { String documentId = documentClient.createDocument(document); assertNotNull(documentId); DocumentInstance fromServer = documentClient.getDocument(documentId); assertEquals(documentId, fromServer.getIdentifier()); assertDocumentInstances(document, fromServer, true); String udpatedDoc = "here comes the update"; byte[] updateDocBytes = udpatedDoc.getBytes(); fromServer.setContent(updateDocBytes); fromServer.setSize(updateDocBytes.length); fromServer.setLastModified(new Date()); documentClient.updateDocument(fromServer); DocumentInstance updatedFromServer = documentClient.getDocument(documentId); assertEquals(documentId, updatedFromServer.getIdentifier()); assertDocumentInstances(fromServer, updatedFromServer, true); }
DocumentInstance documentInstance = DocumentInstance.builder().name("test file.txt").size(50).content("test content".getBytes()).lastModified(new Date()).build(); String documentEntity = marshaller.marshall(documentInstance); DocumentInstance doc = docs.get(0); assertNotNull(doc); assertEquals(documentInstance.getName(), doc.getName()); assertEquals(documentId, doc.getIdentifier()); assertTrue(contentDisposition.contains(documentInstance.getName()));
@Override public void updateDocument(DocumentInstance documentInstance) { if( config.isRest() ) { Map<String, Object> valuesMap = new HashMap<String, Object>(); valuesMap.put(DOCUMENT_ID, documentInstance.getIdentifier()); makeHttpPutRequestAndCreateCustomResponse( build(loadBalancer.getUrl(), DOCUMENT_URI + "/" + DOCUMENT_INSTANCE_PUT_URI, valuesMap), documentInstance, Object.class, new HashMap<String, String>()); } else { CommandScript script = new CommandScript( Collections.singletonList( (KieServerCommand) new DescriptorCommand("DocumentService", "updateDocument", serialize(documentInstance), marshaller.getFormat().getType(), new Object[]{documentInstance.getIdentifier()})) ); ServiceResponse<String> response = (ServiceResponse<String>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM" ).getResponses().get(0); throwExceptionOnFailure(response); } }
@Test public void testCreateEmptyDocument() { content = ""; contentBytes = content.getBytes(); document = DocumentInstance.builder() .name("first document") .size(contentBytes.length) .lastModified(new Date()) .content(contentBytes) .build(); String documentId = documentClient.createDocument(document); assertNotNull(documentId); DocumentInstance fromServer = documentClient.getDocument(documentId); assertEquals(documentId, fromServer.getIdentifier()); assertDocumentInstances(document, fromServer, true); }
public void updateDocument(String documentId, String documentPayload, String marshallingType) { logger.debug("About to unmarshal document payload '{}' with marshaling type {}", documentPayload, marshallingType); DocumentInstance documentInstance = marshallerHelper.unmarshal(documentPayload, marshallingType, DocumentInstance.class); logger.debug("Document created from payload {}", documentInstance); Document document = documentStorageService.getDocument(documentId); logger.debug("Document found {}", documentInstance != null); if (document == null) { throw new KieServerRuntimeException("No document found with id " + documentId); } documentStorageService.saveDocument(document, documentInstance.getContent()); logger.debug("Document {} updated successfully", document); }
DocumentInstance documentResponse = new DocumentInstance(); documentResponse.setIdentifier("1234"); stubFor(get(urlMatching("/documents/1234")) .willReturn(aResponse()
@Before public void createData() { KieServerUtil.deleteDocumentStorageFolder(); content = "just text content"; contentBytes = content.getBytes(); document = DocumentInstance.builder() .name("first document") .size(contentBytes.length) .lastModified(new Date()) .content(contentBytes) .build(); }
public Builder lastModified(Date lastModified) { documentInstance.setLastModified(lastModified); return this; }
public Builder size(long size) { documentInstance.setSize(size); return this; }
response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + documentInstance.getName() + "\""); response.getOutputStream().write(documentInstance.getContent()); } else { logger.error(MessageFormat.format(INVALID_DOCUMENT,
@Test public void testDocumentServiceCamelProducer() throws Exception { MockEndpoint mockEndpoint = getMockEndpoint("mock:result"); mockEndpoint.expectedMessageCount(1); Map<String, Object> headers = new HashMap<>(); headers.put(KIE_CLIENT, "document"); headers.put(KIE_OPERATION, "getDocument"); headers.put(asCamelKieName("identifier"), "1234"); template.sendBodyAndHeaders("direct:start", null, headers); assertMockEndpointsSatisfied(); DocumentInstance documentInstance = getResultMessage(mockEndpoint.getExchanges().get(0)).getBody(DocumentInstance.class); assertEquals(documentInstance.getIdentifier(), "1234"); }
protected DocumentInstance convertDocument(Document document, boolean withContent) { if (document == null) { return null; } Builder documentBuilder = DocumentInstance.builder(); documentBuilder .id(document.getIdentifier()) .name(document.getName()) .link(document.getLink()) .size(document.getSize()) .lastModified(document.getLastModified()); if (withContent) { documentBuilder.content(document.getContent()); } DocumentInstance documentInstance = documentBuilder.build(); return documentInstance; } }
private void assertDocumentInstances(DocumentInstance expected, DocumentInstance actual, boolean assertContent) { assertNotNull(actual); assertNotNull(actual.getIdentifier()); assertNotNull(actual.getName()); assertNotNull(actual.getLastModified()); assertNotNull(actual.getSize()); assertEquals(expected.getName(), actual.getName()); assertEquals(expected.getSize(), actual.getSize()); if (assertContent) { assertEquals(new String(expected.getContent()), new String(actual.getContent())); } }
@Test public void testRetrieveValidDocument() throws Exception { when(kieServerIntegration.getServerClient(anyString())).thenReturn(kieServicesClient); when(kieServicesClient.getServicesClient(any())).thenReturn(documentServicesClient); when(documentServicesClient.getDocument(anyString())).thenReturn(documentInstance); when(documentInstance.getName()).thenReturn("testdoc.properties"); when(documentInstance.getContent()).thenReturn("sampleContent".getBytes()); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); when(request.getParameter("templateid")).thenReturn("kie-server-id"); when(request.getParameter("docid")).thenReturn("AAAA-BBBB-CCCC-DDDD"); ProcessDocumentServletTest.StubServletOutputStream stubServletOutputStream = new ProcessDocumentServletTest.StubServletOutputStream(); when(response.getOutputStream()).thenReturn(stubServletOutputStream); ProcessDocumentServlet processDocServlet = new ProcessDocumentServlet(); processDocServlet.setKieServerIntegration(kieServerIntegration); processDocServlet.doGet(request, response); assertNotNull(response.getOutputStream()); assertEquals("sampleContent", new String(((ProcessDocumentServletTest.StubServletOutputStream) response.getOutputStream()).getContent())); }
@Test public void testListDocuments() { List<DocumentInstance> docs = documentClient.listDocuments(0, 10); assertNotNull(docs); assertEquals(0, docs.size()); String documentId = documentClient.createDocument(document); assertNotNull(documentId); docs = documentClient.listDocuments(0, 10); assertNotNull(docs); assertEquals(1, docs.size()); DocumentInstance fromServer = docs.get(0); assertEquals(documentId, fromServer.getIdentifier()); assertDocumentInstances(document, fromServer, false); }
@Test(expected = KieServicesException.class) public void testUpdateNotExistingDocument() { document.setIdentifier("not-existing"); documentClient.updateDocument(document); }