2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.impl;
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;
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;
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;
47 import com.fasterxml.jackson.annotation.JsonProperty;
48 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
51 * Test the ProviderAdapter implementation.
55 public class TestEventHandler {
57 private ListenerProperties prop;
59 private EventHandler adapter;
61 private static final String PROP_FILE = "/org/onap/appc/default.properties";
63 private static final String MESSAGE_FILE = "/DCAEResponse.txt";
66 * Setup the test environment.
68 * @throws NoSuchMethodException
69 * @throws SecurityException
70 * @throws NoSuchFieldException
74 Properties allProps = new Properties();
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);
82 adapter = new EventHandlerImpl(prop);
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());
92 String hostStr = prop.getProperty("poolMembers");
93 int hostCount = hostStr.length()>0 ? hostStr.split(",").length : 0;
94 assertEquals(hostCount, adapter.getPool().size());
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";
106 adapter.setReadTopic(readTopic);
107 assertEquals(readTopic, adapter.getReadTopic());
109 adapter.setWriteTopics(writeTopic);
110 assertEquals(writeTopic, adapter.getWriteTopics());
112 adapter.setClientName(clientName);
113 assertEquals(clientName, adapter.getClientName());
115 adapter.setClientId(clientId);
116 assertEquals(clientId, adapter.getClientId());
118 adapter.setCredentials("fake", "secret");
119 adapter.clearCredentials();
121 int oldSize = adapter.getPool().size();
122 adapter.addToPool(newHost);
123 assertEquals(oldSize + 1, adapter.getPool().size());
124 assertTrue(adapter.getPool().contains(newHost));
126 adapter.removeFromPool(newHost);
127 assertEquals(oldSize, adapter.getPool().size());
128 assertFalse(adapter.getPool().contains(newHost));
133 public void testRun() {
134 // Runoff any old data
135 List<String> result1 = adapter.getIncomingEvents();
136 assertNotNull(result1);
139 DummyObj data = new DummyObj();
141 adapter.postStatus(data.toJson());
143 // Wait to account for network delay
147 List<DummyObj> result2 = adapter.getIncomingEvents(DummyObj.class);
148 assertNotNull(result2);
149 // assertEquals(1, result2.size());
150 assertEquals(data.toJson(), result2.get(0).toJson());
154 public static class DummyObj implements Serializable {
155 @JsonProperty("request") // Call request for default filter
161 public String toJson() {
162 return String.format("{\"request\": \"%s\"}", key);
166 private void sleep(long ms) {
169 } catch (Exception e) {