BUG-2637: migrate features
[controller.git] / opendaylight / md-sal / samples / toaster-it / src / test / java / org / opendaylight / controller / sample / toaster / it / ToasterTest.java
index 4b4cb48b6332128a2e050ccf8a6c7d0288bf2aec..93b398f5b14ca807435622d74bd94ebb38175338 100644 (file)
+/*
+ * 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 javax.inject.Inject;
-
+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.sample.toaster.provider.api.ToastConsumer;
-import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
+import org.opendaylight.controller.sample.kitchen.api.EggsType;
+import org.opendaylight.controller.sample.kitchen.api.KitchenService;
+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 javax.inject.Inject;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import java.lang.management.ManagementFactory;
+
+import static org.junit.Assert.assertEquals;
+import static org.opendaylight.controller.test.sal.binding.it.TestHelper.baseModelBundles;
+import static org.opendaylight.controller.test.sal.binding.it.TestHelper.bindingAwareSalBundles;
+import static org.opendaylight.controller.test.sal.binding.it.TestHelper.configMinumumBundles;
+import static org.opendaylight.controller.test.sal.binding.it.TestHelper.junitAndMockitoBundles;
+import static org.opendaylight.controller.test.sal.binding.it.TestHelper.mdSalCoreBundles;
+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;
 
 @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";
+    @Inject
+    @Filter(timeout=60*1000)
+    KitchenService kitchenService;
 
-    @Test
-    public void properInitialized() throws Exception {
+    @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"),
 
-        Collection<ServiceReference<ToasterService>> references = ctx.getServiceReferences(ToasterService.class, null);
-        assertEquals(2, references.size());
+                toasterBundles(),
+                mdSalCoreBundles(),
 
-        consumer.createToast(WhiteBread.class, 5);
+                bindingAwareSalBundles(),
+                configMinumumBundles(),
+                // BASE Models
+                baseModelBundles(),
 
+                // Set fail if unresolved bundle present
+                systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
+                junitAndMockitoBundles());
     }
 
-    @Inject
-    BindingAwareBroker broker;
+    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(),
+                mavenBundle("org.openexi", "nagasena").versionAsInProject(),
+                mavenBundle("org.openexi", "nagasena-rta").versionAsInProject()
+        );
+    }
 
-    @Inject
-    ToastConsumer consumer;
+    @Test
+    public void testToaster() throws Exception {
 
-    @Inject
-    BundleContext ctx;
+        MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
+        ObjectName providerOn = new ObjectName("org.opendaylight.controller:instanceName=toaster-provider-impl,type=RuntimeBean,moduleFactoryName=toaster-provider-impl");
 
-    @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-api").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, "concepts").versionAsInProject(),
-                mavenBundle(YANG, "yang-binding").versionAsInProject(), //
-                mavenBundle(YANG, "yang-common").versionAsInProject(), //
-                mavenBundle("com.google.guava", "guava").versionAsInProject(), //
-                mavenBundle("org.javassist", "javassist").versionAsInProject(),
-                junitBundles()
-                );
+        long toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
+        assertEquals(0, toastsMade);
+
+        boolean success = true;
+
+        // Make toasts using OSGi service
+        success &= kitchenService.makeBreakfast( EggsType.SCRAMBLED, HashBrown.class, 4).get().isSuccessful();
+        success &= kitchenService.makeBreakfast( EggsType.POACHED, WhiteBread.class, 8 ).get().isSuccessful();
+
+        Assert.assertTrue("Not all breakfasts succeeded", success);
+
+        // Verify toasts made count on provider via JMX/config-subsystem
+        toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
+        assertEquals(2, toastsMade);
     }
 
 }