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