aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java
blob: f5162a6950768c35f132e41a4921ff079697c3b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Engine
 * ================================================================================
 * 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.
 * See the License for the specific language governing permissions and
 * 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 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;
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("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 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();
        }
    }
}