2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc.listener.impl;
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;
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;
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;
45 import com.fasterxml.jackson.annotation.JsonProperty;
46 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
49 * Test the ProviderAdapter implementation.
53 public class TestEventHandler {
55 private ListenerProperties prop;
57 private EventHandler adapter;
59 private static final String PROP_FILE = "/org/openecomp/appc/default.properties";
61 private static final String MESSAGE_FILE = "/DCAEResponse.txt";
64 * Setup the test environment.
66 * @throws NoSuchMethodException
67 * @throws SecurityException
68 * @throws NoSuchFieldException
72 Properties allProps = new Properties();
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);
80 adapter = new EventHandlerImpl(prop);
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());
90 String hostStr = prop.getProperty("poolMembers");
91 int hostCount = hostStr.length()>0 ? hostStr.split(",").length : 0;
92 assertEquals(hostCount, adapter.getPool().size());
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";
104 adapter.setReadTopic(readTopic);
105 assertEquals(readTopic, adapter.getReadTopic());
107 adapter.setWriteTopics(writeTopic);
108 assertEquals(writeTopic, adapter.getWriteTopics());
110 adapter.setClientName(clientName);
111 assertEquals(clientName, adapter.getClientName());
113 adapter.setClientId(clientId);
114 assertEquals(clientId, adapter.getClientId());
116 adapter.setCredentials("fake", "secret");
117 adapter.clearCredentials();
119 int oldSize = adapter.getPool().size();
120 adapter.addToPool(newHost);
121 assertEquals(oldSize + 1, adapter.getPool().size());
122 assertTrue(adapter.getPool().contains(newHost));
124 adapter.removeFromPool(newHost);
125 assertEquals(oldSize, adapter.getPool().size());
126 assertFalse(adapter.getPool().contains(newHost));
131 public void testRun() {
132 // Runoff any old data
133 List<String> result1 = adapter.getIncomingEvents();
134 assertNotNull(result1);
137 DummyObj data = new DummyObj();
139 adapter.postStatus(data.toJson());
141 // Wait to account for network delay
145 List<DummyObj> result2 = adapter.getIncomingEvents(DummyObj.class);
146 assertNotNull(result2);
147 // assertEquals(1, result2.size());
148 assertEquals(data.toJson(), result2.get(0).toJson());
152 public static class DummyObj implements Serializable {
153 @JsonProperty("request") // Call request for default filter
159 public String toJson() {
160 return String.format("{\"request\": \"%s\"}", key);
164 private void sleep(long ms) {
167 } catch (Exception e) {