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