e8341d46e66fb50841f0a323727f6c35da0a9767
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractSchemaAwareTest.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.adapter.test;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.util.Set;
14 import org.junit.Before;
15 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
16 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
17 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
18 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20
21 public abstract class AbstractSchemaAwareTest {
22     private static final LoadingCache<Set<YangModuleInfo>, BindingRuntimeContext> RUNTIME_CONTEXT_CACHE =
23             CacheBuilder.newBuilder().weakValues().build(
24                 new CacheLoader<Set<YangModuleInfo>, BindingRuntimeContext>() {
25                     @Override
26                     public BindingRuntimeContext load(final Set<YangModuleInfo> key) {
27                         return BindingRuntimeHelpers.createRuntimeContext(key);
28                     }
29                 });
30
31     @Before
32     public final void setup() throws Exception {
33         setupWithRuntimeContext(getRuntimeContext());
34     }
35
36     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
37         return BindingReflections.cacheModuleInfos(Thread.currentThread().getContextClassLoader());
38     }
39
40     protected BindingRuntimeContext getRuntimeContext() throws Exception {
41         return RUNTIME_CONTEXT_CACHE.getUnchecked(getModuleInfos());
42     }
43
44     protected EffectiveModelContext getSchemaContext() throws Exception {
45         return getRuntimeContext().getEffectiveModelContext();
46     }
47
48     protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
49         setupWithSchema(runtimeContext.getEffectiveModelContext());
50     }
51
52     /**
53      * Setups test with EffectiveModelContext.
54      *
55      * @param context schema context
56      */
57     protected void setupWithSchema(final EffectiveModelContext context) {
58         // No-op
59     }
60 }