7d31c24b5682ba38ab8717b52886b67602cd8a9f
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / std / test / ManualClientEndTest.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 import java.io.IOException;
27 import java.net.InetSocketAddress;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
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.NotificationScheme;
37 import org.onap.policy.std.ManualClientEnd;
38 import org.springframework.util.SocketUtils;
39
40 /**
41  * The class <code>ManualClientEndTest</code> contains tests for the class <code>{@link ManualClientEnd}</code>.
42  *
43  */
44 public class ManualClientEndTest {
45     private static WebSocketServer ws;
46
47     private static int port = SocketUtils.findAvailableTcpPort();
48     private static CountDownLatch countServerDownLatch = null;
49     private static String recvMsg = null;
50
51     /**
52      * Start server.
53      *
54      * @throws Exception the exception
55      */
56     @BeforeClass
57     public static void startServer() throws Exception {
58         ws = new WebSocketServer(new InetSocketAddress(port), 1) {
59             @Override
60             public void onOpen(WebSocket conn, ClientHandshake handshake) {
61
62             }
63
64             @Override
65             public void onClose(WebSocket conn, int code, String reason, boolean remote) {
66                 countServerDownLatch.countDown();
67             }
68
69             @Override
70             public void onMessage(WebSocket conn, String message) {
71                 recvMsg = message;
72                 conn.send("{\"removedPolicies\": [],\"loadedPolicies\":"
73                         + "[{\"policyName\": \"Test.Config_BRMS_Param_BrmsParamTestPa.1.xml\","
74                         + "\"versionNo\": \"1\",\"matches\": {\"ECOMPName\": \"DROOLS\","
75                         + "\"ONAPName\": \"DROOLS\",\"ConfigName\": \"BRMS_PARAM_RULE\","
76                         + "\"guard\": \"false\",\"TTLDate\": \"NA\",\"RiskLevel\": \"5\","
77                         + "\"RiskType\": \"default\"},\"updateType\": \"NEW\"}],\"notificationType\": \"UPDATE\"}");
78             }
79
80             @Override
81             public void onError(WebSocket conn, Exception ex) {
82
83                 ex.printStackTrace();
84                 fail("There should be no exception!");
85             }
86
87             @Override
88             public void onStart() {}
89         };
90
91         ws.setConnectionLostTimeout(0);
92         ws.setReuseAddr(true);
93         ws.start();
94     }
95
96     @Test
97     public void testManualClient() throws Exception {
98         countServerDownLatch = new CountDownLatch(1);
99
100         ManualClientEnd.start("http://localhost:" + port + "/");
101         countServerDownLatch.await(45, TimeUnit.SECONDS);
102
103         assertNotNull(ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS));
104         assertTrue("Manual".equalsIgnoreCase(recvMsg));
105     }
106
107     @AfterClass
108     public static void successTests() throws InterruptedException, IOException {
109         ws.stop(30000);
110     }
111 }