@Test (expected = WebServerServiceException.class) public void testGenerateAndDeployFailsToTokenizeMetaData() throws IOException { User mockUser = mock(User.class); when(mockUser.getId()).thenReturn("test-ws-user"); WebServer mockWS = mock(WebServer.class); when(mockWS.getId()).thenReturn(new Identifier<WebServer>(1111L)); when(mockWS.getHost()).thenReturn("test-ws-host"); when(Config.mockWebServerPersistenceService.findWebServerByName(eq("test-web-server"))).thenReturn(mockWS); when(Config.mockResourceService.getResourceContent(any(ResourceIdentifier.class))).thenReturn(new ResourceContent("{\"fake\" : \"meta data\"}", "test resource ws content")); when(Config.mockResourceService.getTokenizedMetaData(eq("test-ws-resource.txt"),eq(mockWS), eq("{\"fake\" : \"meta data\"}"))).thenThrow(new IOException("FAIL THIS TEST")); wsService.generateAndDeployFile("test-web-server","test-ws-resource.txt", mockUser); verify(Config.mockBinaryDistributionLockManager).writeLock(anyString()); verify(Config.mockBinaryDistributionLockManager).writeUnlock(anyString()); verify(Config.mockResourceService, never()).validateSingleResourceForGeneration(any(ResourceIdentifier.class)); verify(Config.mockResourceService, never()).generateAndDeployFile(any(ResourceIdentifier.class), eq("test-web-server"), eq("test-ws-resource.txt"), eq("test-ws-host")); }
@Override public WebServer generateAndDeployFile(String webServerName, String fileName, User user) { WebServer webServer = getWebServer(webServerName); binaryDistributionLockManager.writeLock(webServerName + "-" + webServer.getId().getId().toString()); ResourceIdentifier resourceIdentifier = new ResourceIdentifier.Builder() .setWebServerName(webServerName) .setResourceName(fileName) .build(); try { checkWebServerStateBeforeDeploy(webServer, resourceIdentifier); resourceService.validateSingleResourceForGeneration(resourceIdentifier); resourceService.generateAndDeployFile(resourceIdentifier, webServerName, fileName, webServer.getHost()); } catch (IOException e) { String errorMsg = MessageFormat.format("Failed to retrieve meta data when generating and deploying file {0} for Web Server {1}", fileName, webServerName); LOGGER.error(errorMsg, e); throw new WebServerServiceException(errorMsg); } finally { binaryDistributionLockManager.writeUnlock(webServerName + "-" + webServer.getId().getId().toString()); } return webServer; }
WebServerServiceImpl implements WebServerService { @Override public WebServer generateAndDeployFile(String webServerName, String fileName, User user) { WebServer webServer = getWebServer(webServerName); binaryDistributionLockManager.writeLock(webServerName + "-" + webServer.getId().getId().toString()); ResourceIdentifier resourceIdentifier = new ResourceIdentifier.Builder() .setWebServerName(webServerName) .setResourceName(fileName) .build(); try { checkWebServerStateBeforeDeploy(webServer, resourceIdentifier); resourceService.validateSingleResourceForGeneration(resourceIdentifier); resourceService.generateAndDeployFile(resourceIdentifier, webServerName, fileName, webServer.getHost()); } catch (IOException e) { String errorMsg = MessageFormat.format("Failed to retrieve meta data when generating and deploying file {0} for Web Server {1}", fileName, webServerName); LOGGER.error(errorMsg, e); throw new WebServerServiceException(errorMsg); } finally { binaryDistributionLockManager.writeUnlock(webServerName + "-" + webServer.getId().getId().toString()); } return webServer; } }
WebServerServiceImpl implements WebServerService { @Override public WebServer generateAndDeployFile(String webServerName, String fileName, User user) { WebServer webServer = getWebServer(webServerName); binaryDistributionLockManager.writeLock(webServerName + "-" + webServer.getId().getId().toString()); ResourceIdentifier resourceIdentifier = new ResourceIdentifier.Builder() .setWebServerName(webServerName) .setResourceName(fileName) .build(); try { checkWebServerStateBeforeDeploy(webServer, resourceIdentifier); resourceService.validateSingleResourceForGeneration(resourceIdentifier); resourceService.generateAndDeployFile(resourceIdentifier, webServerName, fileName, webServer.getHost()); } catch (IOException e) { String errorMsg = MessageFormat.format("Failed to retrieve meta data when generating and deploying file {0} for Web Server {1}", fileName, webServerName); LOGGER.error(errorMsg, e); throw new WebServerServiceException(errorMsg); } finally { binaryDistributionLockManager.writeUnlock(webServerName + "-" + webServer.getId().getId().toString()); } return webServer; } WebServerServiceImpl(final WebServerPersistenceService webServerPersistenceService,
                                final ResourceService resourceService,
                                @Qualifier("webServerInMemoryStateManagerService")
                                final InMemoryStateManagerService<Identifier<WebServer>, WebServerReachableState> inMemoryStateManagerService,
                                final String templatePath,
                                final BinaryDistributionLockManager binaryDistributionLockManager); }
WebServerServiceImpl implements WebServerService { @Override public WebServer generateAndDeployFile(String webServerName, String fileName, User user) { WebServer webServer = getWebServer(webServerName); binaryDistributionLockManager.writeLock(webServerName + "-" + webServer.getId().getId().toString()); ResourceIdentifier resourceIdentifier = new ResourceIdentifier.Builder() .setWebServerName(webServerName) .setResourceName(fileName) .build(); try { checkWebServerStateBeforeDeploy(webServer, resourceIdentifier); resourceService.validateSingleResourceForGeneration(resourceIdentifier); resourceService.generateAndDeployFile(resourceIdentifier, webServerName, fileName, webServer.getHost()); } catch (IOException e) { String errorMsg = MessageFormat.format("Failed to retrieve meta data when generating and deploying file {0} for Web Server {1}", fileName, webServerName); LOGGER.error(errorMsg, e); throw new WebServerServiceException(errorMsg); } finally { binaryDistributionLockManager.writeUnlock(webServerName + "-" + webServer.getId().getId().toString()); } return webServer; } WebServerServiceImpl(final WebServerPersistenceService webServerPersistenceService,
                                final ResourceService resourceService,
                                @Qualifier("webServerInMemoryStateManagerService")
                                final InMemoryStateManagerService<Identifier<WebServer>, WebServerReachableState> inMemoryStateManagerService,
                                final String templatePath,
                                final BinaryDistributionLockManager binaryDistributionLockManager); @Override @Transactional WebServer createWebServer(final CreateWebServerRequest createWebServerRequest,
                                     final User aCreatingUser); @Override @Transactional(readOnly = true) WebServer getWebServer(final Identifier<WebServer> aWebServerId); @Override @Transactional(readOnly = true) WebServer getWebServer(final String aWebServerName); @Override @Transactional(readOnly = true) List<WebServer> getWebServers(); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) List<WebServer> getWebServersPropagationNew(); @Override @Transactional(readOnly = true) List<WebServer> findWebServers(final Identifier<Group> aGroupId); @Override @Transactional WebServer updateWebServer(final UpdateWebServerRequest anUpdateWebServerCommand,
                                     final User anUpdatingUser); @Override @Transactional void deleteWebServer(final Identifier<WebServer> id, final boolean hardDelete, final User user); @Override boolean isStarted(WebServer webServer); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void updateState(final Identifier<WebServer> id, final WebServerReachableState state, final String errorStatus); @Override String generateInstallServiceScript(WebServer webServer); @Override @Transactional(readOnly = true) List<String> getResourceTemplateNames(String webServerName); @Override @Transactional(readOnly = true) String getResourceTemplate(final String webServerName, final String resourceTemplateName,
                                      final boolean tokensReplaced, final ResourceGroup resourceGroup); @Override String getResourceTemplateMetaData(String aWebServerName, String resourceTemplateName); @Override @Transactional void uploadWebServerConfig(WebServer webServer, String templateName, String templateContent, String metaDataStr, String groupName, User user); @Override @Transactional String updateResourceTemplate(final String wsName, final String resourceTemplateName, final String template); @Override @Transactional(readOnly = true) String previewResourceTemplate(final String fileName, final String webServerName, final String groupName, final String template); @Override @Transactional(readOnly = true) Long getWebServerStartedCount(final String groupName); @Override @Transactional(readOnly = true) Long getWebServerCount(final String groupName); @Override Long getWebServerStoppedCount(final String groupName); @Override WebServer generateAndDeployFile(String webServerName, String fileName, User user); }
WebServerServiceImpl implements WebServerService { @Override public WebServer generateAndDeployFile(String webServerName, String fileName, User user) { WebServer webServer = getWebServer(webServerName); binaryDistributionLockManager.writeLock(webServerName + "-" + webServer.getId().getId().toString()); ResourceIdentifier resourceIdentifier = new ResourceIdentifier.Builder() .setWebServerName(webServerName) .setResourceName(fileName) .build(); try { checkWebServerStateBeforeDeploy(webServer, resourceIdentifier); resourceService.validateSingleResourceForGeneration(resourceIdentifier); resourceService.generateAndDeployFile(resourceIdentifier, webServerName, fileName, webServer.getHost()); } catch (IOException e) { String errorMsg = MessageFormat.format("Failed to retrieve meta data when generating and deploying file {0} for Web Server {1}", fileName, webServerName); LOGGER.error(errorMsg, e); throw new WebServerServiceException(errorMsg); } finally { binaryDistributionLockManager.writeUnlock(webServerName + "-" + webServer.getId().getId().toString()); } return webServer; } WebServerServiceImpl(final WebServerPersistenceService webServerPersistenceService,
                                final ResourceService resourceService,
                                @Qualifier("webServerInMemoryStateManagerService")
                                final InMemoryStateManagerService<Identifier<WebServer>, WebServerReachableState> inMemoryStateManagerService,
                                final String templatePath,
                                final BinaryDistributionLockManager binaryDistributionLockManager); @Override @Transactional WebServer createWebServer(final CreateWebServerRequest createWebServerRequest,
                                     final User aCreatingUser); @Override @Transactional(readOnly = true) WebServer getWebServer(final Identifier<WebServer> aWebServerId); @Override @Transactional(readOnly = true) WebServer getWebServer(final String aWebServerName); @Override @Transactional(readOnly = true) List<WebServer> getWebServers(); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) List<WebServer> getWebServersPropagationNew(); @Override @Transactional(readOnly = true) List<WebServer> findWebServers(final Identifier<Group> aGroupId); @Override @Transactional WebServer updateWebServer(final UpdateWebServerRequest anUpdateWebServerCommand,
                                     final User anUpdatingUser); @Override @Transactional void deleteWebServer(final Identifier<WebServer> id, final boolean hardDelete, final User user); @Override boolean isStarted(WebServer webServer); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void updateState(final Identifier<WebServer> id, final WebServerReachableState state, final String errorStatus); @Override String generateInstallServiceScript(WebServer webServer); @Override @Transactional(readOnly = true) List<String> getResourceTemplateNames(String webServerName); @Override @Transactional(readOnly = true) String getResourceTemplate(final String webServerName, final String resourceTemplateName,
                                      final boolean tokensReplaced, final ResourceGroup resourceGroup); @Override String getResourceTemplateMetaData(String aWebServerName, String resourceTemplateName); @Override @Transactional void uploadWebServerConfig(WebServer webServer, String templateName, String templateContent, String metaDataStr, String groupName, User user); @Override @Transactional String updateResourceTemplate(final String wsName, final String resourceTemplateName, final String template); @Override @Transactional(readOnly = true) String previewResourceTemplate(final String fileName, final String webServerName, final String groupName, final String template); @Override @Transactional(readOnly = true) Long getWebServerStartedCount(final String groupName); @Override @Transactional(readOnly = true) Long getWebServerCount(final String groupName); @Override Long getWebServerStoppedCount(final String groupName); @Override WebServer generateAndDeployFile(String webServerName, String fileName, User user); static final String INSTALL_SERVICE_SCRIPT_NAME; }