From 1474e204f441bf1eabfaa8d458857c76250133bb Mon Sep 17 00:00:00 2001 From: IanHowell Date: Wed, 9 May 2018 09:20:44 -0500 Subject: Improve coverage of cadi-aaf Tested files in package org.onap.aaf.cadi.persist Issue-ID: AAF-223 Change-Id: I0c597756e96ffea37d30938502ef4f2b62793a02 Signed-off-by: IanHowell --- .../java/org/onap/aaf/cadi/persist/Persisting.java | 6 +- .../org/onap/aaf/cadi/persist/test/JU_Persist.java | 152 +++++++++++++++++++++ .../onap/aaf/cadi/persist/test/JU_PersistFile.java | 121 ++++++++++++++++ .../onap/aaf/cadi/persist/test/JU_Persisting.java | 130 ++++++++++++++++++ 4 files changed, 404 insertions(+), 5 deletions(-) create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_PersistFile.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persisting.java (limited to 'cadi') diff --git a/cadi/aaf/src/main/java/org/onap/aaf/cadi/persist/Persisting.java b/cadi/aaf/src/main/java/org/onap/aaf/cadi/persist/Persisting.java index 7131b607..8b98f5bf 100644 --- a/cadi/aaf/src/main/java/org/onap/aaf/cadi/persist/Persisting.java +++ b/cadi/aaf/src/main/java/org/onap/aaf/cadi/persist/Persisting.java @@ -113,11 +113,7 @@ public class Persisting implements Persistable { //TODO other elements to add here... // Ideas: Is it valid? // if not, How many times has it been checked in the last minute - if(expired()) { - return true; - } else { - return false; - } + return expired(); } @Override diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java new file mode 100644 index 00000000..3fd65a73 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java @@ -0,0 +1,152 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T 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.aaf.cadi.persist.test; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Matchers.any; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.client.Holder; +import org.onap.aaf.cadi.client.Result; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.persist.Persist; +import org.onap.aaf.cadi.persist.Persist.Loader; +import org.onap.aaf.cadi.persist.Persistable; +import org.onap.aaf.cadi.persist.Persisting; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.rosetta.env.RosettaDF; +import org.onap.aaf.misc.rosetta.env.RosettaData; +import org.onap.aaf.misc.rosetta.env.RosettaEnv; + +public class JU_Persist { + + private static final String resourceDirString = "src/test/resources"; + private static final String tokenDirString = "tokenDir"; + private static final String key = "key"; + + private static final int data = 5; + + private static final byte[] cred = "password".getBytes(); + + private PropAccess access; + private Result> result; + + @Mock private RosettaEnv envMock; + @Mock private Persist persistMock; + @Mock private RosettaDF dfMock; + @Mock private RosettaData dataMock; + @Mock private Persistable ctMock1; + @Mock private Persisting ctMock2; + @Mock private Loader> loaderMock; + + @Before + public void setup() throws APIException, CadiException, LocatorException { + MockitoAnnotations.initMocks(this); + + doReturn(dfMock).when(envMock).newDataFactory((Class[]) any()); + when(dfMock.newData()).thenReturn(dataMock); + when(dataMock.load(data)).thenReturn(dataMock); + + + result = Result.ok(200, ctMock1); + when(loaderMock.load(key)).thenReturn(result); + + access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); + access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString); + } + + @After + public void tearDown() { + File dir = new File(resourceDirString + '/' + tokenDirString); + for (File f : dir.listFiles()) { + f.delete(); + } + dir.delete(); + } + + @Test + public void test() throws CadiException, APIException, LocatorException, InterruptedException { + Persist> persist = new PersistStub(access, envMock, null, tokenDirString); + // Second call for coverage + persist = new PersistStub(access, envMock, null, tokenDirString); + assertThat(persist.getDF(), is(dfMock)); + persist.put(key, ctMock2); + Result> output = persist.get(key, cred, loaderMock); + assertThat(output.code, is(200)); + assertThat(output.isOK(), is(true)); + + when(ctMock2.checkSyncTime()).thenReturn(true); + when(ctMock2.hasBeenTouched()).thenReturn(true); + output = persist.get(key, cred, loaderMock); + assertThat(output.code, is(200)); + assertThat(output.isOK(), is(true)); + + persist.delete(key); + + assertThat(persist.get(null, null, null), is(nullValue())); + + // Uncommenting this lets us begin to test the nested Clean class, but + // will dramatically slow down every build that runs tests - We need to + // either refactor or find a more creative way to test Clean +// Thread.sleep(25000); + + persist.close(); + } + + private class PersistStub extends Persist> { + public PersistStub(Access access, RosettaEnv env, Class cls, String sub_dir) + throws CadiException, APIException { super(access, env, cls, sub_dir); } + @Override + protected Persistable newCacheable(Integer t, long expires_secsFrom1970, byte[] hash, Path path) + throws APIException, IOException { return null; } + @Override + public Path writeDisk(final RosettaDF df, final T t, final byte[] cred, final Path target, final long expires) throws CadiException { + return null; + } + @SuppressWarnings("unchecked") + @Override + public T readDisk(final RosettaDF df, final byte[] cred, final String filename,final Holder hp, final Holder hl) throws CadiException { + return (T)new Integer(data); + } + + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_PersistFile.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_PersistFile.java new file mode 100644 index 00000000..cbe865eb --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_PersistFile.java @@ -0,0 +1,121 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T 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.aaf.cadi.persist.test; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Mockito.when; +import static org.mockito.Matchers.any; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; + +import javax.crypto.CipherInputStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.client.Holder; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.persist.PersistFile; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.rosetta.env.RosettaDF; +import org.onap.aaf.misc.rosetta.env.RosettaData; + +public class JU_PersistFile { + + private static final String resourceDirString = "src/test/resources"; + private static final String tokenDirString = "tokenDir"; + private static final String tokenFileName = "token"; + + private static final int data = 5; + private static final long expires = 10000; + + private static final byte[] cred = "password".getBytes(); + + private PropAccess access; + private Holder hp = new Holder(null); + private Holder hl = new Holder(null); + + @Mock private RosettaDF dfMock; + @Mock private RosettaData dataMock; + @Mock private Holder hpMock; + + @Before + public void setup() throws APIException { + MockitoAnnotations.initMocks(this); + + when(dfMock.newData()).thenReturn(dataMock); + when(dataMock.load(data)).thenReturn(dataMock); + when(dataMock.load((CipherInputStream)any())).thenReturn(dataMock); + + access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); + access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString); + } + + @After + public void tearDown() { + File dir = new File(resourceDirString + '/' + tokenDirString); + for (File f : dir.listFiles()) { + f.delete(); + } + dir.delete(); + } + + @Test + public void test() throws CadiException, APIException, IOException { + PersistFile persistFile = new PersistFile(access, tokenDirString); + // Second call is for coverage + persistFile = new PersistFile(access, tokenDirString); + Path filepath = persistFile.writeDisk(dfMock, data, cred, tokenFileName, expires); + persistFile.readDisk(dfMock, cred, tokenFileName, hp, hl); + assertThat(persistFile.readExpiration(filepath), is(expires)); + + FileTime ft1 = persistFile.getFileTime(tokenFileName, hp); + FileTime ft2 = persistFile.getFileTime(tokenFileName, hpMock); + assertThat(ft1.toMillis(), is(ft2.toMillis())); + + persistFile.deleteFromDisk(filepath); + persistFile.deleteFromDisk(resourceDirString + '/' + tokenDirString + '/' + tokenFileName); + assertThat(persistFile.readExpiration(filepath), is(0L)); + + persistFile.getPath(resourceDirString + '/' + tokenDirString + '/' + tokenFileName); + + persistFile.writeDisk(dfMock, data, null, tokenFileName, expires); + try { + persistFile.readDisk(dfMock, cred, tokenFileName, hp, hl); + fail("Should've thrown an exception"); + } catch (CadiException e) { + assertThat(e.getMessage(), is(CadiException.class.getName() + ": Hash does not match in Persistence")); + } + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persisting.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persisting.java new file mode 100644 index 00000000..bb2b918a --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persisting.java @@ -0,0 +1,130 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T 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.aaf.cadi.persist.test; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Mockito.when; +import static org.mockito.Matchers.any; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.nio.file.Path; +import java.nio.file.Paths; + +import javax.crypto.CipherInputStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.persist.Persist; +import org.onap.aaf.cadi.persist.PersistFile; +import org.onap.aaf.cadi.persist.Persisting; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.rosetta.env.RosettaDF; +import org.onap.aaf.misc.rosetta.env.RosettaData; + +public class JU_Persisting { + + private static final String resourceDirString = "src/test/resources"; + private static final String tokenDirString = "tokenDir"; + private static final String tokenFileName = "token"; + + private static final int data = 5; + private static final long expires = 10000; + + private static final byte[] cred = "password".getBytes(); + + private PropAccess access; + + @Mock private Persist persistMock; + @Mock private RosettaDF dfMock; + @Mock private RosettaData dataMock; + + @Before + public void setup() throws APIException { + MockitoAnnotations.initMocks(this); + + when(dfMock.newData()).thenReturn(dataMock); + when(dataMock.load(data)).thenReturn(dataMock); + when(dataMock.load((CipherInputStream)any())).thenReturn(dataMock); + + access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); + access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString); + + persistMock.access = access; + } + + @After + public void tearDown() { + File dir = new File(resourceDirString + '/' + tokenDirString); + for (File f : dir.listFiles()) { + f.delete(); + } + dir.delete(); + } + + @Test + public void test() throws CadiException, APIException { + Path tokenPath = Paths.get(resourceDirString, tokenDirString); + + Persisting persisting = new Persisting<>(persistMock, data, expires, cred, tokenPath); + assertThat(persisting.get(), is(data)); + assertThat(persisting.expires(), is(expires)); + assertThat(persisting.expired(), is(true)); + assertThat(persisting.hasBeenTouched(), is(true)); + + PersistFile persistFile = new PersistFile(access, tokenDirString); + tokenPath = persistFile.writeDisk(dfMock, data, cred, tokenFileName, expires); + persisting = new Persisting<>(persistMock, data, expires, cred, tokenPath); + assertThat(persisting.hasBeenTouched(), is(false)); + + persisting = new Persisting<>(persistMock, data, expires * (int)10e9, cred, tokenPath); + assertThat(persisting.expired(), is(false)); + + assertThat(persisting.checkSyncTime(), is(true)); + assertThat(persisting.checkSyncTime(), is(false)); + + assertThat(persisting.checkReloadable(), is(false)); + + assertThat(persisting.getHash(), is(cred)); + + assertThat(persisting.match(null), is(false)); + assertThat(persisting.match("random!".getBytes()), is(false)); + assertThat(persisting.match("passwrod".getBytes()), is(false)); + assertThat(persisting.match(cred), is(true)); + + persisting.clearCount(); + assertThat(persisting.count(), is(0)); + persisting.inc(); + assertThat(persisting.count(), is(1)); + + assertThat(persisting.path(), is(tokenPath)); + } + +} -- cgit 1.2.3-korg