Merge "Add filtering capability to config.ini in order to reference logging bridge...
[controller.git] / opendaylight / md-sal / samples / toaster-it / src / test / java / org / opendaylight / controller / sample / toaster / it / ToasterTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sample.toaster.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.*;
12 import static org.ops4j.pax.exam.CoreOptions.*;
13
14 import javax.inject.Inject;
15 import javax.management.JMX;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.opendaylight.controller.config.yang.config.toaster_consumer.impl.ToasterConsumerRuntimeMXBean;
23 import org.opendaylight.controller.config.yang.config.toaster_provider.impl.ToasterProviderRuntimeMXBean;
24 import org.opendaylight.controller.sample.toaster.provider.api.ToastConsumer;
25 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.HashBrown;
26 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WhiteBread;
27 import org.ops4j.pax.exam.Configuration;
28 import org.ops4j.pax.exam.Option;
29 import org.ops4j.pax.exam.junit.PaxExam;
30 import org.ops4j.pax.exam.options.DefaultCompositeOption;
31 import org.ops4j.pax.exam.util.Filter;
32 import org.ops4j.pax.exam.util.PathUtils;
33
34 import java.lang.management.ManagementFactory;
35
36 @RunWith(PaxExam.class)
37 public class ToasterTest {
38
39     @Inject
40     @Filter(timeout=60*1000)
41     ToastConsumer toastConsumer;
42
43     @Configuration
44     public Option[] config() {
45         return options(systemProperty("osgi.console").value("2401"), mavenBundle("org.slf4j", "slf4j-api")
46                 .versionAsInProject(), //
47                           mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(), //
48
49                                 systemProperty("logback.configurationFile").value(
50                         "file:" + PathUtils.getBaseDir()
51                                 + "/src/test/resources/logback.xml"),
52                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(), //
53                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(), //
54                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
55                 systemPackages("sun.nio.ch"),
56
57                 toasterBundles(),
58                 mdSalCoreBundles(),
59
60                 bindingAwareSalBundles(),
61                 configMinumumBundles(),
62                 // BASE Models
63                 baseModelBundles(),
64                 flowCapableModelBundles(),
65
66                 // Set fail if unresolved bundle present
67                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
68                 junitAndMockitoBundles());
69     }
70
71     private Option toasterBundles() {
72         return new DefaultCompositeOption(
73                 mavenBundle("org.opendaylight.controller.samples", "sample-toaster-provider").versionAsInProject(),
74                 mavenBundle("org.opendaylight.controller.samples", "sample-toaster-consumer").versionAsInProject(),
75                 mavenBundle("org.opendaylight.controller.samples", "sample-toaster").versionAsInProject()
76         );
77     }
78
79     @Test
80     public void testToaster() throws Exception {
81
82         MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
83         ObjectName consumerOn = new ObjectName("org.opendaylight.controller:instanceName=toaster-consumer-impl,type=RuntimeBean,moduleFactoryName=toaster-consumer-impl");
84         ObjectName providerOn = new ObjectName("org.opendaylight.controller:instanceName=toaster-provider-impl,type=RuntimeBean,moduleFactoryName=toaster-provider-impl");
85
86         long toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
87         assertEquals(0, toastsMade);
88
89         boolean toasts = true;
90
91         // Make toasts using OSGi service
92         toasts &= toastConsumer.createToast(HashBrown.class, 4);
93         toasts &= toastConsumer.createToast(WhiteBread.class, 8);
94
95         // Make toast using JMX/config-subsystem
96         toasts &= (Boolean)platformMBeanServer.invoke(consumerOn, "makeHashBrownToast", new Object[]{4}, new String[]{Integer.class.getName()});
97
98         Assert.assertTrue("Not all toasts done by " + toastConsumer, toasts);
99
100         // Verify toasts made count on provider via JMX/config-subsystem
101         toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
102         assertEquals(3, toastsMade);
103     }
104
105 }