ce3c61eaf0acd8d8d46494a729492e6edbc40788
[yangtools.git] / restconf / restconf-client-impl / src / test / java / org / opendaylight / yangtools / restconf / client / RestconfClientImplTest.java
1 /*
2  * Copyright (c) 2013 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.yangtools.restconf.client;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.util.Iterator;
14 import java.util.Set;
15 import java.util.concurrent.ExecutionException;
16 import javassist.ClassPool;
17 import org.junit.Before;
18 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
19 import org.opendaylight.yangtools.restconf.client.api.UnsupportedProtocolException;
20 import org.opendaylight.yangtools.restconf.client.api.data.ConfigurationDatastore;
21 import org.opendaylight.yangtools.restconf.client.api.data.OperationalDatastore;
22 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
23 import org.opendaylight.yangtools.restconf.client.api.event.ListenableEventStreamContext;
24 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
25 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
26 import org.opendaylight.yangtools.yang.binding.RpcService;
27 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.fail;
32
33 public class RestconfClientImplTest {
34
35     private static final Logger logger = LoggerFactory.getLogger(RestconfClientImplTest.class);
36     private static final String restconfUrl = "http://localhost:8080";
37     //private static final String restconfUrl = "http://pce-guest35.cisco.com:9080";
38     public static final String JSON = "+json";
39     public static final String XML = "+xml";
40     private  RestconfClientContext restconfClientContext;
41     private RuntimeGeneratedMappingServiceImpl mappingService;
42
43
44     @Before
45     public void setupRestconfClientContext() throws MalformedURLException, UnsupportedProtocolException {
46         mappingService = new RuntimeGeneratedMappingServiceImpl();
47         mappingService.setPool(new ClassPool());
48         mappingService.init();
49
50         final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
51         moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
52         mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
53         this.restconfClientContext = new RestconfClientFactory().getRestconfClientContext(new URL(restconfUrl),mappingService, mappingService);
54         assertNotNull(this.restconfClientContext);
55     }
56
57 //    @Test
58     public void testGetAvailableEventStreams(){
59         ListenableFuture<Set<EventStreamInfo>> streamsFuture = restconfClientContext.getAvailableEventStreams();
60         while (!streamsFuture.isDone()){
61             //noop
62         }
63         if (streamsFuture.isDone()){
64             try {
65                 Set<EventStreamInfo> streams = (Set<EventStreamInfo>) streamsFuture.get();
66                 assertNotNull(streams);
67             } catch (InterruptedException e) {
68                 fail(e.getMessage());
69             } catch (ExecutionException e) {
70                 fail(e.getMessage());
71             }
72         }
73     }
74 //    @Test
75     public void testGetRpcServices(){
76         ListenableFuture<Set<Class<? extends RpcService>>> servicesFuture = restconfClientContext.getRpcServices();
77         while (!servicesFuture.isDone()){
78             //noop
79         }
80         if (servicesFuture.isDone()){
81             try {
82                 Set<Class<? extends RpcService>> streams = (Set<Class<? extends RpcService>>) servicesFuture.get();
83                 assertNotNull(streams);
84             } catch (InterruptedException e) {
85                 fail(e.getMessage());
86             } catch (ExecutionException e) {
87                 fail(e.getMessage());
88             }
89         }
90     }
91
92 //    @Test
93     public void getEventStreamContext() throws MalformedURLException, UnsupportedProtocolException, ExecutionException, InterruptedException {
94         ListenableFuture<Set<EventStreamInfo>>  evtStreams = restconfClientContext.getAvailableEventStreams();
95         while (!evtStreams.isDone()){
96             //noop
97         }
98
99         Iterator<EventStreamInfo> it = evtStreams.get().iterator();
100         ListenableEventStreamContext evtStreamCtx = restconfClientContext.getEventStreamContext(it.next());
101         assertNotNull(evtStreamCtx);
102     }
103 //    @Test
104     public void testGetOperationalDatastore() throws ExecutionException, InterruptedException, MalformedURLException, UnsupportedProtocolException {
105         OperationalDatastore datastore = restconfClientContext.getOperationalDatastore();
106         assertNotNull(datastore);
107
108     }
109 //    @Test
110     public void testGetConfigurationDatastore() throws ExecutionException, InterruptedException, MalformedURLException, UnsupportedProtocolException {
111         ConfigurationDatastore datastore = restconfClientContext.getConfigurationDatastore();
112         assertNotNull(datastore);
113     }
114
115
116 }