Incorporate Liam code review
[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 java.util.concurrent.TimeUnit;
31 import org.java_websocket.WebSocket;
32 import org.java_websocket.handshake.ClientHandshake;
33 import org.java_websocket.server.WebSocketServer;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.api.NotificationScheme;
38 import org.onap.policy.std.ManualClientEnd;
39 import org.springframework.util.SocketUtils;
40
41 /**
42  * The class <code>ManualClientEndTest</code> contains tests for the class <code>{@link ManualClientEnd}</code>.
43  *
44  */
45 public class ManualClientEndTest {
46     private static WebSocketServer ws;
47
48     private static int port = SocketUtils.findAvailableTcpPort();
49     private static CountDownLatch countServerDownLatch = null;
50     private static String recvMsg = null;
51
52     /**
53      * Start server.
54      *
55      * @throws Exception the exception
56      */
57     @BeforeClass
58     public static void startServer() throws Exception {
59         ws = new WebSocketServer(new InetSocketAddress(port), 1) {
60             @Override
61             public void onOpen(WebSocket conn, ClientHandshake handshake) {
62
63             }
64
65             @Override
66             public void onClose(WebSocket conn, int code, String reason, boolean remote) {
67                 countServerDownLatch.countDown();
68             }
69
70             @Override
71             public void onMessage(WebSocket conn, String message) {
72                 recvMsg = message;
73                 conn.send("{\"removedPolicies\": [],\"loadedPolicies\":"
74                         + "[{\"policyName\": \"Test.Config_BRMS_Param_BrmsParamTestPa.1.xml\","
75                         + "\"versionNo\": \"1\",\"matches\": {\"ECOMPName\": \"DROOLS\","
76                         + "\"ONAPName\": \"DROOLS\",\"ConfigName\": \"BRMS_PARAM_RULE\","
77                         + "\"guard\": \"false\",\"TTLDate\": \"NA\",\"RiskLevel\": \"5\","
78                         + "\"RiskType\": \"default\"},\"updateType\": \"NEW\"}],\"notificationType\": \"UPDATE\"}");
79             }
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(0);
95         ws.start();
96     }
97
98     @Test
99     public void testManualClient() throws Exception {
100         countServerDownLatch = new CountDownLatch(1);
101
102         ManualClientEnd.start("http://localhost:" + port + "/");
103         countServerDownLatch.await(45, TimeUnit.SECONDS);
104
105         assertNotNull(ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS));
106         assertTrue("Manual".equalsIgnoreCase(recvMsg));
107     }
108
109     @AfterClass
110     public static void successTests() throws InterruptedException, IOException {
111         ws.stop(30000);
112     }
113 }