2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.listener.impl;
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;
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;
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;
44 import com.fasterxml.jackson.annotation.JsonProperty;
45 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
48 * Test the ProviderAdapter implementation.
52 public class TestEventHandler {
54 private ListenerProperties prop;
56 private EventHandler adapter;
58 private static final String PROP_FILE = "/org/openecomp/appc/default.properties";
60 private static final String MESSAGE_FILE = "/DCAEResponse.txt";
63 * Setup the test environment.
65 * @throws NoSuchMethodException
66 * @throws SecurityException
67 * @throws NoSuchFieldException
71 Properties allProps = new Properties();
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);
79 adapter = new EventHandlerImpl(prop);
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());
89 String hostStr = prop.getProperty("poolMembers");
90 int hostCount = hostStr.length()>0 ? hostStr.split(",").length : 0;
91 assertEquals(hostCount, adapter.getPool().size());
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";
103 adapter.setReadTopic(readTopic);
104 assertEquals(readTopic, adapter.getReadTopic());
106 adapter.setWriteTopics(writeTopic);
107 assertEquals(writeTopic, adapter.getWriteTopics());
109 adapter.setClientName(clientName);
110 assertEquals(clientName, adapter.getClientName());
112 adapter.setClientId(clientId);
113 assertEquals(clientId, adapter.getClientId());
115 adapter.setCredentials("fake", "secret");
116 adapter.clearCredentials();
118 int oldSize = adapter.getPool().size();
119 adapter.addToPool(newHost);
120 assertEquals(oldSize + 1, adapter.getPool().size());
121 assertTrue(adapter.getPool().contains(newHost));
123 adapter.removeFromPool(newHost);
124 assertEquals(oldSize, adapter.getPool().size());
125 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) {