Few JUnit additions for PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / HeartbeatTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import com.att.research.xacml.api.pap.PAPException;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.util.HashSet;
33 import java.util.Set;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.policy.xacml.api.pap.OnapPDP;
37 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
38 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
39 import org.onap.policy.xacml.std.pap.StdPDP;
40 import org.onap.policy.xacml.std.pap.StdPDPGroup;
41
42 public class HeartbeatTest {
43     @Test
44     public void testHeartbeat() throws IOException, PAPException {
45         Heartbeat hb = new Heartbeat(null);
46
47         assertThatThrownBy(hb::run).isInstanceOf(NullPointerException.class);
48         assertTrue(hb.isHeartBeatRunning());
49         hb.terminate();
50         assertFalse(hb.isHeartBeatRunning());
51     }
52
53     @Test
54     public void testGetPdps() throws PAPException, IOException {
55         Set<OnapPDPGroup> pdpGroups = new HashSet<OnapPDPGroup>();
56         StdPDPGroup pdpGroup = new StdPDPGroup();
57         OnapPDP pdp = new StdPDP();
58         pdpGroup.addPDP(pdp);
59         pdpGroups.add(pdpGroup);
60         PAPPolicyEngine pap = Mockito.mock(PAPPolicyEngine.class);
61         Mockito.when(pap.getOnapPDPGroups()).thenReturn(pdpGroups);
62         Heartbeat hb = new Heartbeat(pap);
63         hb.getPdpsFromGroup();
64         assertFalse(hb.isHeartBeatRunning());
65
66         assertThatCode(hb::notifyEachPdp).doesNotThrowAnyException();
67         assertThatThrownBy(hb::run).isInstanceOf(Exception.class);
68         assertThatThrownBy(hb::notifyEachPdp).isInstanceOf(Exception.class);
69     }
70
71     @Test
72     public void testOpen() throws MalformedURLException {
73         Heartbeat hb = new Heartbeat(null);
74         OnapPDP pdp = new StdPDP();
75
76         assertThatCode(() -> {
77             URL url = new URL("http://onap.org");
78             hb.openPdpConnection(url, pdp);
79         }).doesNotThrowAnyException();
80
81         assertThatCode(() -> {
82             URL url = new URL("http://1.2.3.4");
83             hb.openPdpConnection(url, pdp);
84         }).doesNotThrowAnyException();
85
86         assertThatCode(() -> {
87             URL url = new URL("http://fakesite.fakenews");
88             hb.openPdpConnection(url, pdp);
89         }).doesNotThrowAnyException();
90     }
91 }