b703a2b20bb3a19ec35948d7051f487ee6a62788
[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         Optional<Toaster> optional;
58         try (ReadTransaction readTx = broker.newReadOnlyTransaction()) {
59             optional = readTx.read(LogicalDatastoreType.OPERATIONAL, TOASTER_IID).get();
60         }
61         assertNotNull(optional);
62         assertTrue("Operational toaster not present", optional.isPresent());
63
64         Toaster toasterData = optional.get();
65
66         assertEquals(Toaster.ToasterStatus.Up, toasterData.getToasterStatus());
67         assertEquals(new DisplayString("Opendaylight"), toasterData.getToasterManufacturer());
68         assertEquals(new DisplayString("Model 1 - Binding Aware"), toasterData.getToasterModelNumber());
69
70         try (ReadTransaction readTx = broker.newReadOnlyTransaction()) {
71             Boolean configToaster = readTx.exists(LogicalDatastoreType.CONFIGURATION, TOASTER_IID).get();
72             assertFalse("Didn't expect config data for toaster.", configToaster);
73         }
74     }
75
76     @Test
77     @Ignore //ignored because it is not a test right now. Illustrative purposes only.
78     public void testSomething() throws Exception {
79         MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness(Uint32.valueOf(1))
80                 .setToasterToastType(WheatBread.VALUE).build();
81
82         // NOTE: In a real test we would want to override the Thread.sleep() to
83         // prevent our junit test
84         // for sleeping for a second...
85         Future<RpcResult<MakeToastOutput>> makeToast = toaster.makeToast(toastInput);
86
87         RpcResult<MakeToastOutput> rpcResult = makeToast.get();
88
89         assertNotNull(rpcResult);
90         assertTrue(rpcResult.isSuccessful());
91         // etc
92     }
93 }