da40353684c2673f4927541efd9dc4ddc9aee476
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / test / Rfc8040RestConfWiringTest.java
1 /*
2  * Copyright (c) 2019 Red Hat, 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.restconf.nb.rfc8040.test;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import com.google.inject.AbstractModule;
14 import com.google.inject.Provides;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
22 import org.opendaylight.aaa.web.WebServer;
23 import org.opendaylight.aaa.web.testutils.TestWebClient;
24 import org.opendaylight.aaa.web.testutils.WebTestModule;
25 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
26 import org.opendaylight.controller.config.threadpool.util.ScheduledThreadPoolWrapper;
27 import org.opendaylight.infrautils.inject.guice.testutils.AnnotationsModule;
28 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
29 import org.opendaylight.restconf.nb.rfc8040.RestconfApplication;
30 import org.opendaylight.restconf.nb.rfc8040.Rfc8040RestConfWiring;
31 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
32 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.JSONRestconfServiceRfc8040Impl;
33 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapper;
34 import org.opendaylight.restconf.nb.rfc8040.streams.websockets.WebSocketConfiguration;
35 import org.opendaylight.restconf.nb.rfc8040.test.incubate.InMemoryMdsalModule;
36 import org.opendaylight.restconf.nb.rfc8040.web.WebInitializer;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
38
39 /**
40  * Tests if the {@link Rfc8040RestConfWiring} works.
41  *
42  * @author Michael Vorburger.ch
43  */
44 public class Rfc8040RestConfWiringTest {
45
46     public static class TestModule extends AbstractModule {
47
48         private static final WebSocketConfiguration SAMPLE_WEB_SOCKET_CONFIGURATION
49                 = new WebSocketConfiguration(8192, 30000, 5000);
50
51         @Override
52         protected void configure() {
53             bind(Rfc8040RestConfWiring.class).asEagerSingleton();
54             bind(RestconfApplication.class).asEagerSingleton();
55             bind(JSONRestconfServiceRfc8040Impl.class).asEagerSingleton();
56             bind(WebInitializer.class).asEagerSingleton();
57             bind(CustomFilterAdapterConfiguration.class).toInstance(listener -> { });
58             bind(WebSocketConfiguration.class).toInstance(SAMPLE_WEB_SOCKET_CONFIGURATION);
59         }
60
61         @Provides
62         @Singleton ServicesWrapper getServicesWrapper(final Rfc8040RestConfWiring wiring) {
63             return wiring.getServicesWrapper();
64         }
65
66         @Provides
67         @Singleton ScheduledThreadPool getScheduledThreadPool() {
68             return new ScheduledThreadPoolWrapper(8, new ThreadFactoryBuilder().build());
69         }
70     }
71
72     public @Rule GuiceRule guice = new GuiceRule(TestModule.class,
73             InMemoryMdsalModule.class, WebTestModule.class, AnnotationsModule.class);
74
75     @Inject WebServer webServer;
76     @Inject TestWebClient webClient;
77
78     @Inject SchemaContextProvider schemaContextProvider;
79     @Inject SchemaContextHandler schemaContextHandler;
80
81     @Test
82     public void testWiring() throws IOException, InterruptedException, URISyntaxException {
83         schemaContextHandler.onGlobalContextUpdated(schemaContextProvider.getSchemaContext());
84         assertEquals(200, webClient.request("GET", "/rests/yang-library-version").statusCode());
85     }
86 }