8d01ed15c253c1d63d7b38a4a0badca5c0a3d61e
[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 com.google.common.collect.ImmutableSet;
14 import java.util.Set;
15 import org.junit.Before;
16 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
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.SchemaContext;
20
21 public abstract class AbstractSchemaAwareTest {
22     private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
23             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
24                 @Override
25                 public SchemaContext load(final Set<YangModuleInfo> key) {
26                     final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
27                     moduleContext.addModuleInfos(key);
28                     return moduleContext.tryToCreateSchemaContext().get();
29                 }
30             });
31
32     @Before
33     public final void setup() throws Exception {
34         setupWithSchema(getSchemaContext());
35     }
36
37     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
38         return BindingReflections.cacheModuleInfos(Thread.currentThread().getContextClassLoader());
39     }
40
41     protected SchemaContext getSchemaContext() throws Exception {
42         // ImmutableSet guarantees non-null
43         return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
44     }
45
46     /**
47      * Setups test with Schema context.
48      *
49      * @param context schema context
50      */
51     protected abstract void setupWithSchema(SchemaContext context);
52 }