diff options
author | pkaras <piotr.karas@nokia.com> | 2019-04-12 13:05:18 +0200 |
---|---|---|
committer | pkaras <piotr.karas@nokia.com> | 2019-04-12 14:53:38 +0200 |
commit | 57850893ab80ef3e144e500f9342ed53bfe33823 (patch) | |
tree | f9f49d14a570fdd14bc09709fde587f3d9711631 /src | |
parent | 0654d98f69b80b8d932dd33a5dcc56dbecc6a64e (diff) |
Junits for classes in util package
Change-Id: If1d692b961533722ca72ec13707099858715542b
Issue-ID: DMAAP-1159
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
Diffstat (limited to 'src')
7 files changed, 137 insertions, 98 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/util/DmaapConfig.java b/src/main/java/org/onap/dmaap/dbcapi/util/DmaapConfig.java index cfcdc1c..a47c0bd 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/util/DmaapConfig.java +++ b/src/main/java/org/onap/dmaap/dbcapi/util/DmaapConfig.java @@ -37,10 +37,8 @@ public class DmaapConfig extends Properties { return(configfname); } private DmaapConfig() { - try { - InputStream is = new FileInputStream(configfname); + try (InputStream is = new FileInputStream(configfname)){ load(is); - is.close(); } catch (Exception e) { System.err.println("Unable to load configuration file " + configfname); org.apache.log4j.Logger.getLogger(getClass()).fatal("Unable to load configuration file " + configfname, e); diff --git a/src/main/java/org/onap/dmaap/dbcapi/util/DmaapTimestamp.java b/src/main/java/org/onap/dmaap/dbcapi/util/DmaapTimestamp.java index 776a4b4..f36df1c 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/util/DmaapTimestamp.java +++ b/src/main/java/org/onap/dmaap/dbcapi/util/DmaapTimestamp.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,30 +20,27 @@ package org.onap.dmaap.dbcapi.util; -import java.util.Calendar; -import java.util.Date; - import javax.xml.bind.annotation.XmlRootElement; - -import org.apache.log4j.Logger; +import java.util.Date; @XmlRootElement public class DmaapTimestamp { - static final Logger logger = Logger.getLogger(DmaapTimestamp.class); - private Calendar cal = Calendar.getInstance(); - private Date stamp; - - public DmaapTimestamp() { - - stamp = cal.getTime(); - } - - public void mark() { - stamp = cal.getTime(); - } - - public Date getVal() { - return stamp; - } + private Date stamp; + + public DmaapTimestamp() { + this(new Date()); + } + + DmaapTimestamp(Date stamp) { + this.stamp = new Date(stamp.getTime()); + } + + public void mark() { + stamp = new Date(); + } + + public Date getVal() { + return new Date(stamp.getTime()); + } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/util/Graph.java b/src/main/java/org/onap/dmaap/dbcapi/util/Graph.java index 700c77f..ab40765 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/util/Graph.java +++ b/src/main/java/org/onap/dmaap/dbcapi/util/Graph.java @@ -53,19 +53,17 @@ public class Graph { if ( clients == null ) return; initGraph( clients, strict, "" ); - return; } public Graph( List<MR_Client> clients, boolean strict, String group ) { if ( clients == null ) return; initGraph( clients, strict, group ); - return; } private void initGraph(List<MR_Client> clients, boolean strict, String group ) { MR_ClusterService clusters = new MR_ClusterService(); - this.graph = new HashMap<String, String>(); + this.graph = new HashMap<>(); this.hasCentral = false; for( MR_Client client: clients ) { if ( ! strict || client.isStatusValid()) { diff --git a/src/main/java/org/onap/dmaap/dbcapi/util/RandomInteger.java b/src/main/java/org/onap/dmaap/dbcapi/util/RandomInteger.java index ea8483f..1c9bc11 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/util/RandomInteger.java +++ b/src/main/java/org/onap/dmaap/dbcapi/util/RandomInteger.java @@ -25,7 +25,6 @@ import java.util.Date; // with some modifications import java.util.Random; - public final class RandomInteger { private static Random randomGenerator; private int range; @@ -39,24 +38,6 @@ public final class RandomInteger { public int next(){ return randomGenerator.nextInt(range); } - -/** Generate 10 random integers in the range 0..99. - public static final void main(String... aArgs){ - log("Generating 10 random integers in range 0..99."); - RandomInteger ri = new RandomInteger(100); - //note a single Random object is reused here - - for (int idx = 1; idx <= 10; ++idx){ - int randomInt = ri.next(); - log("Generated : " + randomInt); - } - - log("Done."); - } - - private static void log(String aMessage){ - System.out.println(aMessage); - } -*/ + } diff --git a/src/test/java/org/onap/dmaap/dbcapi/util/DmaapTimestampTest.java b/src/test/java/org/onap/dmaap/dbcapi/util/DmaapTimestampTest.java new file mode 100644 index 0000000..33d1d17 --- /dev/null +++ b/src/test/java/org/onap/dmaap/dbcapi/util/DmaapTimestampTest.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.dbcapi.util; + +import org.junit.Test; + +import java.util.Date; + +import static org.junit.Assert.assertTrue; + +public class DmaapTimestampTest { + + private DmaapTimestamp dmaapTimestamp = new DmaapTimestamp(); + + @Test + public void mark_shouldUpdateTimestamp() { + dmaapTimestamp = new DmaapTimestamp(new Date(10)); + Date timestamp = dmaapTimestamp.getVal(); + + dmaapTimestamp.mark(); + + assertTrue(timestamp.before(dmaapTimestamp.getVal())); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/dbcapi/util/RandomIntegerTest.java b/src/test/java/org/onap/dmaap/dbcapi/util/RandomIntegerTest.java index bd5f9cd..1184cdb 100644 --- a/src/test/java/org/onap/dmaap/dbcapi/util/RandomIntegerTest.java +++ b/src/test/java/org/onap/dmaap/dbcapi/util/RandomIntegerTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * org.onap.dmaap * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,59 +19,22 @@ */ package org.onap.dmaap.dbcapi.util; -import org.onap.dmaap.dbcapi.model.*; -import org.onap.dmaap.dbcapi.service.*; -import org.onap.dmaap.dbcapi.testframework.ReflectionHarness; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import java.util.*; -import java.sql.*; - -public class RandomIntegerTest { - - private static final String fmt = "%24s: %s%n"; - - ReflectionHarness rh = new ReflectionHarness(); - - RandomInteger ri; - - @Before - public void setUp() throws Exception { - ri = new RandomInteger( 12434 ); - } +import static org.junit.Assert.assertTrue; - @After - public void tearDown() throws Exception { - } - - - @Test - public void test1() { - - - rh.reflect( "org.onap.dmaap.dbcapi.util.RandomInteger", "get", "" ); - - } - - @Test - public void test2() { - String v = "Validate"; - rh.reflect( "org.onap.dmaap.dbcapi.util.RandomInteger", "set", v ); - - } +public class RandomIntegerTest { - @Test - public void test3() { - int i = ri.next(); + private static final int RANGE = 10; + private RandomInteger ri = new RandomInteger(RANGE); - } + @Test + public void next_shouldReturnIntegerFromGivenRange() { + int next = ri.next(); + assertTrue(next >= 0 && next <= RANGE); + } } diff --git a/src/test/java/org/onap/dmaap/dbcapi/util/RandomStringTest.java b/src/test/java/org/onap/dmaap/dbcapi/util/RandomStringTest.java new file mode 100644 index 0000000..e549dc3 --- /dev/null +++ b/src/test/java/org/onap/dmaap/dbcapi/util/RandomStringTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.dbcapi.util; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class RandomStringTest { + + private static final int LENGTH = 10; + @Rule + public ExpectedException thrown = ExpectedException.none(); + private RandomString randomString = new RandomString(LENGTH); + + @Test + public void nextString_shouldReturnStringWithGivenLength() { + + String nextString = randomString.nextString(); + + assertEquals(LENGTH, nextString.length()); + } + + @Test + public void nextString_shouldReturnAlphanumeric() { + + String nextString = randomString.nextString(); + + assertTrue(nextString.matches("[a-z0-9]*")); + } + + @Test + public void constructor_shouldThrowExceptionForNegativeLength() { + + thrown.expect(IllegalArgumentException.class); + + new RandomString(-1); + } +}
\ No newline at end of file |