@Test public void testCreateAttachment() throws IOException { handler.addRequest("POST", "/qcbin/rest/domains/domain/projects/project/defects/1/attachments", 201) .expectHeader("Content-Type", "application/octet-stream") .expectHeader("Slug", "logfile.txt") .content("attachmentServiceTest_attachment.xml"); handler.async(); addEntityListener(new EntityLoaded(handler, new EntityLoaded.Listener() { @Override public void evaluate(Entity entity, EntityListener.Event event) { checkAttachment(entity); Assert.assertEquals(EntityListener.Event.CREATE, event); } })); String name = attachmentService.createAttachment("logfile.txt", new IndicatingInputStream(file, null), file.length(), new EntityRef("defect", 1)); Assert.assertEquals("logfile.txt", name); }
public String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent) { Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/octet-stream"); headers.put("Slug", filename); MyResultInfo result = new MyResultInfo(); if(restService.post(new MyInputData(is, length, headers), result, "{0}s/{1}/attachments", parent.type, parent.id) != HttpStatus.SC_CREATED) { errorService.showException(new RestException(result.getBodyAsString(), result.getLocation())); return null; } else { EntityList list = EntityList.create(result.getBodyAsStream(), true); if(!list.isEmpty()) { Entity entity = list.get(0); entityService.fireEntityLoaded(entity, EntityListener.Event.CREATE); return entity.getPropertyValue("name"); } else { return null; } } }
AttachmentService { public String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent) { Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/octet-stream"); headers.put("Slug", filename); MyResultInfo result = new MyResultInfo(); if(restService.post(new MyInputData(is, length, headers), result, "{0}s/{1}/attachments", parent.type, parent.id) != HttpStatus.SC_CREATED) { errorService.showException(new RestException(result.getBodyAsString(), result.getLocation())); return null; } else { EntityList list = EntityList.create(result.getBodyAsStream(), true); if(!list.isEmpty()) { Entity entity = list.get(0); entityService.fireEntityLoaded(entity, EntityListener.Event.CREATE); return entity.getPropertyValue("name"); } else { return null; } } } }
AttachmentService { public String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent) { Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/octet-stream"); headers.put("Slug", filename); MyResultInfo result = new MyResultInfo(); if(restService.post(new MyInputData(is, length, headers), result, "{0}s/{1}/attachments", parent.type, parent.id) != HttpStatus.SC_CREATED) { errorService.showException(new RestException(result.getBodyAsString(), result.getLocation())); return null; } else { EntityList list = EntityList.create(result.getBodyAsStream(), true); if(!list.isEmpty()) { Entity entity = list.get(0); entityService.fireEntityLoaded(entity, EntityListener.Event.CREATE); return entity.getPropertyValue("name"); } else { return null; } } } AttachmentService(RestService restService, EntityService entityService, ErrorService errorService); }
AttachmentService { public String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent) { Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/octet-stream"); headers.put("Slug", filename); MyResultInfo result = new MyResultInfo(); if(restService.post(new MyInputData(is, length, headers), result, "{0}s/{1}/attachments", parent.type, parent.id) != HttpStatus.SC_CREATED) { errorService.showException(new RestException(result.getBodyAsString(), result.getLocation())); return null; } else { EntityList list = EntityList.create(result.getBodyAsStream(), true); if(!list.isEmpty()) { Entity entity = list.get(0); entityService.fireEntityLoaded(entity, EntityListener.Event.CREATE); return entity.getPropertyValue("name"); } else { return null; } } } AttachmentService(RestService restService, EntityService entityService, ErrorService errorService); Entity getAttachmentEntity(Entity entity); Entity getAttachmentEntity(String name, EntityRef parent); String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent); boolean updateAttachmentContent(String name, EntityRef parent, IndicatingInputStream is, long length, boolean silent); Entity updateAttachmentProperty(String name, EntityRef parent, String propertyName, String propertyValue, boolean silent); void deleteAttachment(String name, EntityRef parent); }
AttachmentService { public String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent) { Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/octet-stream"); headers.put("Slug", filename); MyResultInfo result = new MyResultInfo(); if(restService.post(new MyInputData(is, length, headers), result, "{0}s/{1}/attachments", parent.type, parent.id) != HttpStatus.SC_CREATED) { errorService.showException(new RestException(result.getBodyAsString(), result.getLocation())); return null; } else { EntityList list = EntityList.create(result.getBodyAsStream(), true); if(!list.isEmpty()) { Entity entity = list.get(0); entityService.fireEntityLoaded(entity, EntityListener.Event.CREATE); return entity.getPropertyValue("name"); } else { return null; } } } AttachmentService(RestService restService, EntityService entityService, ErrorService errorService); Entity getAttachmentEntity(Entity entity); Entity getAttachmentEntity(String name, EntityRef parent); String createAttachment(String filename, IndicatingInputStream is, long length, EntityRef parent); boolean updateAttachmentContent(String name, EntityRef parent, IndicatingInputStream is, long length, boolean silent); Entity updateAttachmentProperty(String name, EntityRef parent, String propertyName, String propertyValue, boolean silent); void deleteAttachment(String name, EntityRef parent);  }