/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.data.impl.codec;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.Codec;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;

public final class TypeDefinitionAwareCodecTestHelper {
    private TypeDefinitionAwareCodecTestHelper() {
        throw new UnsupportedOperationException();
    }

    @SuppressWarnings("unchecked")
    public static <T> T getCodec(final TypeDefinition<?> def, final Class<T> clazz) {
        Object codec = TypeDefinitionAwareCodec.fromType(def);
        assertNotNull(codec);
        assertTrue(clazz.isAssignableFrom(codec.getClass()));
        return (T)codec;
    }

    public static void deserializeWithExpectedIllegalArgEx(final Codec<String, ?, IllegalArgumentException> codec,
            final @NonNull String param) {
        try {
            codec.deserialize(param);
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // Expected ...
        }
    }

    public static EnumTypeDefinition toEnumTypeDefinition(final String... enums) {
        final EnumerationTypeBuilder b = BaseTypes.enumerationTypeBuilder(mock(SchemaPath.class));
        int val = 0;
        for (String en : enums) {
            EnumTypeDefinition.EnumPair mockEnum = mock(EnumTypeDefinition.EnumPair.class);
            when(mockEnum.getName()).thenReturn(en);
            when(mockEnum.getValue()).thenReturn(val);
            b.addEnum(mockEnum);
            val++;
        }

        return b.build();
    }
}

@@ -381,34 +381,40 @@
 org.
-junit.Assert.assertNotNull
+hamcrest.CoreMatchers.instanceOf
 ;%0Aim
@@ -425,30 +425,40 @@
  static org.
-junit.
+hamcrest.Matcher
 Assert.asser
@@ -459,19 +459,19 @@
 .assertT
-rue
+hat
 ;%0Aimport
@@ -1190,43 +1190,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22unchecked%22)%0A
     
@@ -1284,14 +1284,38 @@
     
-Object
+TypeDefinitionAwareCodec%3C?, ?%3E
  cod
@@ -1377,82 +1377,36 @@
 sert
-NotNull(codec);%0A        assertTrue(clazz.isAssignableFrom(codec.getClass()
+That(codec, instanceOf(clazz
 ));%0A
@@ -1424,16 +1424,25 @@
 urn 
-(T)
+clazz.cast(
 codec
+)
 ;%0A  

Improve TypeDefinitionAwareCodecTestHelper.getCodec()