/** * creates and sends an Email to the given emailReceipients * * @param subject * @param content * @param emailReceipients * @return * @throws MessagingException */ public boolean createAndSendMail(String subject, String content, Address[] emailReceipients) throws MessagingException{ boolean deliverEmails = ConfigurationService.getPropertyAsBoolean(ConfigKey.DELIVER_MAIL, true); // deliverEmails is active and emailReceipients are available if (deliverEmails && emailReceipients != null && emailReceipients.length > 0) { return mailService.createMessageAndSend(subject, content, emailReceipients); } return false; } }
@Test public void test_createAndSendMail_nok() throws MessagingException { // given Address[] to = new InternetAddress[1]; to[0] = new InternetAddress("test@test.ch"); when(mailService.createMessageAndSend("subject", "content", to)).thenReturn(false); // when boolean result = notificationService.createAndSendMail("subject", "content", to); // then verify(mailService, times(1)).createMessageAndSend("subject", "content", to); assertFalse(result); }
@Test public void test_createAndSendMail_noEmailReceipients_emptyList() throws MessagingException { // given Address[] to = new InternetAddress[0]; // when boolean result = notificationService.createAndSendMail("subject", "content", to); // then assertFalse(result); }
@Test public void test_createAndSendMail_noEmailReceipients() throws MessagingException { // given // when boolean result = notificationService.createAndSendMail("subject", "content", null); // then assertFalse(result); }
@Test public void test_createAndSendMail_ok() throws MessagingException { // given Address[] to = new InternetAddress[1]; to[0] = new InternetAddress("test@test.ch"); when(mailService.createMessageAndSend("subject", "content", to)).thenReturn(true); // when boolean result = notificationService.createAndSendMail("subject", "content", to); // then verify(mailService, times(1)).createMessageAndSend("subject", "content", to); assertTrue(result); }
@Test public void test_createMessageAndSend_logging_Empty() throws MessagingException { // given // Mail Session is null Address[] to = new InternetAddress[0]; // when mailService.createMessageAndSend("subject", "content", to); // then verify(log, times(1)).warning("Mail session not available, unable to send Mail(subject: subject, content: content ) to Receipients: "); }
Address[] emailReceipients = getAllReceipients(deployments); if (notificationService.createAndSendMail(subjectMessage, getMessageContentForDeployments(deployments), emailReceipients)) {
@Test public void test_createMessageAndSend_logging_null() throws MessagingException { // given // Mail Session is null // when mailService.createMessageAndSend("subject", "content", null); // then verify(log, times(1)).warning("Mail session not available, unable to send Mail(subject: subject, content: content ) to Receipients: "); }
@Test public void test_createAndSendMail_noEmailReceipients_DeliverEmailsFalse() throws MessagingException { // given Properties props = System.getProperties(); props.setProperty(ConfigKey.DELIVER_MAIL.getValue(), "false"); // when boolean result = notificationService.createAndSendMail("subject", "content", null); // then assertFalse(result); props.remove(ConfigKey.DELIVER_MAIL.getValue()); }
@Test public void test_createMessageAndSend_logging() throws MessagingException { // given // Mail Session is null Address[] to = new InternetAddress[1]; to[0] = new InternetAddress("test@test.ch"); // when mailService.createMessageAndSend("subject", "content", to); // then verify(log, times(1)).warning("Mail session not available, unable to send Mail(subject: subject, content: content ) to Receipients: test@test.ch, "); }
@Test public void createAndSendMailForDeplyoments_oneDeployment_noAddress_sending_nok() throws MessagingException { // given ArrayList<DeploymentEntity> deployments = new ArrayList<DeploymentEntity>(); DeploymentEntity deployment = new DeploymentEntity(); deployment.setTrackingId(Integer.valueOf(12)); deployment.setDeploymentState(DeploymentState.success); deployment.setResourceGroup(applicationServer.getResourceGroup()); deployment.setContext(context); deployments.add(deployment); when(notificationService.createAndSendMail(anyString(), anyString(), any(Address[].class))).thenReturn(false); when(releaseMgmtService.getDefaultRelease()).thenReturn(defaultRelease); // when String result = deploymentNotificationService.createAndSendMailForDeplyoments(deployments); // then assertNull(result); verify(log, times(0)).log(any(Level.class),anyString()); }
@Test public void createAndSendMailForDeplyoments_oneDeployment_noAddress_sending_nok_release_onDeployment() throws MessagingException { // given ArrayList<DeploymentEntity> deployments = new ArrayList<DeploymentEntity>(); DeploymentEntity deployment = new DeploymentEntity(); deployment.setTrackingId(Integer.valueOf(12)); deployment.setDeploymentState(DeploymentState.success); deployment.setResourceGroup(applicationServer.getResourceGroup()); deployment.setContext(context); deployment.setRelease(defaultRelease); deployments.add(deployment); when(notificationService.createAndSendMail(anyString(), anyString(), any(Address[].class))).thenReturn(false); // when String result = deploymentNotificationService.createAndSendMailForDeplyoments(deployments); // then assertNull(result); verify(log, times(0)).log(any(Level.class),anyString()); verify(releaseMgmtService, times(0)).getDefaultRelease(); }
@Test public void createAndSendMailForDeplyoments_oneDeployment_noAddress_sending_ok() throws MessagingException { // given ArrayList<DeploymentEntity> deployments = new ArrayList<DeploymentEntity>(); DeploymentEntity deployment = new DeploymentEntity(); deployment.setTrackingId(Integer.valueOf(12)); deployment.setDeploymentState(DeploymentState.success); deployment.setResourceGroup(applicationServer.getResourceGroup()); deployment.setContext(context); deployments.add(deployment); when(notificationService.createAndSendMail(anyString(), anyString(), any(Address[].class))).thenReturn(true); // when String result = deploymentNotificationService.createAndSendMailForDeplyoments(deployments); // then assertNotNull(result); assertEquals("Notification email sent to following receipients: \n", result); }
@Test public void createAndSendMailForDeplyoments_oneDeployment_noAddress_sending_Exception() throws MessagingException { // given ArrayList<DeploymentEntity> deployments = new ArrayList<DeploymentEntity>(); DeploymentEntity deployment = new DeploymentEntity(); deployment.setTrackingId(Integer.valueOf(12)); deployment.setDeploymentState(DeploymentState.success); deployment.setResourceGroup(applicationServer.getResourceGroup()); deployment.setContext(context); deployments.add(deployment); when(notificationService.createAndSendMail(anyString(), anyString(), any(Address[].class))).thenThrow(new MessagingException("ErrorSending")); when(releaseMgmtService.getDefaultRelease()).thenReturn(defaultRelease); // when String result = deploymentNotificationService.createAndSendMailForDeplyoments(deployments); // then assertNotNull(result); assertEquals("Failure occoured while sending notification email: ErrorSending", result); verify(log, times(1)).log(any(Level.class),anyString(), any(Exception.class)); }
@Test public void createAndSendMailForDeplyoments_oneDeployment_noAddress_sending_Receipients() throws MessagingException { // given ArrayList<DeploymentEntity> deployments = new ArrayList<DeploymentEntity>(); DeploymentEntity deployment = new DeploymentEntity(); deployment.setTrackingId(Integer.valueOf(12)); deployment.setDeploymentState(DeploymentState.success); deployment.setResourceGroup(applicationServer.getResourceGroup()); deployment.setApplicationsWithVersion(new ArrayList<DeploymentEntity.ApplicationWithVersion>()); deployment.setContext(context); deployments.add(deployment); when(notificationService.createAndSendMail(anyString(), anyString(), any(Address[].class))).thenReturn(true); when(releaseMgmtService.getDefaultRelease()).thenReturn(defaultRelease); List<String> value = new ArrayList<String>(); value.add("testuser1"); when(userSettingsService.getRegisteredUsernamesForResourcesIds(any(Set.class))).thenReturn(value ); // when String result = deploymentNotificationService.createAndSendMailForDeplyoments(deployments); // then assertNotNull(result); assertEquals("Notification email sent to following receipients: \ntestuser1@null\n", result); }