diff options
Diffstat (limited to 'core/core-infrastructure/src/test')
6 files changed, 507 insertions, 0 deletions
diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java new file mode 100644 index 000000000..e18c327c0 --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.messaging; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageClient; +import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener; +import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageServer; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class EndToEndMessagingTest. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class EndToEndStringMessagingTest { + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(EndToEndStringMessagingTest.class); + + private WSStringMessageServer server; + private WSStringMessageClient client; + + private boolean finished = false; + + @Test + public void testEndToEndMessaging() throws MessagingException { + logger.debug("end to end messaging test starting . . ."); + server = new WSStringMessageServer(44441); + assertNotNull(server); + server.start(new WSStringServerMessageListener()); + + client = new WSStringMessageClient("localhost", 44441); + assertNotNull(client); + client.start(new WSStringClientMessageListener()); + + client.sendString("Hello, client here"); + + while (!finished) { + ThreadUtilities.sleep(50); + } + client.stop(); + + server.stop(); + logger.debug("end to end messaging test finished"); + } + + private class WSStringServerMessageListener implements WSStringMessageListener { + @Override + public void receiveString(final String stringMessage) { + logger.debug(stringMessage); + assertEquals("Hello, client here", stringMessage); + server.sendString("Hello back from server"); + } + } + + private class WSStringClientMessageListener implements WSStringMessageListener { + @Override + public void receiveString(final String stringMessage) { + logger.debug(stringMessage); + assertEquals("Hello back from server", stringMessage); + finished = true; + } + } +} diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java new file mode 100644 index 000000000..09fa62d59 --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.messaging; + +import static org.junit.Assert.assertNotNull; + +import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageListener; +import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageServer; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; + +public class StringTestServer { + private WSStringMessageServer server; + + public StringTestServer(final int port, long timeToLive) throws MessagingException { + System.out.println("StringTestServer starting on port " + port + " for " + timeToLive + " seconds . . ."); + server = new WSStringMessageServer(port); + assertNotNull(server); + server.start(new WSStringServerMessageListener()); + + System.out.println("StringTestServer started on port " + port + " for " + timeToLive + " seconds"); + + for (; timeToLive > 0; timeToLive--) { + ThreadUtilities.sleep(1000); + } + + server.stop(); + System.out.println("StringTestServer completed"); + } + + private class WSStringServerMessageListener implements WSStringMessageListener { + @Override + public void receiveString(final String stringMessage) { + System.out.println("Server received string \"" + stringMessage + "\""); + server.sendString("Server echoing back the message: \"" + stringMessage + "\""); + } + } + + public static void main(final String[] args) throws MessagingException { + if (args.length != 2) { + System.err.println("Usage: StringTestServer port timeToLive"); + return; + } + + int port = 0; + try { + port = Integer.parseInt(args[0]); + } catch (final Exception e) { + System.err.println("Usage: StringTestServer port timeToLive"); + e.printStackTrace(); + return; + } + + long timeToLive = 0; + try { + timeToLive = Long.parseLong(args[1]); + } catch (final Exception e) { + System.err.println("Usage: StringTestServer port timeToLive"); + e.printStackTrace(); + return; + } + + new StringTestServer(port, timeToLive); + + } +} diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/TestMessageListener.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/TestMessageListener.java new file mode 100644 index 000000000..9e7562b5e --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/TestMessageListener.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.messaging; + +import com.google.common.eventbus.Subscribe; + +import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The listener interface for receiving testMessage events. The class that is interested in processing a testMessage + * event implements this interface, and the object created with that class is registered with a component using the + * component's <code>addTestMessageListener</code> method. When the testMessage event occurs, that object's appropriate + * method is invoked. + * + */ +public abstract class TestMessageListener implements MessageListener<String> { + + /** The Constant logger. */ + private static final XLogger logger = XLoggerFactory.getXLogger(TestMessageListener.class); + + /** + * On command. + * + * @param data the data + */ + public abstract void onCommand(MessageBlock<String> data); + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(org.onap.policy.apex.core. + * infrastructure. messaging.impl.ws.data.Data) + */ + @Subscribe + @Override + public final void onMessage(final MessageBlock<String> data) { + if (data != null) { + if (logger.isDebugEnabled()) { + logger.debug("{} command recieved from machine {} ", data.getMessages().size(), + data.getConnection().getRemoteSocketAddress().getHostString()); + } + onCommand(data); + } + } +} diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java new file mode 100644 index 000000000..e570ae0c7 --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.threading; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class ThreadingTest. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ThreadingTest { + + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTest.class); + + /** + * Test thread factory initialization. + */ + @Test + public void testThreadFactoryInitialization() { + final ApplicationThreadFactory threadFactory0 = new ApplicationThreadFactory("localName", 0); + assertNotNull("Failed to create ApplicationThreadFactory threadFactory0", threadFactory0); + logger.debug(threadFactory0.toString()); + assertTrue("Failed to name ApplicationThreadFactory threadFactory0", + threadFactory0.getName().startsWith("Apex-localName")); + final ApplicationThreadFactory threadFactory1 = new ApplicationThreadFactory("localName", 0); + assertNotNull("Failed to create ApplicationThreadFactory threadFactory1", threadFactory1); + logger.debug(threadFactory1.toString()); + assertTrue("Failed to name ApplicationThreadFactory threadFactory1", + threadFactory1.getName().startsWith("Apex-localName")); + + testThreadFactory(threadFactory0, 0); + testThreadFactory(threadFactory1, 1); + } + + /** + * Test thread factory. + * + * @param threadFactory the thread factory + * @param factoryId the factory id + */ + private void testThreadFactory(final ApplicationThreadFactory threadFactory, final int factoryId) { + final List<ThreadingTestThread> threadList = new ArrayList<ThreadingTestThread>(); + + for (int i = 0; i < 5; i++) { + threadList.add(new ThreadingTestThread()); + threadList.get(i).setThread(threadFactory.newThread(threadList.get(i))); + assertTrue(threadList.get(i).getName().startsWith("Apex-localName")); + assertTrue(threadList.get(i).getName().contains(":" + i)); + threadList.get(i).getThread().start(); + } + + // Threads should need a little more than 300ms to count to 3 + ThreadUtilities.sleep(380); + + for (int i = 0; i < 5; i++) { + threadList.get(i).interrupt(); + } + + for (int i = 0; i < 5; i++) { + assertTrue("Thread (" + i + ") failed to get count (" + threadList.get(i).getCounter() + ") up to 3", + threadList.get(i).getCounter() == 3); + } + } +} diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java new file mode 100644 index 000000000..195072886 --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.threading; + +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class ThreadingTestThread. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ThreadingTestThread implements Runnable { + + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTestThread.class); + + private boolean interrupted = false; + + private long counter = -1; + + private Thread thread = null; + + /** + * Sets the thread. + * + * @param thread the new thread + */ + public void setThread(final Thread thread) { + this.thread = thread; + } + + /** + * Gets the thread. + * + * @return the thread + */ + public Thread getThread() { + return thread; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + if (logger.isDebugEnabled()) { + logger.debug("starting threading test thread \"" + thread.getName() + "\" . . ."); + } + + while (!interrupted) { + counter++; + if (logger.isDebugEnabled()) { + logger.debug("in threading test thread \"" + thread.getName() + "\", counter=" + counter + " . . ."); + } + + if (!ThreadUtilities.sleep(100)) { + interrupted = true; + } + } + if (logger.isDebugEnabled()) { + logger.debug("stopped threading test thread \"" + thread.getName() + "\""); + } + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return thread.getName(); + } + + /** + * Interrupt. + */ + public void interrupt() { + interrupted = true; + } + + /** + * Gets the counter. + * + * @return the counter + */ + public Long getCounter() { + return counter; + } +} diff --git a/core/core-infrastructure/src/test/resources/logback-test.xml b/core/core-infrastructure/src/test/resources/logback-test.xml new file mode 100644 index 000000000..034511335 --- /dev/null +++ b/core/core-infrastructure/src/test/resources/logback-test.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2016-2018 Ericsson. All rights reserved. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> + +<configuration> + + <contextName>Apex</contextName> + <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> + <property name="LOG_DIR" value="${java.io.tmpdir}/apex_logging/" /> + + <!-- USE FOR STD OUT ONLY --> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <Pattern>%d %contextName [%t] %level %logger{36} - %msg%n</Pattern> + </encoder> + </appender> + + <root level="INFO"> + <appender-ref ref="STDOUT" /> + </root> + + <logger name="org.infinispan" level="INFO" additivity="false"> + <appender-ref ref="STDOUT" /> + </logger> + + <logger name="org.apache.zookeeper.ClientCnxn" level="OFF" additivity="false"> + <appender-ref ref="STDOUT" /> + </logger> + + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <file>${LOG_DIR}/apex.log</file> + <encoder> + <pattern>%d %-5relative [procId=${processId}] [%thread] %-5level + %logger{26} - %msg %n %ex{full}</pattern> + </encoder> + </appender> + + <appender name="CTXT_FILE" class="ch.qos.logback.core.FileAppender"> + <file>${LOG_DIR}/apex_ctxt.log</file> + <encoder> + <pattern>%d %-5relative [procId=${processId}] [%thread] %-5level + %logger{26} - %msg %n %ex{full}</pattern> + </encoder> + </appender> + + <logger name="org.onap.policy.apex.core.context.impl.monitoring" level="TRACE" additivity="false"> + <appender-ref ref="CTXT_FILE" /> + </logger> + + <logger name="org.onap.policy.apex.core.context" level="INFO" additivity="false"> + <appender-ref ref="STDOUT" /> + </logger> +</configuration> |