b37d713ad8f2977568b4ca0489d8acdcd1a1b5a3
[mdsal.git] / binding / mdsal-binding-spec-util / src / test / java / org / opendaylight / mdsal / binding / spec / reflect / AugmentationFieldGetterTest.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.mdsal.binding.spec.reflect;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
15 import static org.opendaylight.mdsal.binding.spec.reflect.AugmentationFieldGetter.getGetter;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.binding.Augmentation;
21
22 public class AugmentationFieldGetterTest {
23
24     @Test
25     public void getGetterTest() throws Exception {
26         assertTrue(getGetter(Object.class).getAugmentations(null).isEmpty());
27         assertTrue(getGetter(TestAugmentationWrongTypeClass.class).getAugmentations(null).isEmpty());
28
29         final AugmentationFieldGetter augmentationFieldGetter = getGetter(TestAugmentationClass.class);
30         final Augmentation<?> augmentation = mock(Augmentation.class);
31         final TestAugmentationClass testAugmentationClass = new TestAugmentationClass();
32
33         testAugmentationClass.addAugmentation(augmentation, augmentation);
34         assertNotNull(augmentationFieldGetter.getAugmentations(testAugmentationClass));
35         assertEquals(1, augmentationFieldGetter.getAugmentations(testAugmentationClass).size());
36     }
37
38     @Test(expected = IllegalStateException.class)
39     public void getWrongGetterTest() throws Exception {
40         final AugmentationFieldGetter augmentationFieldGetter = getGetter(TestAugmentationClass.class);
41         augmentationFieldGetter.getAugmentations("");
42         fail("Expected IllegalStateException");
43     }
44
45     @Test
46     public void getNoGetterTest() throws Exception {
47         assertTrue(getGetter(Object.class).getAugmentations(null).isEmpty());
48     }
49
50     private final class TestAugmentationClass {
51         private final Map<Augmentation<?>, Augmentation<?>> augmentation = new HashMap<>();
52
53         void addAugmentation(final Augmentation<?> key, final Augmentation<?> value) {
54             augmentation.put(key, value);
55         }
56     }
57
58     private final class TestAugmentationWrongTypeClass {
59         private String augmentation;
60     }
61 }