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