Reformat PolicyEngineAPI test cases
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.std.test;
24
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import java.io.IOException;
29 import java.net.InetSocketAddress;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.TimeUnit;
32 import org.java_websocket.WebSocket;
33 import org.java_websocket.handshake.ClientHandshake;
34 import org.java_websocket.server.WebSocketServer;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.api.NotificationScheme;
39 import org.onap.policy.std.ManualClientEnd;
40
41 /**
42  * The class <code>ManualClientEndTest</code> contains tests for the class
43  * <code>{@link ManualClientEnd}</code>.
44  *
45  */
46 public class ManualClientEndTest {
47     private static WebSocketServer ws;
48
49     private static int port;
50     private static volatile String recvMsg = null;
51     private static volatile Exception webEx = null;
52
53     /**
54      * Start server.
55      *
56      * @throws Exception the exception
57      */
58     @BeforeClass
59     public static void startServer() throws Exception {
60
61         CountDownLatch latch = new CountDownLatch(1);
62
63         ws = new WebSocketServer(new InetSocketAddress(0), 1) {
64             @Override
65             public void onOpen(WebSocket conn, ClientHandshake handshake) {}
66
67             @Override
68             public void onClose(WebSocket conn, int code, String reason, boolean remote) {}
69
70             @Override
71             public void onMessage(WebSocket conn, String message) {
72
73                 // NOTE: must copy to recvMsg BEFORE invoking conn.send()
74                 recvMsg = message;
75
76                 conn.send("{\"removedPolicies\": [],\"loadedPolicies\":"
77                         + "[{\"policyName\": \"Test.Config_BRMS_Param_BrmsParamTestPa.1.xml\","
78                         + "\"versionNo\": \"1\",\"matches\": {\"ECOMPName\": \"DROOLS\","
79                         + "\"ONAPName\": \"DROOLS\",\"ConfigName\": \"BRMS_PARAM_RULE\","
80                         + "\"guard\": \"false\",\"TTLDate\": \"NA\",\"RiskLevel\": \"5\","
81                         + "\"RiskType\": \"default\"},\"updateType\": \"NEW\"}],"
82                         + "\"notificationType\": \"UPDATE\"}");
83             }
84
85             @Override
86             public void onError(WebSocket conn, Exception ex) {
87                 webEx = ex;
88                 ex.printStackTrace();
89                 latch.countDown();
90             }
91
92             @Override
93             public void onStart() {
94                 latch.countDown();
95             }
96         };
97
98         ws.setConnectionLostTimeout(0);
99         ws.setReuseAddr(true);
100         ws.start();
101
102         // ensure port connected (or error) before running the actual test
103         latch.await(20, TimeUnit.SECONDS);
104
105         // ensure no error during start-up
106         assertNull(webEx);
107
108         port = ws.getPort();
109     }
110
111     @Test
112     public void testManualClient() throws Exception {
113         ManualClientEnd.start("http://localhost:" + port + "/");
114
115         assertNotNull(ManualClientEnd.result(NotificationScheme.MANUAL_ALL_NOTIFICATIONS));
116         assertTrue("Manual".equalsIgnoreCase(recvMsg));
117     }
118
119     @AfterClass
120     public static void successTests() throws InterruptedException, IOException {
121         ws.stop(5000);
122     }
123 }