Bug 1112: Update toaster to use async best practices
[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 org.junit.Assert;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.opendaylight.controller.sample.kitchen.api.EggsType;
14 import org.opendaylight.controller.sample.kitchen.api.KitchenService;
15 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.HashBrown;
16 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WhiteBread;
17 import org.ops4j.pax.exam.Configuration;
18 import org.ops4j.pax.exam.Option;
19 import org.ops4j.pax.exam.junit.PaxExam;
20 import org.ops4j.pax.exam.options.DefaultCompositeOption;
21 import org.ops4j.pax.exam.util.Filter;
22 import org.ops4j.pax.exam.util.PathUtils;
23
24 import javax.inject.Inject;
25 import javax.management.MBeanServer;
26 import javax.management.ObjectName;
27
28 import java.lang.management.ManagementFactory;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.baseModelBundles;
32 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.bindingAwareSalBundles;
33 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.configMinumumBundles;
34 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.flowCapableModelBundles;
35 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.junitAndMockitoBundles;
36 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.mdSalCoreBundles;
37 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
38 import static org.ops4j.pax.exam.CoreOptions.options;
39 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
40 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
41
42 @RunWith(PaxExam.class)
43 public class ToasterTest {
44
45     @Inject
46     @Filter(timeout=60*1000)
47     KitchenService kitchenService;
48
49     @Configuration
50     public Option[] config() {
51         return options(systemProperty("osgi.console").value("2401"), mavenBundle("org.slf4j", "slf4j-api")
52                 .versionAsInProject(), //
53                           mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(), //
54
55                                 systemProperty("logback.configurationFile").value(
56                         "file:" + PathUtils.getBaseDir()
57                                 + "/src/test/resources/logback.xml"),
58                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(), //
59                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(), //
60                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
61                 systemPackages("sun.nio.ch"),
62
63                 toasterBundles(),
64                 mdSalCoreBundles(),
65
66                 bindingAwareSalBundles(),
67                 configMinumumBundles(),
68                 // BASE Models
69                 baseModelBundles(),
70                 flowCapableModelBundles(),
71
72                 // Set fail if unresolved bundle present
73                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
74                 junitAndMockitoBundles());
75     }
76
77     private Option toasterBundles() {
78         return new DefaultCompositeOption(
79                 mavenBundle("org.opendaylight.controller.samples", "sample-toaster-provider").versionAsInProject(),
80                 mavenBundle("org.opendaylight.controller.samples", "sample-toaster-consumer").versionAsInProject(),
81                 mavenBundle("org.opendaylight.controller.samples", "sample-toaster").versionAsInProject(),
82                 mavenBundle("org.openexi", "nagasena").versionAsInProject(),
83                 mavenBundle("org.openexi", "nagasena-rta").versionAsInProject()
84         );
85     }
86
87     @Test
88     public void testToaster() throws Exception {
89
90         MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
91         ObjectName providerOn = new ObjectName("org.opendaylight.controller:instanceName=toaster-provider-impl,type=RuntimeBean,moduleFactoryName=toaster-provider-impl");
92
93         long toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
94         assertEquals(0, toastsMade);
95
96         boolean success = true;
97
98         // Make toasts using OSGi service
99         success &= kitchenService.makeBreakfast( EggsType.SCRAMBLED, HashBrown.class, 4).get().isSuccessful();
100         success &= kitchenService.makeBreakfast( EggsType.POACHED, WhiteBread.class, 8 ).get().isSuccessful();
101
102         Assert.assertTrue("Not all breakfasts succeeded", success);
103
104         // Verify toasts made count on provider via JMX/config-subsystem
105         toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
106         assertEquals(2, toastsMade);
107     }
108
109 }