Format java POLICY-SDK-APP
[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     @Before
49     public void setUp() throws Exception {
50         PolicyController.setjUnit(true);
51         PolicyController.setSmtpApplicationName("Test");
52         PolicyController.setSmtpEmailExtension("test.com");
53         PolicyController.setSmtpHost("localhost");
54         PolicyController.setSmtpPassword("test");
55         PolicyController.setSmtpUsername("test");
56
57         /*
58          * Allocate a port to which the mail sender should connect, but don't actually
59          * start a listener on the port so that connection attempts will be immediately
60          * rejected.
61          */
62         PolicyController.setSmtpPort(String.valueOf(NetworkUtil.allocPort()));
63
64         version = new PolicyVersion();
65         version.setPolicyName("com/Config_Test");
66         version.setModifiedBy("xyz");
67
68         WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
69         watch.setPolicyName("com/Config_Test");
70         data = new ArrayList<>();
71         data.add(watch);
72
73         commonClassDao = mock(CommonClassDao.class);
74         PolicyController.setCommonClassDao(commonClassDao);
75         when(commonClassDao.getDataByQuery("from WatchPolicyNotificationTable where policyName like:policyFileName",
76                 null)).thenReturn(data);
77     }
78
79     @Test
80     public final void testJavaMailSenderImpl() {
81         PolicyNotificationMail notificationMail = new PolicyNotificationMail();
82         try {
83             assertTrue(notificationMail.javaMailSenderImpl() != null);
84         } catch (Exception e) {
85             fail();
86         }
87     }
88
89     @Test
90     public final void testSendMail() {
91         PolicyNotificationMail notificationMail = new PolicyNotificationMail();
92         try {
93             notificationMail.sendMail(version, policyName, "EditPolicy", commonClassDao);
94             notificationMail.sendMail(version, policyName, "Rename", commonClassDao);
95             notificationMail.sendMail(version, policyName, "DeleteAll", commonClassDao);
96             notificationMail.sendMail(version, policyName, "DeleteOne", commonClassDao);
97             notificationMail.sendMail(version, policyName, "DeleteScope", commonClassDao);
98             notificationMail.sendMail(version, policyName, "SwitchVersion", commonClassDao);
99             notificationMail.sendMail(version, policyName, "Move", commonClassDao);
100         } catch (Exception e) {
101             fail();
102         }
103     }
104 }