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