9b05d53a542c594c2a7e9e3624194037deee9f29
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / EnumerationCodecTest.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.dom.codec.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13
14 import com.google.common.collect.ImmutableList;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.binding.Enumeration;
17 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
19
20 public class EnumerationCodecTest {
21
22     private enum TestEnum implements Enumeration {
23         ENUM;
24
25         @Override
26         public String getName() {
27             return "ENUM";
28         }
29
30         @Override
31         public int getIntValue() {
32             return 0;
33         }
34     }
35
36     @Test
37     public void basicTest() throws Exception {
38         final EnumPair pair = mock(EnumPair.class);
39         doReturn(TestEnum.ENUM.name()).when(pair).getName();
40         doReturn(0).when(pair).getValue();
41         EnumTypeDefinition definition = mock(EnumTypeDefinition.class);
42         doReturn(ImmutableList.of(pair)).when(definition).getValues();
43
44         final EnumerationCodec codec = EnumerationCodec.loader(TestEnum.class, definition).call();
45         assertEquals(codec.deserialize(codec.serialize(TestEnum.ENUM)), TestEnum.ENUM);
46         assertEquals(codec.serialize(codec.deserialize(TestEnum.ENUM.name())), TestEnum.ENUM.name());
47     }
48 }