/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.restconf.nb.rfc8040.handlers; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import com.google.common.util.concurrent.CheckedFuture; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain; import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; /** * Tests for handling {@link SchemaContext}. */ public class SchemaContextHandlerTest { private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules"; private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point"; private SchemaContextHandler schemaContextHandler; private SchemaContext schemaContext; @Before public void setup() throws Exception { final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class); final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class); Mockito.when(txHandler.get()).thenReturn(domTx); final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class); Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx); final CheckedFuture checked = Mockito.mock(CheckedFuture.class); Mockito.when(wTx.submit()).thenReturn(checked); Mockito.when(checked.checkedGet()).thenReturn(null); this.schemaContextHandler = new SchemaContextHandler(txHandler); this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT)); this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext); } /** * Testing init of {@link SchemaContextHandler}. */ @Test public void schemaContextHandlerImplInitTest() { assertNotNull("Handler should be created and not null", this.schemaContextHandler); } /** * Test getting actual {@link SchemaContext}. * *

* Get SchemaContext from SchemaContextHandler and compare it to actual * SchemaContext. */ @Test public void getSchemaContextTest() { assertEquals("SchemaContextHandler should has reference to actual SchemaContext", this.schemaContext, this.schemaContextHandler.get()); } /** * Test updating of {@link SchemaContext}. * *

* Create new SchemaContext, set it to SchemaContextHandler and check if * SchemaContextHandler reference to new SchemaContext instead of old one. */ @Test public void onGlobalContextUpdateTest() throws Exception { // create new SchemaContext and update SchemaContextHandler final SchemaContext newSchemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)); this.schemaContextHandler.onGlobalContextUpdated(newSchemaContext); assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext", this.schemaContext, this.schemaContextHandler.get()); assertEquals("SchemaContextHandler should has reference to new SchemaContext", newSchemaContext, this.schemaContextHandler.get()); } }