Update license header in appc client-lib files
[appc.git] / appc-client / client-lib / src / test / java / org / onap / appc / client / impl / protocol / TestAsyncProtocolImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.client.impl.protocol;
25
26 import org.onap.appc.client.impl.core.MessageContext;
27 import org.onap.appc.client.impl.protocol.AsyncProtocol;
28 import org.onap.appc.client.impl.protocol.AsyncProtocolImpl;
29 import org.onap.appc.client.impl.protocol.ProtocolException;
30 import org.onap.appc.client.impl.protocol.RetrieveMessageCallback;
31 import org.onap.appc.client.impl.protocol.UEBPropertiesKeys;
32 import org.junit.Assert;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.util.Properties;
39 import java.util.concurrent.atomic.AtomicBoolean;
40
41 public class TestAsyncProtocolImpl {
42
43     private static AsyncProtocol protocol;
44     private static AtomicBoolean gotResponse;
45     private static Properties props;
46
47     private static class TestCallback implements RetrieveMessageCallback{
48
49         public void onResponse(String payload, MessageContext context) {
50             Assert.assertNotEquals(null, payload);
51             Assert.assertNotEquals(null, context);
52             protocol = null;
53             gotResponse.set(true);
54         }
55     }
56
57     @BeforeClass
58     public static void setUp() throws IOException, ProtocolException {
59
60         gotResponse = new AtomicBoolean(false);
61
62         props = new Properties();
63         String propFileName = "ueb.properties";
64
65         InputStream input = TestAsyncProtocolImpl.class.getClassLoader().getResourceAsStream(propFileName);
66
67         props.load(input);
68
69         protocol = new AsyncProtocolImpl();
70         protocol.init(props, new TestCallback());
71     }
72
73     public void testSendRequest() throws ProtocolException {
74
75         MessageContext context = new MessageContext();
76         context.setType("Test");
77
78         protocol.sendRequest("{\"Test\":\"\"}", context);
79
80         try {
81             Long timeToSleep = Long.parseLong((String)props.get(UEBPropertiesKeys.TOPIC_READ_TIMEOUT))*2;
82             Thread.sleep(timeToSleep);
83         } catch (InterruptedException e) {
84             Assert.assertFalse(e.getMessage(), false);
85         }
86         if (gotResponse.get() == false) {
87             Assert.assertFalse("Message was not read !", true);
88         }
89     }
90 }