2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.pdp.rest.api.test;
22 import static org.awaitility.Awaitility.await;
23 import static org.junit.Assert.assertEquals;
25 import java.io.IOException;
26 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;
35 import com.att.research.xacml.util.XACMLProperties;
37 public class NotificationAPITest {
40 public void setup() throws IOException{
41 // Fix properties for next test cases.
42 XACMLProperties.reloadProperties();
43 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/pass.xacml.pdp.properties");
44 XACMLProperties.getProperties();
48 public void testPropertyFailure() throws IOException{
49 // Change properties and fail.
50 XACMLProperties.reloadProperties();
51 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/fail.xacml.pdp.properties");
52 XACMLProperties.getProperties();
53 NotificationService notificationService = new NotificationService(null,null,null);
54 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
59 public void testFailureTopicName(){
60 NotificationService notificationService = new NotificationService(null,null,null);
61 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
62 assertEquals(XACMLErrorConstants.ERROR_DATA_ISSUE + "org.onap.policy.api.PolicyException: Notification Topic is null", notificationService.getResult());
63 notificationService = new NotificationService(" ",null,null);
64 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
65 assertEquals(XACMLErrorConstants.ERROR_DATA_ISSUE + "org.onap.policy.api.PolicyException: Notification Topic is not valid. ", notificationService.getResult());
69 public void testFailureServiceType(){
70 NotificationService notificationService = new NotificationService("test",null,null);
71 assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
75 public void threadTest() throws InterruptedException{
76 NotificationService notificationSerivce = new NotificationService("test",null,NotificationServiceType.ADD);
77 assertEquals(HttpStatus.OK, notificationSerivce.getResponseCode());
78 // Wait for thread to remove the Topic Entry.
79 await().atMost(Integer.toUnsignedLong(2500),TimeUnit.MILLISECONDS).until(()-> {
80 // Trying to remove again should fail
81 NotificationService nService = new NotificationService("test",null,NotificationServiceType.REMOVE);
82 return HttpStatus.BAD_REQUEST.equals(nService.getResponseCode());
84 // Coverage Tests, Call Notification Service.
85 NotificationService.sendNotification("test");