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