2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest;
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;
28 import com.att.research.xacml.api.pap.PAPException;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
32 import java.util.HashSet;
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;
42 public class HeartbeatTest {
44 public void testHeartbeat() throws IOException, PAPException {
45 Heartbeat hb = new Heartbeat(null);
47 assertThatThrownBy(hb::run).isInstanceOf(NullPointerException.class);
48 assertTrue(hb.isHeartBeatRunning());
50 assertFalse(hb.isHeartBeatRunning());
54 public void testGetPdps() throws PAPException, IOException {
55 Set<OnapPDPGroup> pdpGroups = new HashSet<OnapPDPGroup>();
56 StdPDPGroup pdpGroup = new StdPDPGroup();
57 OnapPDP pdp = new StdPDP();
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());
66 assertThatCode(hb::notifyEachPdp).doesNotThrowAnyException();
67 assertThatThrownBy(hb::run).isInstanceOf(Exception.class);
68 assertThatThrownBy(hb::notifyEachPdp).isInstanceOf(Exception.class);
72 public void testOpen() throws MalformedURLException {
73 Heartbeat hb = new Heartbeat(null);
74 OnapPDP pdp = new StdPDP();
76 assertThatCode(() -> {
77 URL url = new URL("http://onap.org");
78 hb.openPdpConnection(url, pdp);
79 }).doesNotThrowAnyException();
81 assertThatCode(() -> {
82 URL url = new URL("http://1.2.3.4");
83 hb.openPdpConnection(url, pdp);
84 }).doesNotThrowAnyException();
86 assertThatCode(() -> {
87 URL url = new URL("http://fakesite.fakenews");
88 hb.openPdpConnection(url, pdp);
89 }).doesNotThrowAnyException();