Fix license on updated engine files
[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 package org.onap.policy.admin;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 import static org.mockito.Mockito.when;
32 import org.onap.policy.common.utils.network.NetworkUtil;
33 import org.onap.policy.controller.PolicyController;
34 import org.onap.policy.rest.dao.CommonClassDao;
35 import org.onap.policy.rest.jpa.PolicyVersion;
36 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
37
38 public class PolicyNotificationMailTest {
39
40     private PolicyVersion version;
41     private String policyName = "com/Config_Test";
42     private CommonClassDao commonClassDao;
43     private List<Object> data = null;
44
45     @Before
46     public void setUp() throws Exception {
47         PolicyController.setjUnit(true);
48         PolicyController.setSmtpApplicationName("Test");
49         PolicyController.setSmtpEmailExtension("test.com");
50         PolicyController.setSmtpHost("localhost");
51         PolicyController.setSmtpPassword("test");
52         PolicyController.setSmtpUsername("test");
53
54         /*
55          * Allocate a port to which the mail sender should connect, but don't actually
56          * start a listener on the port so that connection attempts will be immediately
57          * rejected.
58          */
59         PolicyController.setSmtpPort(String.valueOf(NetworkUtil.allocPort()));
60
61         version = new PolicyVersion();
62         version.setPolicyName("com/Config_Test");
63         version.setModifiedBy("xyz");
64
65         WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
66         watch.setPolicyName("com/Config_Test");
67         data = new ArrayList<>();
68         data.add(watch);
69
70         commonClassDao = mock(CommonClassDao.class);
71         PolicyController.setCommonClassDao(commonClassDao);
72         when(commonClassDao.getDataByQuery(
73                 "from WatchPolicyNotificationTable where policyName like:policyFileName", null))
74                         .thenReturn(data);
75     }
76
77     @Test
78     public final void testJavaMailSenderImpl() {
79         PolicyNotificationMail notificationMail = new PolicyNotificationMail();
80         try {
81             assertTrue(notificationMail.javaMailSenderImpl() != null);
82         } catch (Exception e) {
83             fail();
84         }
85     }
86
87     @Test
88     public final void testSendMail() {
89         PolicyNotificationMail notificationMail = new PolicyNotificationMail();
90         try {
91             notificationMail.sendMail(version, policyName, "EditPolicy", commonClassDao);
92             notificationMail.sendMail(version, policyName, "Rename", commonClassDao);
93             notificationMail.sendMail(version, policyName, "DeleteAll", commonClassDao);
94             notificationMail.sendMail(version, policyName, "DeleteOne", commonClassDao);
95             notificationMail.sendMail(version, policyName, "DeleteScope", commonClassDao);
96             notificationMail.sendMail(version, policyName, "SwitchVersion", commonClassDao);
97             notificationMail.sendMail(version, policyName, "Move", commonClassDao);
98         } catch (Exception e) {
99             fail();
100         }
101     }
102 }