86d2e6d19c249dad322f0f8a94d5ecbd2b546264
[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.api.RpcProviderService;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString;
29 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
30 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder;
31 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutput;
32 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster;
33 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.Uint32;
37
38 public class OpenDaylightToasterTest extends AbstractConcurrentDataBrokerTest {
39
40     private static final InstanceIdentifier<Toaster> TOASTER_IID = InstanceIdentifier.builder(Toaster.class).build();
41     private OpendaylightToaster toaster;
42
43     @Before
44     public void setupToaster() {
45         toaster = new OpendaylightToaster(getDataBroker(), mock(NotificationPublishService.class),
46             mock(RpcProviderService.class));
47     }
48
49     @Test
50     public void testToasterInitOnStartUp() throws Exception {
51         DataBroker broker = getDataBroker();
52
53         Optional<Toaster> optional;
54         try (ReadTransaction readTx = broker.newReadOnlyTransaction()) {
55             optional = readTx.read(LogicalDatastoreType.OPERATIONAL, TOASTER_IID).get();
56         }
57         assertNotNull(optional);
58         assertTrue("Operational toaster not present", optional.isPresent());
59
60         Toaster toasterData = optional.orElseThrow();
61
62         assertEquals(Toaster.ToasterStatus.Up, toasterData.getToasterStatus());
63         assertEquals(new DisplayString("Opendaylight"), toasterData.getToasterManufacturer());
64         assertEquals(new DisplayString("Model 1 - Binding Aware"), toasterData.getToasterModelNumber());
65
66         try (ReadTransaction readTx = broker.newReadOnlyTransaction()) {
67             Boolean configToaster = readTx.exists(LogicalDatastoreType.CONFIGURATION, TOASTER_IID).get();
68             assertFalse("Didn't expect config data for toaster.", configToaster);
69         }
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.VALUE).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.invoke(toastInput);
82
83         RpcResult<MakeToastOutput> rpcResult = makeToast.get();
84
85         assertNotNull(rpcResult);
86         assertTrue(rpcResult.isSuccessful());
87         // etc
88     }
89 }