From: Igor Foltin Date: Thu, 4 Aug 2016 14:03:37 +0000 (+0200) Subject: Bug 3899: Milestone: Increase test coverage for Yangtools X-Git-Tag: release/boron~18 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=7d396358270b8faa4cc74adac05f4d0c00fceb7d;hp=d126017e3d7bf8483d5e9a0cfb78d9c0a68763d7;p=yangtools.git Bug 3899: Milestone: Increase test coverage for Yangtools Added several unit tests. Change-Id: I67d1b4fa4f879d4af186e6c42c724d66de9ca5d2 Signed-off-by: Igor Foltin --- diff --git a/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/SemVerTest.java b/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/SemVerTest.java new file mode 100644 index 0000000000..6ddc906247 --- /dev/null +++ b/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/SemVerTest.java @@ -0,0 +1,55 @@ +/* + * 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.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class SemVerTest { + + @Test + public void testSemVer() { + final SemVer semVer = SemVer.create(5); + assertNotNull(semVer); + + assertEquals(5, semVer.getMajor()); + assertEquals(0, semVer.getMinor()); + assertEquals(0, semVer.getPatch()); + + final SemVer semVer2 = SemVer.valueOf("1.2.3"); + assertNotNull(semVer2); + + assertEquals(1, semVer2.getMajor()); + assertEquals(2, semVer2.getMinor()); + assertEquals(3, semVer2.getPatch()); + + final SemVer semVer3 = SemVer.valueOf("1"); + assertNotNull(semVer3); + + assertEquals(1, semVer3.getMajor()); + assertEquals(0, semVer3.getMinor()); + assertEquals(0, semVer3.getPatch()); + + assertEquals(1, semVer2.compareTo(semVer3)); + assertEquals(-1, semVer3.compareTo(semVer2)); + assertEquals(0, semVer2.compareTo(semVer2)); + + assertTrue(semVer2.equals(semVer2)); + assertFalse(semVer2.equals("not equal")); + assertFalse(semVer2.equals(semVer3)); + + assertEquals(semVer2.hashCode(), semVer2.hashCode()); + + assertEquals("1.0.0", semVer3.toString()); + } +} diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/UnmodifiableCollectionTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/UnmodifiableCollectionTest.java new file mode 100644 index 0000000000..af7d65beb9 --- /dev/null +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/UnmodifiableCollectionTest.java @@ -0,0 +1,70 @@ +/* + * 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.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.collect.UnmodifiableIterator; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import org.junit.Test; + +public class UnmodifiableCollectionTest { + + @Test + public void testUnmodifiableCollection() { + final List immutableTestList = ImmutableList.builder() + .add(1) + .add(2) + .add(3) + .add(4) + .add(5).build(); + + final Collection testUnmodifiableCollection = UnmodifiableCollection.create(immutableTestList); + assertNotNull(testUnmodifiableCollection); + + final List testList = Lists.newArrayList(); + testList.add(1); + testList.add(2); + testList.add(3); + testList.add(4); + testList.add(5); + + final Collection testUnmodifiableCollection2 = UnmodifiableCollection.create(testList); + + final Iterator iterator = testUnmodifiableCollection2.iterator(); + assertNotNull(iterator); + assertTrue(iterator instanceof UnmodifiableIterator); + + assertEquals(5, testUnmodifiableCollection2.size()); + + assertFalse(testUnmodifiableCollection2.isEmpty()); + + assertTrue(testUnmodifiableCollection2.contains(1)); + + final Object[] objectArray = testUnmodifiableCollection2.toArray(); + assertNotNull(objectArray); + assertEquals(5, objectArray.length); + + final Integer[] integerArray = testUnmodifiableCollection2.toArray( + new Integer[testUnmodifiableCollection2.size()]); + assertNotNull(integerArray); + assertEquals(5, integerArray.length); + + assertTrue(testUnmodifiableCollection2.containsAll(testUnmodifiableCollection)); + + assertEquals("UnmodifiableCollection{" + testList + "}", testUnmodifiableCollection2.toString()); + } +} diff --git a/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/stream/NormalizedNodeWriterTest.java b/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/stream/NormalizedNodeWriterTest.java new file mode 100644 index 0000000000..4ee8bb51fb --- /dev/null +++ b/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/stream/NormalizedNodeWriterTest.java @@ -0,0 +1,147 @@ +/* + * 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.api.schema.stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.text.ParseException; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode; +import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; +import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; +import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode; +import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode; +import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode; +import org.opendaylight.yangtools.yang.data.api.schema.YangModeledAnyXmlNode; + +public class NormalizedNodeWriterTest { + + private QNameModule bazModule; + + private QName myKeyedList; + private QName myKeyLeaf; + private QName myLeafList; + + @Before + public void setUp() throws URISyntaxException, ParseException, UnsupportedEncodingException { + bazModule = QNameModule.create(new URI("baz-namespace"), SimpleDateFormatUtil.getRevisionFormat() + .parse("1970-01-01")); + + myKeyedList = QName.create(bazModule, "my-keyed-list"); + myKeyLeaf = QName.create(bazModule, "my-key-leaf"); + myLeafList = QName.create(bazModule, "my-leaf-list"); + } + + @Test + public void testNormalizedNodeWriter() throws IOException { + final NormalizedNodeStreamWriter loggingNormalizedNodeStreamWriter = new LoggingNormalizedNodeStreamWriter(); + final NormalizedNodeWriter orderedNormalizedNodeWriter = NormalizedNodeWriter.forStreamWriter + (loggingNormalizedNodeStreamWriter); + + assertEquals(loggingNormalizedNodeStreamWriter, orderedNormalizedNodeWriter.getWriter()); + + final NormalizedNode mockedNormalizedNode = mock(NormalizedNode.class); + try { + orderedNormalizedNodeWriter.write(mockedNormalizedNode); + fail("An IllegalStateException should have been thrown!"); + } catch (IllegalStateException ex) { + assertTrue(ex.getMessage().startsWith("It wasn't possible to serialize node")); + } + + final NormalizedNode mockedLeafSetEntryNode = mock(LeafSetEntryNode.class); + doReturn(new NodeWithValue<>(myLeafList, "leaflist-value-1")).when(mockedLeafSetEntryNode).getIdentifier(); + doReturn("leaflist-value-1").when(mockedLeafSetEntryNode).getValue(); + assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafSetEntryNode)); + + final NormalizedNode mockedLeafNode = mock(LeafNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafNode)); + doReturn("leaf-value-1").when(mockedLeafNode).getValue(); + assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafNode)); + + final NormalizedNode mockedAnyXmlNode = mock(AnyXmlNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedAnyXmlNode)); + + final NormalizedNode mockedContainerNode = mock(ContainerNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedContainerNode)); + + final NormalizedNode mockedYangModeledAnyXmlNode = mock(YangModeledAnyXmlNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedYangModeledAnyXmlNode)); + + final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class); + doReturn(new NodeIdentifierWithPredicates(myKeyedList, myKeyLeaf, "list-key-value-1")) + .when(mockedMapEntryNode).getIdentifier(); + doReturn(Optional.absent()).when(mockedMapEntryNode).getChild(any(NodeIdentifier.class)); + assertNotNull(orderedNormalizedNodeWriter.write(mockedMapEntryNode)); + + final UnkeyedListEntryNode mockedUnkeyedListEntryNode = mock(UnkeyedListEntryNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedUnkeyedListEntryNode)); + + final ChoiceNode mockedChoiceNode = mock(ChoiceNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedChoiceNode)); + + final AugmentationNode mockedAugmentationNode = mock(AugmentationNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedAugmentationNode)); + + final UnkeyedListNode mockedUnkeyedListNode = mock(UnkeyedListNode.class); + final Set value = ImmutableSet.builder().add(mockedUnkeyedListEntryNode).build(); + doReturn(value).when(mockedUnkeyedListNode).getValue(); + assertNotNull(orderedNormalizedNodeWriter.write(mockedUnkeyedListNode)); + + final OrderedMapNode mockedOrderedMapNode = mock(OrderedMapNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedOrderedMapNode)); + + final MapNode mockedMapNode = mock(MapNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedMapNode)); + + final OrderedLeafSetNode mockedOrderedLeafSetNode = mock(OrderedLeafSetNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedOrderedLeafSetNode)); + + final LeafSetNode mockedLeafSetNode = mock(LeafSetNode.class); + assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafSetNode)); + + orderedNormalizedNodeWriter.flush(); + orderedNormalizedNodeWriter.close(); + + final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter + (loggingNormalizedNodeStreamWriter, false); + + assertNotNull(normalizedNodeWriter.write(mockedMapEntryNode)); + + normalizedNodeWriter.flush(); + normalizedNodeWriter.close(); + } +} diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/LengthConstraintImplTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/LengthConstraintImplTest.java new file mode 100644 index 0000000000..948613e33a --- /dev/null +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/LengthConstraintImplTest.java @@ -0,0 +1,57 @@ +/* + * 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.model.util; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.google.common.base.Optional; +import org.junit.Test; +import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; + +public class LengthConstraintImplTest { + + @Test + public void testLengthConstraint() { + LengthConstraint lengthConstraint = new LengthConstraintImpl(3, 5, Optional.of("test description"), + Optional.of("test reference")); + LengthConstraint lengthConstraint2 = new LengthConstraintImpl(3, 5, Optional.of("test description"), + Optional.of("test reference")); + assertTrue(lengthConstraint.equals(lengthConstraint2)); + + assertFalse(lengthConstraint.equals(null)); + assertFalse(lengthConstraint.equals(new Object())); + + lengthConstraint2 = new LengthConstraintImpl(3, 5, Optional.of("another test description"), + Optional.of("test reference")); + assertFalse(lengthConstraint.equals(lengthConstraint2)); + + lengthConstraint2 = new LengthConstraintImpl(3, 5, Optional.of("test description"), + Optional.of("another test reference")); + assertFalse(lengthConstraint.equals(lengthConstraint2)); + + lengthConstraint = new LengthConstraintImpl(3, 5, Optional.of("test description"), + Optional.of("test reference"), "error app-tag", "error message"); + lengthConstraint2 = new LengthConstraintImpl(2, 5, Optional.of("test description"), + Optional.of("test reference"), "error app-tag", "error message"); + assertFalse(lengthConstraint.equals(lengthConstraint2)); + + lengthConstraint2 = new LengthConstraintImpl(3, 6, Optional.of("test description"), + Optional.of("test reference"), "error app-tag", "error message"); + assertFalse(lengthConstraint.equals(lengthConstraint2)); + + lengthConstraint2 = new LengthConstraintImpl(3, 5, Optional.of("test description"), + Optional.of("test reference"), "another error app-tag", "error message"); + assertFalse(lengthConstraint.equals(lengthConstraint2)); + + lengthConstraint2 = new LengthConstraintImpl(3, 5, Optional.of("test description"), + Optional.of("test reference"), "error app-tag", "another error message"); + assertFalse(lengthConstraint.equals(lengthConstraint2)); + } +} diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/NumberUtilTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/NumberUtilTest.java new file mode 100644 index 0000000000..f6cdc85726 --- /dev/null +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/NumberUtilTest.java @@ -0,0 +1,130 @@ +/* + * 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.model.util.type; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.base.Function; +import java.math.BigDecimal; +import java.math.BigInteger; +import org.junit.Test; + +public class NumberUtilTest { + + @Test + public void testRangeCoveredForShort() { + final short min = 100; + final short superMin = 50; + final short max = 200; + final short superMax = 300; + + assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax)); + } + + @Test + public void testRangeCoveredForLong() { + final long min = 100L; + final long superMin = 50L; + final long max = 200L; + final long superMax = 300L; + + assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax)); + } + + @Test + public void testRangeCoveredForBigDecimal() { + final BigDecimal min = new BigDecimal(100.0); + final BigDecimal superMin = new BigDecimal(50.0); + final BigDecimal max = new BigDecimal(200.0); + final BigDecimal superMax = new BigDecimal(300.0); + + assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax)); + } + + @Test + public void testRangeCoveredForBigInteger() { + final BigInteger min = new BigInteger("100"); + final BigInteger superMin = new BigInteger("50"); + final BigInteger max = new BigInteger("200"); + final BigInteger superMax = new BigInteger("300"); + + assertTrue(NumberUtil.isRangeCovered(min, max, superMin, superMax)); + } + + @Test(expected = IllegalArgumentException.class) + public void testRangeCoveredForUnsupportedNumberType() { + final double min = 100.0; + final double superMin = 50.0; + final double max = 200.0; + final double superMax = 300.0; + + NumberUtil.isRangeCovered(min, max, superMin, superMax); + } + + @Test + public void testConverterToShort() { + final short shortNum = 20; + final Function numberFunction = NumberUtil.converterTo(Short.class); + assertEquals(shortNum, numberFunction.apply(shortNum)); + + final byte byteNum = 20; + assertEquals(shortNum, numberFunction.apply(byteNum)); + + final int intNum = 20; + assertEquals(shortNum, numberFunction.apply(intNum)); + } + + @Test + public void testConverterToInteger() { + final int intNum = 20; + final byte byteNum = 20; + final Function numberFunction = NumberUtil.converterTo(Integer.class); + assertEquals(intNum, numberFunction.apply(byteNum)); + } + + @Test + public void testConverterToLong() { + final long longNum = 20L; + final Function numberFunction = NumberUtil.converterTo(Long.class); + assertEquals(longNum, numberFunction.apply(longNum)); + + final byte byteNum = 20; + assertEquals(longNum, numberFunction.apply(byteNum)); + + final BigInteger bigIntNum = new BigInteger("20"); + assertEquals(longNum, numberFunction.apply(bigIntNum)); + } + + @Test + public void testConverterToBigDecimal() { + BigDecimal bigDecNum = new BigDecimal(20.0); + final Function numberFunction = NumberUtil.converterTo(BigDecimal.class); + assertEquals(bigDecNum, numberFunction.apply(bigDecNum)); + + int intNum = 20; + assertEquals(bigDecNum, numberFunction.apply(intNum)); + + double doubleNum = 20.0; + bigDecNum = new BigDecimal("20.0"); + assertEquals(bigDecNum, numberFunction.apply(doubleNum)); + } + + @Test public void testConverterToBigInteger() { + final BigInteger bigIntNum = new BigInteger("20"); + final Function numberFunction = NumberUtil.converterTo(BigInteger.class); + assertEquals(bigIntNum, numberFunction.apply(bigIntNum)); + + final int intNum = 20; + assertEquals(bigIntNum, numberFunction.apply(intNum)); + + final BigDecimal bigDecNum = new BigDecimal(20.0); + assertEquals(bigIntNum, numberFunction.apply(bigDecNum)); + } +}