1b6dc3cc2c1413fae485dd08f0c3d1bfde80e0e4
[controller.git] / opendaylight / md-sal / samples / toaster-provider / src / test / java / org / opendaylight / controller / sample / toaster / provider / OpenDaylightToasterTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.controller.sample.toaster.provider;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.mock;
16
17 import java.util.Optional;
18 import java.util.concurrent.Future;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.mdsal.binding.api.ReadTransaction;
25 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString;
28 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
29 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder;
30 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutput;
31 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster;
32 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.Uint32;
36
37 public class OpenDaylightToasterTest extends AbstractConcurrentDataBrokerTest {
38
39     private static final InstanceIdentifier<Toaster> TOASTER_IID = InstanceIdentifier.builder(Toaster.class).build();
40     private OpendaylightToaster toaster;
41
42     @Before
43     public void setupToaster() {
44         toaster = new OpendaylightToaster();
45         toaster.setDataBroker(getDataBroker());
46         toaster.init();
47
48         // We'll mock the NotificationProviderService.
49         NotificationPublishService mockNotification = mock(NotificationPublishService.class);
50         toaster.setNotificationProvider(mockNotification);
51     }
52
53     @Test
54     public void testToasterInitOnStartUp() throws Exception {
55         DataBroker broker = getDataBroker();
56
57         ReadTransaction readTx = broker.newReadOnlyTransaction();
58         Optional<Toaster> optional = readTx.read(LogicalDatastoreType.OPERATIONAL, TOASTER_IID).get();
59         assertNotNull(optional);
60         assertTrue("Operational toaster not present", optional.isPresent());
61
62         Toaster toasterData = optional.get();
63
64         assertEquals(Toaster.ToasterStatus.Up, toasterData.getToasterStatus());
65         assertEquals(new DisplayString("Opendaylight"), toasterData.getToasterManufacturer());
66         assertEquals(new DisplayString("Model 1 - Binding Aware"), toasterData.getToasterModelNumber());
67
68         Optional<Toaster> configToaster = readTx.read(LogicalDatastoreType.CONFIGURATION, TOASTER_IID).get();
69         assertFalse("Didn't expect config data for toaster.", configToaster.isPresent());
70     }
71
72     @Test
73     @Ignore //ignored because it is not a test right now. Illustrative purposes only.
74     public void testSomething() throws Exception {
75         MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness(Uint32.valueOf(1))
76                 .setToasterToastType(WheatBread.class).build();
77
78         // NOTE: In a real test we would want to override the Thread.sleep() to
79         // prevent our junit test
80         // for sleeping for a second...
81         Future<RpcResult<MakeToastOutput>> makeToast = toaster.makeToast(toastInput);
82
83         RpcResult<MakeToastOutput> rpcResult = makeToast.get();
84
85         assertNotNull(rpcResult);
86         assertTrue(rpcResult.isSuccessful());
87         // etc
88     }
89 }