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