Consolidate PolicyRestAdapter setup
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / admin / PolicyNotificationMailTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.admin;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.network.NetworkUtil;
36 import org.onap.policy.controller.PolicyController;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.PolicyVersion;
39 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
40
41 public class PolicyNotificationMailTest {
42
43     private PolicyVersion version;
44     private String policyName = "com/Config_Test";
45     private CommonClassDao commonClassDao;
46     private List<Object> data = null;
47
48     /**
49      * setUp.
50      *
51      * @throws Exception should not throw one
52      */
53     @Before
54     public void setUp() throws Exception {
55         PolicyController.setjUnit(true);
56         PolicyController.setSmtpApplicationName("Test");
57         PolicyController.setSmtpEmailExtension("test.com");
58         PolicyController.setSmtpHost("localhost");
59         PolicyController.setSmtpPassword("test");
60         PolicyController.setSmtpUsername("test");
61
62         /*
63          * Allocate a port to which the mail sender should connect, but don't actually
64          * start a listener on the port so that connection attempts will be immediately
65          * rejected.
66          */
67         PolicyController.setSmtpPort(String.valueOf(NetworkUtil.allocPort()));
68
69         version = new PolicyVersion();
70         version.setPolicyName("com/Config_Test");
71         version.setModifiedBy("xyz");
72
73         WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
74         watch.setPolicyName("com/Config_Test");
75         data = new ArrayList<>();
76         data.add(watch);
77
78         commonClassDao = mock(CommonClassDao.class);
79         PolicyController.setCommonClassDao(commonClassDao);
80         when(commonClassDao.getDataByQuery("from WatchPolicyNotificationTable where policyName like:policyFileName",
81                 null)).thenReturn(data);
82     }
83
84     @Test
85     public final void testJavaMailSenderImpl() {
86         PolicyNotificationMail notificationMail = new PolicyNotificationMail();
87         try {
88             assertTrue(notificationMail.javaMailSenderImpl() != null);
89         } catch (Exception e) {
90             fail();
91         }
92     }
93
94     @Test
95     public final void testSendMail() {
96         PolicyNotificationMail notificationMail = new PolicyNotificationMail();
97         try {
98             notificationMail.sendMail(version, policyName, "EditPolicy", commonClassDao);
99             notificationMail.sendMail(version, policyName, "Rename", commonClassDao);
100             notificationMail.sendMail(version, policyName, "DeleteAll", commonClassDao);
101             notificationMail.sendMail(version, policyName, "DeleteOne", commonClassDao);
102             notificationMail.sendMail(version, policyName, "DeleteScope", commonClassDao);
103             notificationMail.sendMail(version, policyName, "SwitchVersion", commonClassDao);
104             notificationMail.sendMail(version, policyName, "Move", commonClassDao);
105         } catch (Exception e) {
106             fail();
107         }
108     }
109 }