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