Added tests for reading:
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ReadConfAndOperDataTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7
8 import java.io.FileNotFoundException;
9 import java.io.InputStream;
10 import java.io.UnsupportedEncodingException;
11 import java.net.URI;
12 import java.net.URLEncoder;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.concurrent.Future;
16 import java.util.logging.Level;
17 import java.util.logging.LogRecord;
18
19 import javax.ws.rs.client.Entity;
20 import javax.ws.rs.core.Application;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.glassfish.jersey.test.JerseyTest;
26 import org.glassfish.jersey.test.TestProperties;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
32 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41
42 import com.google.common.base.Charsets;
43
44 public class ReadConfAndOperDataTest extends JerseyTest {
45
46     private static ControllerContext controllerContext;
47     private static BrokerFacade brokerFacade;
48     private static RestconfImpl restconfImpl;
49     private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
50
51     @BeforeClass
52     public static void init() throws FileNotFoundException {
53         Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs")
54                 .getPath());
55         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
56         controllerContext = ControllerContext.getInstance();
57         controllerContext.setSchemas(schemaContext);
58         brokerFacade = mock(BrokerFacade.class);
59         restconfImpl = RestconfImpl.getInstance();
60         restconfImpl.setBroker(brokerFacade);
61         restconfImpl.setControllerContext(controllerContext);
62     }
63
64     @Before
65     public void logs() {
66         List<LogRecord> loggedRecords = getLoggedRecords();
67         for (LogRecord l : loggedRecords) {
68             System.out.println(l.getMessage());
69         }
70     }
71
72     @Test
73     public void testReadConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
74
75         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
76
77         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
78         CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
79         when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
80
81         Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
82         assertEquals(200, response.getStatus());
83         
84         uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
85         when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
86         
87         response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
88         assertEquals(404, response.getStatus());
89     }
90
91     @Test
92     public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException {
93         String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
94
95         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
96         CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
97         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
98
99         Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
100         assertEquals(200, response.getStatus());
101         
102         uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
103         when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
104         
105         response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
106         assertEquals(404, response.getStatus());
107     }
108
109     private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
110         return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
111     }
112
113     @Override
114     protected Application configure() {
115         enable(TestProperties.LOG_TRAFFIC);
116         enable(TestProperties.DUMP_ENTITY);
117         enable(TestProperties.RECORD_LOG_LEVEL);
118         set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
119
120         ResourceConfig resourceConfig = new ResourceConfig();
121         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
122                 XmlToCompositeNodeProvider.INSTANCE);
123         return resourceConfig;
124     }
125 }