Reformat ONAP-PDP-REST test cases
[policy/engine.git] / ONAP-PDP-REST / src / test / java / org / onap / policy / pdp / rest / api / test / NotificationAPITest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22 package org.onap.policy.pdp.rest.api.test;
23
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;
35
36 public class NotificationAPITest {
37
38     @Before
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();
44     }
45
46     @Test
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());
54         setup();
55     }
56
57     @Test
58     public void testFailureTopicName() {
59         NotificationService notificationService = new NotificationService(null, null, null);
60         assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
61         assertEquals(
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());
67         assertEquals(
68                 XACMLErrorConstants.ERROR_DATA_ISSUE
69                         + "org.onap.policy.api.PolicyException: Notification Topic is not valid. ",
70                 notificationService.getResult());
71     }
72
73     @Test
74     public void testFailureServiceType() {
75         NotificationService notificationService = new NotificationService("test", null, null);
76         assertEquals(HttpStatus.BAD_REQUEST, notificationService.getResponseCode());
77     }
78
79     @Test
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());
88         });
89         // Coverage Tests, Call Notification Service.
90         NotificationService.sendNotification("test");
91     }
92 }