Format java POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / admin / PolicyNotificationMailTest.java
index 2c56a8b..4449676 100644 (file)
@@ -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.
  * 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<Object> 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<Object> 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();
+        }
+    }
 }