23a60834d01febbac5edadc0ba601c1ba13ed677
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / handlers / SchemaContextHandlerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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
9 package org.opendaylight.restconf.handlers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
17 import org.opendaylight.restconf.handlers.SchemaContextHandler;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19
20 /**
21  * Tests for handling {@link SchemaContext}
22  */
23 public class SchemaContextHandlerTest {
24
25     private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules";
26     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point";
27
28     private SchemaContextHandler schemaContextHandler;
29     private SchemaContext schemaContext;
30
31     @Before
32     public void setup() throws Exception {
33         this.schemaContextHandler = new SchemaContextHandler();
34
35         this.schemaContext = TestRestconfUtils.loadSchemaContext(PATH_FOR_ACTUAL_SCHEMA_CONTEXT);
36         this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
37     }
38
39     /**
40      * Testing init of {@link SchemaContextHandler}
41      */
42     @Test
43     public void schemaContextHandlerImplInitTest() {
44         assertNotNull("Handler should be created and not null", this.schemaContextHandler);
45     }
46
47     /**
48      * Test getting actual {@link SchemaContext}.
49      * <p>
50      * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
51      * <code>SchemaContext</code>.
52      */
53     @Test
54     public void getSchemaContextTest() {
55         assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
56                 this.schemaContext, this.schemaContextHandler.get());
57     }
58
59     /**
60      * Test updating of {@link SchemaContext}.
61      * <p>
62      * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
63      * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
64      */
65     @Test
66     public void onGlobalContextUpdateTest() throws Exception {
67         // create new SchemaContext and update SchemaContextHandler
68         final SchemaContext newSchemaContext = TestRestconfUtils.loadSchemaContext(PATH_FOR_NEW_SCHEMA_CONTEXT);
69         this.schemaContextHandler.onGlobalContextUpdated(newSchemaContext);
70
71         assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
72                 this.schemaContext, this.schemaContextHandler.get());
73         assertEquals("SchemaContextHandler should has reference to new SchemaContext",
74                 newSchemaContext, this.schemaContextHandler.get());
75     }
76 }