4a09164b51547103f1a0fc43b8657e9f4c810791
[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
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.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  * @generatedBy CodePro at 6/1/16 1:41 PM
44  * @version $Revision: 1.0 $
45  */
46 public class ManualClientEndTest {
47     private static WebSocketServer ws;
48
49     private static int port = 18080;
50     private static CountDownLatch countServerDownLatch = null;
51     private static String recvMsg = null;
52
53     /**
54      * Start server.
55      *
56      * @throws Exception the exception
57      */
58     @BeforeClass
59     public static void startServer() throws Exception {
60         port = SocketUtils.findAvailableTcpPort();
61         ws = new WebSocketServer(new InetSocketAddress(port), 16) {
62             @Override
63             public void onOpen(WebSocket conn, ClientHandshake handshake) {
64
65             }
66
67             @Override
68             public void onClose(WebSocket conn, int code, String reason, boolean remote) {
69                 countServerDownLatch.countDown();
70             }
71
72             @Override
73             public void onMessage(WebSocket conn, String message) {
74                 recvMsg = message;
75                 conn.send("{\"removedPolicies\": [],\"loadedPolicies\":"
76                         + "[{\"policyName\": \"Test.Config_BRMS_Param_BrmsParamTestPa.1.xml\","
77                         + "\"versionNo\": \"1\",\"matches\": {\"ECOMPName\": \"DROOLS\","
78                         + "\"ONAPName\": \"DROOLS\",\"ConfigName\": \"BRMS_PARAM_RULE\","
79                         + "\"guard\": \"false\",\"TTLDate\": \"NA\",\"RiskLevel\": \"5\","
80                         + "\"RiskType\": \"default\"},\"updateType\": \"NEW\"}],\"notificationType\": \"UPDATE\"}");
81             }
82
83             @Override
84             public void onError(WebSocket conn, Exception ex) {
85
86                 ex.printStackTrace();
87                 fail("There should be no exception!");
88             }
89
90             @Override
91             public void onStart() {}
92
93
94         };
95
96         ws.setConnectionLostTimeout(30);
97         ws.start();
98     }
99
100     @Test
101     public void testAutoClient() throws Exception {
102         countServerDownLatch = new CountDownLatch(1);
103
104         ManualClientEnd.start("http://localhost:" + port + "/");
105         countServerDownLatch.await();
106
107         assertNotNull(ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS));
108         assertTrue("Manual".equalsIgnoreCase(recvMsg));
109     }
110
111     @AfterClass
112     public static void successTests() throws InterruptedException, IOException {
113         ws.stop();
114     }
115 }