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