2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pdp.rest.api.test;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import java.io.IOException;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.pdp.rest.api.services.NotificationService;
31 import org.onap.policy.pdp.rest.api.services.NotificationService.NotificationServiceType;
32 import org.onap.policy.xacml.api.XACMLErrorConstants;
33 import org.springframework.http.HttpStatus;
34 import com.att.research.xacml.util.XACMLProperties;
36 public class NotificationAPITest {
39 public void setup() throws IOException {
40 // Fix properties for next test cases.
41 XACMLProperties.reloadProperties();
42 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/pass.xacml.pdp.properties");
43 XACMLProperties.getProperties();
47 public void testPropertyFailure() throws IOException {
48 // Change properties and fail.
49 XACMLProperties.reloadProperties();
50 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/fail.xacml.pdp.properties");
51 XACMLProperties.getProperties();
52 NotificationService notificationService = new NotificationService(null, null, null);
53 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
58 public void testFailureTopicName() {
59 NotificationService notificationService = new NotificationService(null, null, null);
60 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
62 XACMLErrorConstants.ERROR_DATA_ISSUE
63 + "org.onap.policy.api.PolicyException: Notification Topic is null",
64 notificationService.getResult());
65 notificationService = new NotificationService(" ", null, null);
66 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
68 XACMLErrorConstants.ERROR_DATA_ISSUE
69 + "org.onap.policy.api.PolicyException: Notification Topic is not valid. ",
70 notificationService.getResult());
74 public void testFailureServiceType() {
75 NotificationService notificationService = new NotificationService("test", null, null);
76 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
80 public void threadTest() throws InterruptedException {
81 NotificationService notificationSerivce = new NotificationService("test", null, NotificationServiceType.ADD);
82 assertEquals(HttpStatus.OK, notificationSerivce.getResponseCode());
83 // Wait for thread to remove the Topic Entry.
84 await().atMost(Integer.toUnsignedLong(2500), TimeUnit.MILLISECONDS).until(() -> {
85 // Trying to remove again should fail
86 NotificationService nService = new NotificationService("test", null, NotificationServiceType.REMOVE);
87 return HttpStatus.BAD_REQUEST.equals(nService.getResponseCode());
89 // Coverage Tests, Call Notification Service.
90 NotificationService.sendNotification("test");