+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
package org.opendaylight.controller.sample.toaster.it;
-import static org.junit.Assert.*;
-import static org.ops4j.pax.exam.CoreOptions.junitBundles;
-import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
-import static org.ops4j.pax.exam.CoreOptions.options;
-import static org.ops4j.pax.exam.CoreOptions.systemPackages;
-import static org.ops4j.pax.exam.CoreOptions.systemProperty;
-import static org.ops4j.pax.exam.CoreOptions.maven;
-
-import java.util.Collection;
+import static org.junit.Assert.assertEquals;
+import static org.opendaylight.controller.test.sal.binding.it.TestHelper.*;
+import static org.ops4j.pax.exam.CoreOptions.*;
import javax.inject.Inject;
+import javax.management.JMX;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sample.toaster.provider.ToasterProvider;
+import org.opendaylight.controller.config.yang.config.toaster_consumer.impl.ToasterConsumerRuntimeMXBean;
+import org.opendaylight.controller.config.yang.config.toaster_provider.impl.ToasterProviderRuntimeMXBean;
import org.opendaylight.controller.sample.toaster.provider.api.ToastConsumer;
-import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
+import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.HashBrown;
import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WhiteBread;
import org.ops4j.pax.exam.Configuration;
-import org.ops4j.pax.exam.CoreOptions;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
+import org.ops4j.pax.exam.options.DefaultCompositeOption;
+import org.ops4j.pax.exam.util.Filter;
+import org.ops4j.pax.exam.util.PathUtils;
+
+import java.lang.management.ManagementFactory;
@RunWith(PaxExam.class)
public class ToasterTest {
- public static final String ODL = "org.opendaylight.controller";
- public static final String YANG = "org.opendaylight.yangtools";
- public static final String SAMPLE = "org.opendaylight.controller.samples";
-
- @Test
- public void properInitialized() throws Exception {
-
- Collection<ServiceReference<ToasterService>> references = ctx
- .getServiceReferences(ToasterService.class, null);
- assertEquals(2, references.size());
-
- consumer.createToast(WhiteBread.class, 5);
-
- }
-
- @Inject
- BindingAwareBroker broker;
-
- @Inject
- ToastConsumer consumer;
-
- @Inject
- BundleContext ctx;
-
- @Configuration
- public Option[] config() {
- return options(systemProperty("osgi.console").value("2401"),
- mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
- mavenBundle("org.slf4j", "log4j-over-slf4j")
- .versionAsInProject(),
- mavenBundle("ch.qos.logback", "logback-core")
- .versionAsInProject(),
- mavenBundle("ch.qos.logback", "logback-classic")
- .versionAsInProject(),
- mavenBundle(ODL, "sal-binding-api").versionAsInProject(),
- mavenBundle(ODL, "sal-binding-broker-impl")
- .versionAsInProject(), mavenBundle(ODL, "sal-common")
- .versionAsInProject(),
- mavenBundle(ODL, "sal-common-util").versionAsInProject(),
- mavenBundle(SAMPLE, "sample-toaster").versionAsInProject(),
- mavenBundle(SAMPLE, "sample-toaster-consumer")
- .versionAsInProject(),
- mavenBundle(SAMPLE, "sample-toaster-provider")
- .versionAsInProject(),
- mavenBundle(YANG, "yang-binding").versionAsInProject(),
- mavenBundle(YANG, "yang-common").versionAsInProject(),
- mavenBundle("com.google.guava", "guava").versionAsInProject(),
- junitBundles(), mavenBundle("org.javassist", "javassist")
- .versionAsInProject());
- }
+ @Inject
+ @Filter(timeout=60*1000)
+ ToastConsumer toastConsumer;
+
+ @Configuration
+ public Option[] config() {
+ return options(systemProperty("osgi.console").value("2401"), mavenBundle("org.slf4j", "slf4j-api")
+ .versionAsInProject(), //
+ mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(), //
+
+ systemProperty("logback.configurationFile").value(
+ "file:" + PathUtils.getBaseDir()
+ + "/src/test/resources/logback.xml"),
+ mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(), //
+ mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(), //
+ systemProperty("osgi.bundles.defaultStartLevel").value("4"),
+ systemPackages("sun.nio.ch"),
+
+ toasterBundles(),
+ mdSalCoreBundles(),
+
+ bindingAwareSalBundles(),
+ configMinumumBundles(),
+ // BASE Models
+ baseModelBundles(),
+ flowCapableModelBundles(),
+
+ // Set fail if unresolved bundle present
+ systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
+ junitAndMockitoBundles());
+ }
+
+ private Option toasterBundles() {
+ return new DefaultCompositeOption(
+ mavenBundle("org.opendaylight.controller.samples", "sample-toaster-provider").versionAsInProject(),
+ mavenBundle("org.opendaylight.controller.samples", "sample-toaster-consumer").versionAsInProject(),
+ mavenBundle("org.opendaylight.controller.samples", "sample-toaster").versionAsInProject()
+ );
+ }
+
+ @Test
+ public void testToaster() throws Exception {
+
+ MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
+ ObjectName consumerOn = new ObjectName("org.opendaylight.controller:instanceName=toaster-consumer-impl,type=RuntimeBean,moduleFactoryName=toaster-consumer-impl");
+ ObjectName providerOn = new ObjectName("org.opendaylight.controller:instanceName=toaster-provider-impl,type=RuntimeBean,moduleFactoryName=toaster-provider-impl");
+
+ long toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
+ assertEquals(0, toastsMade);
+
+ boolean toasts = true;
+
+ // Make toasts using OSGi service
+ toasts &= toastConsumer.createToast(HashBrown.class, 4);
+ toasts &= toastConsumer.createToast(WhiteBread.class, 8);
+
+ // Make toast using JMX/config-subsystem
+ toasts &= (Boolean)platformMBeanServer.invoke(consumerOn, "makeHashBrownToast", new Object[]{4}, new String[]{Integer.class.getName()});
+
+ Assert.assertTrue("Not all toasts done by " + toastConsumer, toasts);
+
+ // Verify toasts made count on provider via JMX/config-subsystem
+ toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
+ assertEquals(3, toastsMade);
+ }
}