1f9ba590278a8d9b9f4ab6158806de0ec8502140
[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.spi.BindingRuntimeHelpers;
16 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
17 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19
20 public abstract class AbstractSchemaAwareTest {
21     private static final LoadingCache<Set<YangModuleInfo>, EffectiveModelContext> SCHEMA_CONTEXT_CACHE =
22             CacheBuilder.newBuilder().weakValues().build(
23                 new CacheLoader<Set<YangModuleInfo>, EffectiveModelContext>() {
24                     @Override
25                     public EffectiveModelContext load(final Set<YangModuleInfo> key) {
26                         return BindingRuntimeHelpers.createEffectiveModel(key);
27                     }
28                 });
29
30     @Before
31     public final void setup() throws Exception {
32         setupWithSchema(getSchemaContext());
33     }
34
35     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
36         return BindingReflections.cacheModuleInfos(Thread.currentThread().getContextClassLoader());
37     }
38
39     protected EffectiveModelContext getSchemaContext() throws Exception {
40         return SCHEMA_CONTEXT_CACHE.getUnchecked(getModuleInfos());
41     }
42
43     /**
44      * Setups test with Schema context.
45      *
46      * @param context schema context
47      */
48     protected abstract void setupWithSchema(EffectiveModelContext context);
49 }