From e37de6e66c5cc554384a1f27f68f4d27962fe15d Mon Sep 17 00:00:00 2001 From: Lisa Revel Date: Mon, 20 Feb 2017 19:15:19 +0000 Subject: Re-add source from correct branch Change-Id: I515f8378ea1d2baf0aa2434ec46272f1b5bdb229 Signed-off-by: Lisa Revel --- .../test/BaseDCAEAnalyticsCommonTest.java | 216 --------------------- .../dcae/analytics/test/BaseDCAEAnalyticsIT.java | 29 --- .../analytics/test/BaseDCAEAnalyticsUnitTest.java | 31 --- .../analytics/test/annotation/GuiceModules.java | 45 ----- .../analytics/test/runner/GuiceJUnitRunner.java | 102 ---------- .../test/BaseDCAEAnalyticsCommonTest.java | 216 +++++++++++++++++++++ .../apod/analytics/test/BaseDCAEAnalyticsIT.java | 29 +++ .../analytics/test/BaseDCAEAnalyticsUnitTest.java | 31 +++ .../analytics/test/annotation/GuiceModules.java | 45 +++++ .../analytics/test/runner/GuiceJUnitRunner.java | 102 ++++++++++ 10 files changed, 423 insertions(+), 423 deletions(-) delete mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsCommonTest.java delete mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsIT.java delete mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsUnitTest.java delete mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/annotation/GuiceModules.java delete mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/runner/GuiceJUnitRunner.java create mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsCommonTest.java create mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsIT.java create mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsUnitTest.java create mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/annotation/GuiceModules.java create mode 100644 dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/runner/GuiceJUnitRunner.java (limited to 'dcae-analytics-test/src/main/java') diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsCommonTest.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsCommonTest.java deleted file mode 100644 index c40bd94..0000000 --- a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsCommonTest.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * ============LICENSE_START========================================================= - * dcae-analytics - * ================================================================================ - * Copyright © 2017 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.openecomp.dcae.analytics.test; - -import org.json.JSONException; -import org.junit.Assert; -import org.skyscreamer.jsonassert.JSONAssert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.ObjectOutputStream; -import java.io.OutputStreamWriter; -import java.lang.reflect.Field; -import java.net.URL; -import java.nio.charset.Charset; -import java.nio.file.Paths; -import java.security.AccessController; -import java.security.PrivilegedAction; - -import static java.nio.file.Files.deleteIfExists; -import static java.nio.file.Files.exists; - -/** - * Base common test class for all DCAE Analytics Test e.g. unit tests, integration test, CDAP tests etc. - *

- * @author Rajiv Singla. Creation Date: 10/19/2016. - */ -abstract class BaseDCAEAnalyticsCommonTest { - - protected static final Logger LOG = LoggerFactory.getLogger(BaseDCAEAnalyticsCommonTest.class); - - /** - * Asserts if expected Json String and actual Json String contain the same properties ignoring - * property order. Simple String assertion might fail as property order during serialization and deserialization - * is generally non-deterministic. Also proper error message are generated more missing or unexpected - * properties - * - * @param expectedJsonString expected Json String - * @param actualJsonString actual Json String - * @throws JSONException Json Exception - */ - public static void assertJson(String expectedJsonString, String actualJsonString) throws JSONException { - JSONAssert.assertEquals(expectedJsonString, actualJsonString, true); - } - - /** - * Converts given file location to String - * - * @param fileLocation location of the file which needs to be converted to String - * @return Contents of file as string - * @throws IOException IOException - */ - public static String fromStream(String fileLocation) throws IOException { - final InputStream jsonFileInputStream = - BaseDCAEAnalyticsCommonTest.class.getClassLoader().getResourceAsStream(fileLocation); - Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream); - try (BufferedReader reader = - new BufferedReader(new InputStreamReader(jsonFileInputStream, Charset.forName("UTF-8")))) { - final StringBuilder result = new StringBuilder(); - final String newLine = System.getProperty("line.separator"); - String line = reader.readLine(); - while (line != null) { - result.append(line); - result.append(newLine); - line = reader.readLine(); - } - jsonFileInputStream.close(); - return result.toString(); - } - } - - - /** - * Checks if object can be serialized properly - * - * @param object input object - * @param callingClass calling class - * @throws Exception Exception - */ - public static void testSerialization(Object object, Class callingClass) throws Exception { - final URL location = callingClass.getProtectionDomain().getCodeSource().getLocation(); - final File serializedOutputFile = - new File(location.getPath() + String.format("serialization/%s.ser", object.getClass().getSimpleName())); - - // Maybe file already try deleting it first - final boolean deleteIfExists = deleteIfExists(Paths.get(serializedOutputFile.getPath())); - - if (deleteIfExists) { - LOG.warn("Previous serialization file was overwritten at location: {}", serializedOutputFile.getPath()); - } - - boolean mkdirs = true; - if (!exists(Paths.get(serializedOutputFile.getParentFile().getPath()))) { - mkdirs = serializedOutputFile.getParentFile().mkdirs(); - } - if (mkdirs) { - try (FileOutputStream fileOutputStream = new FileOutputStream(serializedOutputFile); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { - objectOutputStream.writeObject(object); - LOG.debug("Successfully created serialization file at location: {}", serializedOutputFile.getPath()); - } - } else { - throw new Exception( - String.format("Failed to create location to store serialization file: %s", - serializedOutputFile)); - } - } - - /** - * Writes Text to Output file - * - * @param textFileLocation - location of text file e.g. textfiles/fileName.json - * @param content - file content - * @param callingClass - calling class - * @throws Exception - exception - */ - public static void writeToOutputTextFile(String textFileLocation, String content, Class callingClass) throws - Exception { - final URL location = callingClass.getProtectionDomain().getCodeSource().getLocation(); - final File fileLocation = new File(location.getPath() + textFileLocation); - - // Maybe file already try deleting it first - final boolean deleteIfExists = deleteIfExists(Paths.get(fileLocation.getPath())); - - if (deleteIfExists) { - LOG.warn("Previous file will be overwritten at location: {}", fileLocation.getPath()); - } - - boolean mkdirs = true; - if (!exists(Paths.get(fileLocation.getParentFile().getPath()))) { - mkdirs = fileLocation.getParentFile().mkdirs(); - } - if (mkdirs) { - try (OutputStreamWriter outputStream = new OutputStreamWriter( - new FileOutputStream(fileLocation), Charset.forName("UTF-8"))) { - outputStream.write(content); - LOG.debug("Successfully created text file at location: {}", fileLocation.getPath()); - } - } else { - throw new Exception( - String.format("Failed to create location to store text file: %s", - fileLocation)); - } - - } - - - /** - * For testing purposes only we may sometime we may want to access private fields of underlying - * object to confirm the values are setup correctly. - *

- * This method uses java reflection to get the value to private object in the class - * - * @param object Actual object which has the private field you want to check - * @param fieldName Field name in the Actual Object you want to get the value of - * @param privateFieldClass Type of the private field - * @param Class of Actual Object - * @param Class of private field - * @return value of the private field - */ - public static U getPrivateFiledValue(T object, String fieldName, Class privateFieldClass) { - - final Class objectClass = object.getClass(); - try { - final Field privateField = objectClass.getDeclaredField(fieldName); - try { - - // mark private field to be accessible for testing purposes - AccessController.doPrivileged(new PrivilegedAction() { - @Override - public Object run() { - privateField.setAccessible(true); - return null; - } - }); - - - return privateFieldClass.cast(privateField.get(object)); - - } catch (IllegalAccessException e) { - LOG.error("Unable to access field: {}", fieldName); - throw new RuntimeException(e); - } - } catch (NoSuchFieldException e) { - LOG.error("Unable to locate field name: {} in class: {}", fieldName, objectClass.getSimpleName()); - throw new RuntimeException(e); - } - - - } - -} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsIT.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsIT.java deleted file mode 100644 index 789f7ec..0000000 --- a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsIT.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * ============LICENSE_START========================================================= - * dcae-analytics - * ================================================================================ - * Copyright © 2017 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.openecomp.dcae.analytics.test; - -/** - * Base class for all DCAE Analytics Integration Tests - *

- * @author Rajiv Singla. Creation Date: 10/6/2016. - */ -public abstract class BaseDCAEAnalyticsIT extends BaseDCAEAnalyticsCommonTest { -} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsUnitTest.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsUnitTest.java deleted file mode 100644 index 7c5526f..0000000 --- a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/BaseDCAEAnalyticsUnitTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * ============LICENSE_START========================================================= - * dcae-analytics - * ================================================================================ - * Copyright © 2017 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.openecomp.dcae.analytics.test; - -/** - * Base class for all DCAE Analytics Unit Tests - *

- * @author Rajiv Singla. Creation Date: 10/6/2016. - */ -public abstract class BaseDCAEAnalyticsUnitTest extends BaseDCAEAnalyticsCommonTest { - - -} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/annotation/GuiceModules.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/annotation/GuiceModules.java deleted file mode 100644 index 3ee8163..0000000 --- a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/annotation/GuiceModules.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ============LICENSE_START========================================================= - * dcae-analytics - * ================================================================================ - * Copyright © 2017 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.openecomp.dcae.analytics.test.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation which can be used to provide test guice modules - *

- * @author Rajiv Singla. Creation Date: 10/20/2016. - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Inherited -public @interface GuiceModules { - - /** - * Test Modules - * - * @return module classes - */ - Class[] value(); -} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/runner/GuiceJUnitRunner.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/runner/GuiceJUnitRunner.java deleted file mode 100644 index 21a89a9..0000000 --- a/dcae-analytics-test/src/main/java/org/openecomp/dcae/analytics/test/runner/GuiceJUnitRunner.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * ============LICENSE_START========================================================= - * dcae-analytics - * ================================================================================ - * Copyright © 2017 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.openecomp.dcae.analytics.test.runner; - -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Module; -import org.junit.runners.BlockJUnit4ClassRunner; -import org.junit.runners.model.InitializationError; -import org.openecomp.dcae.analytics.test.annotation.GuiceModules; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Arrays; - -/** - * A custom Junit Runner which can be used to run Guice Test with custom Test Modules - *

- * @author Rajiv Singla. Creation Date: 10/20/2016. - */ -public class GuiceJUnitRunner extends BlockJUnit4ClassRunner { - - private static final Logger LOG = LoggerFactory.getLogger(GuiceJUnitRunner.class); - - private Injector injector; - - public GuiceJUnitRunner(Class klass) throws InitializationError { - super(klass); - Class[] classes = getModulesFor(klass); - injector = createInjectorFor(classes); - } - - - /** - * Returns a new fixture for running a test. Injects Guice members - */ - @Override - public Object createTest() throws Exception { - Object obj = super.createTest(); - injector.injectMembers(obj); - return obj; - } - - - /** - * Creates new Guice Injector and registers Guice test modules - * - * @param classes test module classes - * @return Guice injector with test modules - * @throws InitializationError - */ - private Injector createInjectorFor(Class[] classes) throws InitializationError { - Module[] modules = new Module[classes.length]; - for (int i = 0; i < classes.length; i++) { - try { - modules[i] = (Module) (classes[i]).newInstance(); - } catch (InstantiationException e) { - throw new InitializationError(e); - } catch (IllegalAccessException e) { - throw new InitializationError(e); - } - } - LOG.debug("Creating Junit Test Runner with Guice Test Modules: {}", Arrays.toString(modules)); - return Guice.createInjector(modules); - } - - /** - * Extract user provide test modules from the {@link GuiceModules} annotation - * - * @param klass Target class which is running the test - * @return Guice modules contained passed in annotation of Guice Modules - * @throws InitializationError - */ - private Class[] getModulesFor(Class klass) throws InitializationError { - GuiceModules annotation = klass.getAnnotation(GuiceModules.class); - if (annotation == null) { - throw new InitializationError( - "Missing @GuiceModules annotation for unit test '" + klass.getName() - + "'"); - } - return annotation.value(); - } - -} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsCommonTest.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsCommonTest.java new file mode 100644 index 0000000..a7f2e04 --- /dev/null +++ b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsCommonTest.java @@ -0,0 +1,216 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 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.openecomp.dcae.apod.analytics.test; + +import org.json.JSONException; +import org.junit.Assert; +import org.skyscreamer.jsonassert.JSONAssert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectOutputStream; +import java.io.OutputStreamWriter; +import java.lang.reflect.Field; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.file.Paths; +import java.security.AccessController; +import java.security.PrivilegedAction; + +import static java.nio.file.Files.deleteIfExists; +import static java.nio.file.Files.exists; + +/** + * Base common test class for all DCAE Analytics Test e.g. unit tests, integration test, CDAP tests etc. + *

+ * @author Rajiv Singla. Creation Date: 10/19/2016. + */ +abstract class BaseDCAEAnalyticsCommonTest { + + protected static final Logger LOG = LoggerFactory.getLogger(BaseDCAEAnalyticsCommonTest.class); + + /** + * Asserts if expected Json String and actual Json String contain the same properties ignoring + * property order. Simple String assertion might fail as property order during serialization and deserialization + * is generally non-deterministic. Also proper error message are generated more missing or unexpected + * properties + * + * @param expectedJsonString expected Json String + * @param actualJsonString actual Json String + * @throws JSONException Json Exception + */ + public static void assertJson(String expectedJsonString, String actualJsonString) throws JSONException { + JSONAssert.assertEquals(expectedJsonString, actualJsonString, true); + } + + /** + * Converts given file location to String + * + * @param fileLocation location of the file which needs to be converted to String + * @return Contents of file as string + * @throws IOException IOException + */ + public static String fromStream(String fileLocation) throws IOException { + final InputStream jsonFileInputStream = + BaseDCAEAnalyticsCommonTest.class.getClassLoader().getResourceAsStream(fileLocation); + Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream); + try (BufferedReader reader = + new BufferedReader(new InputStreamReader(jsonFileInputStream, Charset.forName("UTF-8")))) { + final StringBuilder result = new StringBuilder(); + final String newLine = System.getProperty("line.separator"); + String line = reader.readLine(); + while (line != null) { + result.append(line); + result.append(newLine); + line = reader.readLine(); + } + jsonFileInputStream.close(); + return result.toString(); + } + } + + + /** + * Checks if object can be serialized properly + * + * @param object input object + * @param callingClass calling class + * @throws Exception Exception + */ + public static void testSerialization(Object object, Class callingClass) throws Exception { + final URL location = callingClass.getProtectionDomain().getCodeSource().getLocation(); + final File serializedOutputFile = + new File(location.getPath() + String.format("serialization/%s.ser", object.getClass().getSimpleName())); + + // Maybe file already try deleting it first + final boolean deleteIfExists = deleteIfExists(Paths.get(serializedOutputFile.getPath())); + + if (deleteIfExists) { + LOG.warn("Previous serialization file was overwritten at location: {}", serializedOutputFile.getPath()); + } + + boolean mkdirs = true; + if (!exists(Paths.get(serializedOutputFile.getParentFile().getPath()))) { + mkdirs = serializedOutputFile.getParentFile().mkdirs(); + } + if (mkdirs) { + try (FileOutputStream fileOutputStream = new FileOutputStream(serializedOutputFile); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { + objectOutputStream.writeObject(object); + LOG.debug("Successfully created serialization file at location: {}", serializedOutputFile.getPath()); + } + } else { + throw new Exception( + String.format("Failed to create location to store serialization file: %s", + serializedOutputFile)); + } + } + + /** + * Writes Text to Output file + * + * @param textFileLocation - location of text file e.g. textfiles/fileName.json + * @param content - file content + * @param callingClass - calling class + * @throws Exception - exception + */ + public static void writeToOutputTextFile(String textFileLocation, String content, Class callingClass) throws + Exception { + final URL location = callingClass.getProtectionDomain().getCodeSource().getLocation(); + final File fileLocation = new File(location.getPath() + textFileLocation); + + // Maybe file already try deleting it first + final boolean deleteIfExists = deleteIfExists(Paths.get(fileLocation.getPath())); + + if (deleteIfExists) { + LOG.warn("Previous file will be overwritten at location: {}", fileLocation.getPath()); + } + + boolean mkdirs = true; + if (!exists(Paths.get(fileLocation.getParentFile().getPath()))) { + mkdirs = fileLocation.getParentFile().mkdirs(); + } + if (mkdirs) { + try (OutputStreamWriter outputStream = new OutputStreamWriter( + new FileOutputStream(fileLocation), Charset.forName("UTF-8"))) { + outputStream.write(content); + LOG.debug("Successfully created text file at location: {}", fileLocation.getPath()); + } + } else { + throw new Exception( + String.format("Failed to create location to store text file: %s", + fileLocation)); + } + + } + + + /** + * For testing purposes only we may sometime we may want to access private fields of underlying + * object to confirm the values are setup correctly. + *

+ * This method uses java reflection to get the value to private object in the class + * + * @param object Actual object which has the private field you want to check + * @param fieldName Field name in the Actual Object you want to get the value of + * @param privateFieldClass Type of the private field + * @param Class of Actual Object + * @param Class of private field + * @return value of the private field + */ + public static U getPrivateFiledValue(T object, String fieldName, Class privateFieldClass) { + + final Class objectClass = object.getClass(); + try { + final Field privateField = objectClass.getDeclaredField(fieldName); + try { + + // mark private field to be accessible for testing purposes + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Object run() { + privateField.setAccessible(true); + return null; + } + }); + + + return privateFieldClass.cast(privateField.get(object)); + + } catch (IllegalAccessException e) { + LOG.error("Unable to access field: {}", fieldName); + throw new RuntimeException(e); + } + } catch (NoSuchFieldException e) { + LOG.error("Unable to locate field name: {} in class: {}", fieldName, objectClass.getSimpleName()); + throw new RuntimeException(e); + } + + + } + +} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsIT.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsIT.java new file mode 100644 index 0000000..067a95a --- /dev/null +++ b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsIT.java @@ -0,0 +1,29 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 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.openecomp.dcae.apod.analytics.test; + +/** + * Base class for all DCAE Analytics Integration Tests + *

+ * @author Rajiv Singla. Creation Date: 10/6/2016. + */ +public abstract class BaseDCAEAnalyticsIT extends BaseDCAEAnalyticsCommonTest { +} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsUnitTest.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsUnitTest.java new file mode 100644 index 0000000..87d40b3 --- /dev/null +++ b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/BaseDCAEAnalyticsUnitTest.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 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.openecomp.dcae.apod.analytics.test; + +/** + * Base class for all DCAE Analytics Unit Tests + *

+ * @author Rajiv Singla. Creation Date: 10/6/2016. + */ +public abstract class BaseDCAEAnalyticsUnitTest extends BaseDCAEAnalyticsCommonTest { + + +} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/annotation/GuiceModules.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/annotation/GuiceModules.java new file mode 100644 index 0000000..585b399 --- /dev/null +++ b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/annotation/GuiceModules.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 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.openecomp.dcae.apod.analytics.test.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation which can be used to provide test guice modules + *

+ * @author Rajiv Singla. Creation Date: 10/20/2016. + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface GuiceModules { + + /** + * Test Modules + * + * @return module classes + */ + Class[] value(); +} diff --git a/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/runner/GuiceJUnitRunner.java b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/runner/GuiceJUnitRunner.java new file mode 100644 index 0000000..67b346f --- /dev/null +++ b/dcae-analytics-test/src/main/java/org/openecomp/dcae/apod/analytics/test/runner/GuiceJUnitRunner.java @@ -0,0 +1,102 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 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.openecomp.dcae.apod.analytics.test.runner; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.InitializationError; +import org.openecomp.dcae.apod.analytics.test.annotation.GuiceModules; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * A custom Junit Runner which can be used to run Guice Test with custom Test Modules + *

+ * @author Rajiv Singla. Creation Date: 10/20/2016. + */ +public class GuiceJUnitRunner extends BlockJUnit4ClassRunner { + + private static final Logger LOG = LoggerFactory.getLogger(GuiceJUnitRunner.class); + + private Injector injector; + + public GuiceJUnitRunner(Class klass) throws InitializationError { + super(klass); + Class[] classes = getModulesFor(klass); + injector = createInjectorFor(classes); + } + + + /** + * Returns a new fixture for running a test. Injects Guice members + */ + @Override + public Object createTest() throws Exception { + Object obj = super.createTest(); + injector.injectMembers(obj); + return obj; + } + + + /** + * Creates new Guice Injector and registers Guice test modules + * + * @param classes test module classes + * @return Guice injector with test modules + * @throws InitializationError + */ + private Injector createInjectorFor(Class[] classes) throws InitializationError { + Module[] modules = new Module[classes.length]; + for (int i = 0; i < classes.length; i++) { + try { + modules[i] = (Module) (classes[i]).newInstance(); + } catch (InstantiationException e) { + throw new InitializationError(e); + } catch (IllegalAccessException e) { + throw new InitializationError(e); + } + } + LOG.debug("Creating Junit Test Runner with Guice Test Modules: {}", Arrays.toString(modules)); + return Guice.createInjector(modules); + } + + /** + * Extract user provide test modules from the {@link GuiceModules} annotation + * + * @param klass Target class which is running the test + * @return Guice modules contained passed in annotation of Guice Modules + * @throws InitializationError + */ + private Class[] getModulesFor(Class klass) throws InitializationError { + GuiceModules annotation = klass.getAnnotation(GuiceModules.class); + if (annotation == null) { + throw new InitializationError( + "Missing @GuiceModules annotation for unit test '" + klass.getName() + + "'"); + } + return annotation.value(); + } + +} -- cgit 1.2.3-korg