4f1ce6f59a4ee8833200c1bbca138519cc8ffa83
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / std / test / AutoClientEndTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
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.std.test;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.net.InetSocketAddress;
29 import java.util.concurrent.CountDownLatch;
30 import org.java_websocket.WebSocket;
31 import org.java_websocket.handshake.ClientHandshake;
32 import org.java_websocket.server.WebSocketServer;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.api.NotificationHandler;
37 import org.onap.policy.api.NotificationScheme;
38 import org.onap.policy.api.PDPNotification;
39 import org.onap.policy.std.AutoClientEnd;
40 import org.onap.policy.std.StdPDPNotification;
41 import org.springframework.util.SocketUtils;
42
43 /**
44  * The class <code>AutoClientEndTest</code> contains tests for the class <code>{@link AutoClientEnd}</code>.
45  *
46  */
47 public class AutoClientEndTest {
48     private static WebSocketServer ws;
49
50     private static int port = 18080;
51     private static CountDownLatch countServerDownLatch = null;
52     private StdPDPNotification notification = null;
53
54     /**
55      * Start server.
56      *
57      * @throws Exception the exception
58      */
59     @BeforeClass
60     public static void startServer() throws Exception {
61         port = SocketUtils.findAvailableTcpPort();
62         ws = new WebSocketServer(new InetSocketAddress(port), 16) {
63             @Override
64             public void onOpen(WebSocket conn, ClientHandshake handshake) {
65                 conn.send("{\"removedPolicies\": [],\"loadedPolicies\": "
66                         + "[{\"policyName\": \"Test.Config_BRMS_Param_BrmsParamTestPa.1.xml\","
67                         + "\"versionNo\": \"1\",\"matches\": {\"ECOMPName\": \"DROOLS\","
68                         + "\"ONAPName\": \"DROOLS\",\"ConfigName\": \"BRMS_PARAM_RULE\","
69                         + "\"guard\": \"false\",\"TTLDate\": \"NA\",\"RiskLevel\": \"5\","
70                         + "\"RiskType\": \"default\"},\"updateType\": \"NEW\"}],\"notificationType\": \"UPDATE\"}");
71             }
72
73             @Override
74             public void onClose(WebSocket conn, int code, String reason, boolean remote) {
75
76             }
77
78             @Override
79             public void onMessage(WebSocket conn, String message) {}
80
81             @Override
82             public void onError(WebSocket conn, Exception ex) {
83
84                 ex.printStackTrace();
85                 fail("There should be no exception!");
86             }
87
88             @Override
89             public void onStart() {}
90
91
92         };
93
94         ws.setConnectionLostTimeout(30);
95         ws.start();
96     }
97
98     @Test
99     public void testAutoClient() throws Exception {
100
101         NotificationScheme scheme = NotificationScheme.AUTO_ALL_NOTIFICATIONS;
102         NotificationHandler handler = new NotificationHandler() {
103
104             @Override
105             public void notificationReceived(PDPNotification notifi) {
106                 notification = (StdPDPNotification) notifi;
107                 countServerDownLatch.countDown();
108
109             }
110         };
111
112         AutoClientEnd.setAuto(scheme, handler);
113         countServerDownLatch = new CountDownLatch(1);
114
115         AutoClientEnd.start("http://localhost:" + port + "/");
116         countServerDownLatch.await();
117
118
119         assertNotNull(notification);
120         assertTrue(AutoClientEnd.getStatus());
121     }
122
123     @AfterClass
124     public static void successTests() throws InterruptedException, IOException {
125         AutoClientEnd.stop();
126         ws.stop();
127     }
128
129
130
131 }