X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fadmin%2FPolicyNotificationMailTest.java;h=4449676d3bf80840af0d15a81fb7aabedee6d1ff;hp=2c56a8b7fb8cc425e9aeadf4fb85a2234ff82c10;hb=dfd9c0a09c35e4b5b4b61be08b8424e4a3d0d500;hpb=1b6feb8d0ca8094a0b7a552f1e3907f206be2527 diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java index 2c56a8b7f..4449676d3 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java @@ -2,14 +2,16 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (C) 2019 Samsung * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,18 +19,20 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.admin; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; -import static org.mockito.Mockito.when; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.controller.PolicyController; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.PolicyVersion; @@ -36,59 +40,65 @@ import org.onap.policy.rest.jpa.WatchPolicyNotificationTable; public class PolicyNotificationMailTest { - private PolicyVersion version; - private String policyName = "com/Config_Test"; - private CommonClassDao commonClassDao; - private List data = null; - - @Before - public void setUp() throws Exception { - PolicyController.setjUnit(true); - PolicyController.setSmtpApplicationName("Test"); - PolicyController.setSmtpEmailExtension("test.com"); - PolicyController.setSmtpHost("test"); - PolicyController.setSmtpPort("23"); - PolicyController.setSmtpPassword("test"); - PolicyController.setSmtpUsername("test"); - - version = new PolicyVersion(); - version.setPolicyName("com/Config_Test"); - version.setModifiedBy("xyz"); - - WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable(); - watch.setPolicyName("com/Config_Test"); - data = new ArrayList<>(); - data.add(watch); - - commonClassDao = mock(CommonClassDao.class); - PolicyController.setCommonClassDao(commonClassDao); - when(commonClassDao.getDataByQuery("from WatchPolicyNotificationTable where policyName like:policyFileName", null)).thenReturn(data); - } + private PolicyVersion version; + private String policyName = "com/Config_Test"; + private CommonClassDao commonClassDao; + private List data = null; - @Test - public final void testJavaMailSenderImpl() { - PolicyNotificationMail notificationMail = new PolicyNotificationMail(); - try{ - assertTrue(notificationMail.javaMailSenderImpl() != null); - }catch(Exception e){ - fail(); - } - } + @Before + public void setUp() throws Exception { + PolicyController.setjUnit(true); + PolicyController.setSmtpApplicationName("Test"); + PolicyController.setSmtpEmailExtension("test.com"); + PolicyController.setSmtpHost("localhost"); + PolicyController.setSmtpPassword("test"); + PolicyController.setSmtpUsername("test"); + + /* + * Allocate a port to which the mail sender should connect, but don't actually + * start a listener on the port so that connection attempts will be immediately + * rejected. + */ + PolicyController.setSmtpPort(String.valueOf(NetworkUtil.allocPort())); + + version = new PolicyVersion(); + version.setPolicyName("com/Config_Test"); + version.setModifiedBy("xyz"); + + WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable(); + watch.setPolicyName("com/Config_Test"); + data = new ArrayList<>(); + data.add(watch); + + commonClassDao = mock(CommonClassDao.class); + PolicyController.setCommonClassDao(commonClassDao); + when(commonClassDao.getDataByQuery("from WatchPolicyNotificationTable where policyName like:policyFileName", + null)).thenReturn(data); + } - @Test - public final void testSendMail() { - PolicyNotificationMail notificationMail = new PolicyNotificationMail(); - try{ - notificationMail.sendMail(version, policyName, "EditPolicy", commonClassDao); - notificationMail.sendMail(version, policyName, "Rename", commonClassDao); - notificationMail.sendMail(version, policyName, "DeleteAll", commonClassDao); - notificationMail.sendMail(version, policyName, "DeleteOne", commonClassDao); - notificationMail.sendMail(version, policyName, "DeleteScope", commonClassDao); - notificationMail.sendMail(version, policyName, "SwitchVersion", commonClassDao); - notificationMail.sendMail(version, policyName, "Move", commonClassDao); - }catch(Exception e){ - fail(); - } - } + @Test + public final void testJavaMailSenderImpl() { + PolicyNotificationMail notificationMail = new PolicyNotificationMail(); + try { + assertTrue(notificationMail.javaMailSenderImpl() != null); + } catch (Exception e) { + fail(); + } + } + @Test + public final void testSendMail() { + PolicyNotificationMail notificationMail = new PolicyNotificationMail(); + try { + notificationMail.sendMail(version, policyName, "EditPolicy", commonClassDao); + notificationMail.sendMail(version, policyName, "Rename", commonClassDao); + notificationMail.sendMail(version, policyName, "DeleteAll", commonClassDao); + notificationMail.sendMail(version, policyName, "DeleteOne", commonClassDao); + notificationMail.sendMail(version, policyName, "DeleteScope", commonClassDao); + notificationMail.sendMail(version, policyName, "SwitchVersion", commonClassDao); + notificationMail.sendMail(version, policyName, "Move", commonClassDao); + } catch (Exception e) { + fail(); + } + } }