Merge of new rebased code
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / openecomp / appc / listener / impl / TestEventHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.listener.impl;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.IOException;
30 import java.io.Serializable;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.Set;
35
36 import org.apache.commons.lang3.StringUtils;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.openecomp.appc.listener.EventHandler;
41 import org.openecomp.appc.listener.ListenerProperties;
42 import org.openecomp.appc.listener.impl.EventHandlerImpl;
43
44 import com.fasterxml.jackson.annotation.JsonProperty;
45 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
46
47 /**
48  * Test the ProviderAdapter implementation.
49  *
50  */
51
52 public class TestEventHandler {
53
54     private ListenerProperties prop;
55
56     private EventHandler adapter;
57
58     private static final String PROP_FILE = "/org/openecomp/appc/default.properties";
59
60     private static final String MESSAGE_FILE = "/DCAEResponse.txt";
61
62     /**
63      * Setup the test environment.
64      * 
65      * @throws NoSuchMethodException
66      * @throws SecurityException
67      * @throws NoSuchFieldException
68      */
69     @Before
70     public void setup() {
71         Properties allProps = new Properties();
72         try {
73             allProps.load(getClass().getResourceAsStream(PROP_FILE));
74             allProps.remove("appc.ClosedLoop.topic.read.filter");
75             prop = new ListenerProperties("appc.ClosedLoop", allProps);
76         } catch (IOException e) {
77             System.out.println("WARNING: Failed to load properties file: " + PROP_FILE);
78         }
79         adapter = new EventHandlerImpl(prop);
80     }
81     
82     @Test
83     public void testInitialProperties() {
84         assertEquals(prop.getProperty("topic.read"), adapter.getReadTopic());
85         assertTrue(adapter.getWriteTopics().contains(prop.getProperty("topic.write")));
86         assertEquals(prop.getProperty("client.name"), adapter.getClientName());
87         assertEquals(prop.getProperty("client.name.id"), adapter.getClientId());
88
89         String hostStr = prop.getProperty("poolMembers");
90         int hostCount = hostStr.length()>0 ? hostStr.split(",").length : 0;
91         assertEquals(hostCount, adapter.getPool().size());
92     }
93
94     @Test
95     public void testGettersAndSetters() {
96         String readTopic = "read";
97         Set<String> writeTopic = new HashSet<String>();
98         writeTopic.add("write");
99         String clientName = "APPC-TEST";
100         String clientId = "00";
101         String newHost = "google.com";
102
103         adapter.setReadTopic(readTopic);
104         assertEquals(readTopic, adapter.getReadTopic());
105
106         adapter.setWriteTopics(writeTopic);
107         assertEquals(writeTopic, adapter.getWriteTopics());
108
109         adapter.setClientName(clientName);
110         assertEquals(clientName, adapter.getClientName());
111
112         adapter.setClientId(clientId);
113         assertEquals(clientId, adapter.getClientId());
114
115         adapter.setCredentials("fake", "secret");
116         adapter.clearCredentials();
117
118         int oldSize = adapter.getPool().size();
119         adapter.addToPool(newHost);
120         assertEquals(oldSize + 1, adapter.getPool().size());
121         assertTrue(adapter.getPool().contains(newHost));
122
123         adapter.removeFromPool(newHost);
124         assertEquals(oldSize, adapter.getPool().size());
125         assertFalse(adapter.getPool().contains(newHost));
126
127     }
128
129 //    @Test
130     public void testRun() {
131         // Runoff any old data
132         List<String> result1 = adapter.getIncomingEvents();
133         assertNotNull(result1);
134
135         // Post new data
136         DummyObj data = new DummyObj();
137         data.key = "value";
138         adapter.postStatus(data.toJson());
139
140         // Wait to account for network delay
141         sleep(2000);
142
143         // Get data back
144         List<DummyObj> result2 = adapter.getIncomingEvents(DummyObj.class);
145         assertNotNull(result2);
146 //        assertEquals(1, result2.size());
147         assertEquals(data.toJson(), result2.get(0).toJson());
148     }
149
150     @JsonSerialize
151     public static class DummyObj implements Serializable {
152         @JsonProperty("request") // Call request for default filter
153         public String key;
154
155         public DummyObj() {
156         }
157
158         public String toJson() {
159             return String.format("{\"request\": \"%s\"}", key);
160         }
161     }
162
163     private void sleep(long ms) {
164         try {
165             Thread.sleep(ms);
166         } catch (Exception e) {
167             return;
168         }
169     }
170 }