@Test public void testCreateTemplateThrowsIOException() throws IOException { List<Attachment> attachmentList = new ArrayList<>(); Attachment json = mock(Attachment.class); Attachment tpl = mock(Attachment.class); attachmentList.add(json); attachmentList.add(tpl); DataHandler jsonDataHandler = mock(DataHandler.class); DataHandler tplDataHandler = mock(DataHandler.class); when(json.getDataHandler()).thenReturn(jsonDataHandler); when(tpl.getDataHandler()).thenReturn(tplDataHandler); when(jsonDataHandler.getName()).thenReturn("test-target.json"); when(tplDataHandler.getName()).thenReturn("test-target.tpl"); when(jsonDataHandler.getInputStream()).thenThrow(new IOException()); when(tplDataHandler.getInputStream()).thenThrow(new IOException()); when(impl.createTemplate(any(InputStream.class), any(InputStream.class), anyString(), any(User.class))).thenReturn(new CreateResourceResponseWrapper(new ConfigTemplate())); Response response = cut.createTemplate(attachmentList, "test-target-name", authenticatedUser); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); }
public Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user) { LOGGER.info("create template for target {} by user {}", targetName, user.getUser().getId()); List<Attachment> filteredAttachments = new ArrayList<>(); for (Attachment attachment : attachments) { if (null != attachment.getDataHandler() && null != attachment.getDataHandler().getName()) { filteredAttachments.add(attachment); } } if (filteredAttachments.size() == CREATE_TEMPLATE_EXPECTED_NUM_OF_ATTACHMENTS) { InputStream metadataInputStream = null; InputStream templateInputStream = null; for (Attachment attachment : filteredAttachments) { final DataHandler handler = attachment.getDataHandler(); try { LOGGER.debug("filename is {}", handler.getName()); if (handler.getName().toLowerCase(Locale.US).endsWith(JSON_FILE_EXTENSION)) { metadataInputStream = attachment.getDataHandler().getInputStream(); } else { templateInputStream = attachment.getDataHandler().getInputStream(); } } catch (final IOException ioe) { LOGGER.error("Create template failed!", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.IO_EXCEPTION, ioe.getMessage())); } } return ResponseBuilder.created(resourceService.createTemplate(metadataInputStream, templateInputStream, targetName, user.getUser())); } else { return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.INVALID_NUMBER_OF_ATTACHMENTS, "Invalid number of attachments! 2 attachments is expected by the service.")); } }
ResourceServiceRestImpl implements ResourceServiceRest { public Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user) { LOGGER.info("create template for target {} by user {}", targetName, user.getUser().getId()); List<Attachment> filteredAttachments = new ArrayList<>(); for (Attachment attachment : attachments) { if (null != attachment.getDataHandler() && null != attachment.getDataHandler().getName()) { filteredAttachments.add(attachment); } } if (filteredAttachments.size() == CREATE_TEMPLATE_EXPECTED_NUM_OF_ATTACHMENTS) { InputStream metadataInputStream = null; InputStream templateInputStream = null; for (Attachment attachment : filteredAttachments) { final DataHandler handler = attachment.getDataHandler(); try { LOGGER.debug("filename is {}", handler.getName()); if (handler.getName().toLowerCase(Locale.US).endsWith(JSON_FILE_EXTENSION)) { metadataInputStream = attachment.getDataHandler().getInputStream(); } else { templateInputStream = attachment.getDataHandler().getInputStream(); } } catch (final IOException ioe) { LOGGER.error("Create template failed!", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.IO_EXCEPTION, ioe.getMessage())); } } return ResponseBuilder.created(resourceService.createTemplate(metadataInputStream, templateInputStream, targetName, user.getUser())); } else { return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.INVALID_NUMBER_OF_ATTACHMENTS, "Invalid number of attachments! 2 attachments is expected by the service.")); } } }
ResourceServiceRestImpl implements ResourceServiceRest { public Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user) { LOGGER.info("create template for target {} by user {}", targetName, user.getUser().getId()); List<Attachment> filteredAttachments = new ArrayList<>(); for (Attachment attachment : attachments) { if (null != attachment.getDataHandler() && null != attachment.getDataHandler().getName()) { filteredAttachments.add(attachment); } } if (filteredAttachments.size() == CREATE_TEMPLATE_EXPECTED_NUM_OF_ATTACHMENTS) { InputStream metadataInputStream = null; InputStream templateInputStream = null; for (Attachment attachment : filteredAttachments) { final DataHandler handler = attachment.getDataHandler(); try { LOGGER.debug("filename is {}", handler.getName()); if (handler.getName().toLowerCase(Locale.US).endsWith(JSON_FILE_EXTENSION)) { metadataInputStream = attachment.getDataHandler().getInputStream(); } else { templateInputStream = attachment.getDataHandler().getInputStream(); } } catch (final IOException ioe) { LOGGER.error("Create template failed!", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.IO_EXCEPTION, ioe.getMessage())); } } return ResponseBuilder.created(resourceService.createTemplate(metadataInputStream, templateInputStream, targetName, user.getUser())); } else { return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.INVALID_NUMBER_OF_ATTACHMENTS, "Invalid number of attachments! 2 attachments is expected by the service.")); } } ResourceServiceRestImpl(final ResourceService resourceService); }
ResourceServiceRestImpl implements ResourceServiceRest { public Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user) { LOGGER.info("create template for target {} by user {}", targetName, user.getUser().getId()); List<Attachment> filteredAttachments = new ArrayList<>(); for (Attachment attachment : attachments) { if (null != attachment.getDataHandler() && null != attachment.getDataHandler().getName()) { filteredAttachments.add(attachment); } } if (filteredAttachments.size() == CREATE_TEMPLATE_EXPECTED_NUM_OF_ATTACHMENTS) { InputStream metadataInputStream = null; InputStream templateInputStream = null; for (Attachment attachment : filteredAttachments) { final DataHandler handler = attachment.getDataHandler(); try { LOGGER.debug("filename is {}", handler.getName()); if (handler.getName().toLowerCase(Locale.US).endsWith(JSON_FILE_EXTENSION)) { metadataInputStream = attachment.getDataHandler().getInputStream(); } else { templateInputStream = attachment.getDataHandler().getInputStream(); } } catch (final IOException ioe) { LOGGER.error("Create template failed!", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.IO_EXCEPTION, ioe.getMessage())); } } return ResponseBuilder.created(resourceService.createTemplate(metadataInputStream, templateInputStream, targetName, user.getUser())); } else { return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.INVALID_NUMBER_OF_ATTACHMENTS, "Invalid number of attachments! 2 attachments is expected by the service.")); } } ResourceServiceRestImpl(final ResourceService resourceService); Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user); @Override Response getResourceAttrData(); @Override Response getResourceTopology(); @Override Response getApplicationResourceNames(final String groupName, final String appName); @Override Response getAppTemplate(final String groupName, final String appName, final String templateName); @Override Response checkFileExists(final String groupName, final String jvmName, final String webappName, final String webserverName, final String fileName); @Override Response createResource(final String deployFilename, final CreateResourceParam createResourceParam,
                                   final List<Attachment> attachments); void setMessageContext(MessageContext messageContext); @Override Response uploadExternalProperties(AuthenticatedUser user); @Override // TODO: Re validation, maybe we can use CXF bean validation ? Response deleteResource(final String templateName, final ResourceHierarchyParam resourceHierarchyParam,
                                   final AuthenticatedUser aUser); @Override Response deleteResources(final String[] templateNameArray, ResourceHierarchyParam
            resourceHierarchyParam, AuthenticatedUser user); @Override Response getResourceContent(final String resourceName, final ResourceHierarchyParam param); @Override Response updateResourceContent(String resourceName, ResourceHierarchyParam
            resourceHierarchyParam, String templateContent); @Override Response updateResourceMetaData(String resourceName, ResourceHierarchyParam resourceHierarchyParam, String metaData); @Override Response previewResourceContent(final String resourceName, final ResourceHierarchyParam resourceHierarchyParam, String content); @Override Response getResourcesFileNames(ResourceHierarchyParam resourceHierarchyParam); @Override Response getExternalProperties(); @Override Response getExternalPropertiesView(); @Override Response getExternalPropertiesDownload(); @Override void afterPropertiesSet(); }
ResourceServiceRestImpl implements ResourceServiceRest { public Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user) { LOGGER.info("create template for target {} by user {}", targetName, user.getUser().getId()); List<Attachment> filteredAttachments = new ArrayList<>(); for (Attachment attachment : attachments) { if (null != attachment.getDataHandler() && null != attachment.getDataHandler().getName()) { filteredAttachments.add(attachment); } } if (filteredAttachments.size() == CREATE_TEMPLATE_EXPECTED_NUM_OF_ATTACHMENTS) { InputStream metadataInputStream = null; InputStream templateInputStream = null; for (Attachment attachment : filteredAttachments) { final DataHandler handler = attachment.getDataHandler(); try { LOGGER.debug("filename is {}", handler.getName()); if (handler.getName().toLowerCase(Locale.US).endsWith(JSON_FILE_EXTENSION)) { metadataInputStream = attachment.getDataHandler().getInputStream(); } else { templateInputStream = attachment.getDataHandler().getInputStream(); } } catch (final IOException ioe) { LOGGER.error("Create template failed!", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.IO_EXCEPTION, ioe.getMessage())); } } return ResponseBuilder.created(resourceService.createTemplate(metadataInputStream, templateInputStream, targetName, user.getUser())); } else { return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.INVALID_NUMBER_OF_ATTACHMENTS, "Invalid number of attachments! 2 attachments is expected by the service.")); } } ResourceServiceRestImpl(final ResourceService resourceService); Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user); @Override Response getResourceAttrData(); @Override Response getResourceTopology(); @Override Response getApplicationResourceNames(final String groupName, final String appName); @Override Response getAppTemplate(final String groupName, final String appName, final String templateName); @Override Response checkFileExists(final String groupName, final String jvmName, final String webappName, final String webserverName, final String fileName); @Override Response createResource(final String deployFilename, final CreateResourceParam createResourceParam,
                                   final List<Attachment> attachments); void setMessageContext(MessageContext messageContext); @Override Response uploadExternalProperties(AuthenticatedUser user); @Override // TODO: Re validation, maybe we can use CXF bean validation ? Response deleteResource(final String templateName, final ResourceHierarchyParam resourceHierarchyParam,
                                   final AuthenticatedUser aUser); @Override Response deleteResources(final String[] templateNameArray, ResourceHierarchyParam
            resourceHierarchyParam, AuthenticatedUser user); @Override Response getResourceContent(final String resourceName, final ResourceHierarchyParam param); @Override Response updateResourceContent(String resourceName, ResourceHierarchyParam
            resourceHierarchyParam, String templateContent); @Override Response updateResourceMetaData(String resourceName, ResourceHierarchyParam resourceHierarchyParam, String metaData); @Override Response previewResourceContent(final String resourceName, final ResourceHierarchyParam resourceHierarchyParam, String content); @Override Response getResourcesFileNames(ResourceHierarchyParam resourceHierarchyParam); @Override Response getExternalProperties(); @Override Response getExternalPropertiesView(); @Override Response getExternalPropertiesDownload(); @Override void afterPropertiesSet(); static final String UNEXPECTED_CONTENT_TYPE_ERROR_MSG; static final String TPL_FILE_EXTENSION; static final String EXT_PROPERTIES_RESOURCE_NAME; static final String EXT_PROPERTIES_RESOURCE_META_DATA; static final int CREATE_RESOURCE_ATTACHMENT_SIZE; }