diff options
Diffstat (limited to 'common-app-api/src/test/java/org/openecomp')
9 files changed, 832 insertions, 102 deletions
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java index daeb0c94d2..b5036060eb 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java @@ -20,125 +20,231 @@ package org.openecomp.sdc.common.util; -import java.util.List; -import org.junit.Assert; +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; import org.junit.Test; +import java.io.File; +import java.io.IOException; +import java.util.Base64; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class GeneralUtilityTest { - private GeneralUtility createTestSubject() { - return new GeneralUtility(); + @Test + public void validateGenerateTextFileReturnsTrueIfGeneratesTextFile() throws IOException { + + final String fileName = "test.txt"; + final String fileData = "test data"; + final File expectedFile = new File(fileName); + + boolean result = GeneralUtility.generateTextFile(fileName, fileData); + + String createdFileData = FileUtils.readFileToString(expectedFile); + + assertTrue(result); + assertEquals(createdFileData ,fileData); + + FileUtils.forceDelete(expectedFile); + } + + @Test + public void validateIsBase64EncodedReturnsProperResponseFromByteArray() { + + final String testString = "testDataToEncode"; + final byte[] testBytes = testString.getBytes(); + final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes); + + boolean result = GeneralUtility.isBase64Encoded(testEncodedBytes); + + assertTrue(result); + } + + @Test + public void validateIsBase64EncodedReturnsProperResponseFromString() { + + final String testString = "testDataToEncode"; + final byte[] testBytes = testString.getBytes(); + final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes); + final String testEncodedString = new String(testEncodedBytes); + + boolean result = GeneralUtility.isBase64Encoded(testEncodedString); + + assertTrue(result); + } + + @Test + public void validateIsExceedingLimitReturnsFalseIfStringIsShorterThenLimit() { + + final String testString = "test"; + final int limit = 5; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitReturnsFalseIfStringIsNull() { + + final String testString = null; + final int limit = 5; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitReturnsFalseIfStringLengthIsEqualToLimit() { + + final String testString = "test"; + final int limit = 4; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitReturnsTrueIfStringExceedsLimit() { + + final String testString = "test"; + final int limit = 3; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertTrue(result); + } + + @Test + public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsSmallerThenLimit() { + + final List<String> testString = Lists.newArrayList("testing","list"); + final int limit = 15; + final int delimiterLength = 2; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitWithDelimiterReturnsFalseIfListIsNull() { + + final List<String> testString = null; + final int limit = 15; + final int delimiterLength = 2; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertFalse(result); } - @Test - public void testGenerateTextFile() throws Exception { - String fileName = ""; - String fileData = ""; - boolean result; + public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsEqualThenLimit() { - // default test - result = GeneralUtility.generateTextFile(fileName, fileData); + final List<String> testString = Lists.newArrayList("testing","list","equal"); + final int limit = 18; + final int delimiterLength = 1; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertFalse(result); } - @Test - public void testIsBase64Encoded() throws Exception { - byte[] data = new byte[] { ' ' }; - boolean result; + public void validateIsExceedingLimitWithDelimiterReturnsTrueIfSumOfAllElementsLengthAndDelimiterLengthIsBiggerThenLimit() { + + final List<String> testString = Lists.newArrayList("long","testing","list","of","strings"); + final int limit = 20; + final int delimiterLength = 2; - // default test - result = GeneralUtility.isBase64Encoded(data); + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertTrue(result); } - @Test - public void testIsBase64Encoded_1() throws Exception { - String str = ""; - boolean result; + public void validateGetFilenameExtensionReturnsProperExtension() { + + final String testFile = "test.yaml"; - // default test - result = GeneralUtility.isBase64Encoded(str); + String extension = GeneralUtility.getFilenameExtension(testFile); + + assertEquals(extension, "yaml"); } - @Test - public void testIsExceedingLimit() throws Exception { - String str = ""; - int limit = 0; - boolean result; + public void validateCalculateMD5Base64EncodedByByteArrayReturnsCorrectString() { + + final String testStringToEncode = "testString"; + + String result = GeneralUtility.calculateMD5Base64EncodedByByteArray(testStringToEncode.getBytes()); - // test 1 - str = null; - result = GeneralUtility.isExceedingLimit(str, limit); - Assert.assertEquals(false, result); + final String encodedString = + org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes()); - // test 2 - str = ""; - result = GeneralUtility.isExceedingLimit(str, limit); - Assert.assertEquals(false, result); + assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result)); } - @Test - public void testIsExceedingLimit_1() throws Exception { - List<String> strList = null; - int limit = 0; - int delimiterLength = 0; - boolean result; + public void validateCalculateMD5Base64EncodedByStringReturnsCorrectString() { + + final String testStringToEncode = "testString"; + + String result = GeneralUtility.calculateMD5Base64EncodedByString(testStringToEncode); - // test 1 - strList = null; - result = GeneralUtility.isExceedingLimit(strList, limit, delimiterLength); - Assert.assertEquals(false, result); + final String encodedString = + org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes()); + + assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result)); } - @Test - public void testGetFilenameExtension() throws Exception { - String fileName = ""; - String result; + public void validateIsEmptyStringReturnTrueIfStringIsEmpty() { + + final String empty = ""; - // test 1 - fileName = null; - result = GeneralUtility.getFilenameExtension(fileName); - Assert.assertEquals("", result); + boolean result = GeneralUtility.isEmptyString(empty); - // test 2 - fileName = ""; - result = GeneralUtility.getFilenameExtension(fileName); - Assert.assertEquals("", result); + assertTrue(result); } - @Test - public void testCalculateMD5Base64EncodedByByteArray() throws Exception { - byte[] payload = new byte[] { ' ' }; - String result; + public void validateIsEmptyStringReturnTrueIfStringIsContainingOnlyWightSpaces() { + + final String empty = " \t "; - // default test - result = GeneralUtility.calculateMD5Base64EncodedByByteArray(payload); + boolean result = GeneralUtility.isEmptyString(empty); + + assertTrue(result); } - @Test - public void testCalculateMD5Base64EncodedByString() throws Exception { - String data = ""; - String result; + public void validateIsEmptyStringReturnFalseIfStringIsNotEmpty() { + + final String empty = "test"; - // default test - result = GeneralUtility.calculateMD5Base64EncodedByString(data); + boolean result = GeneralUtility.isEmptyString(empty); + + assertFalse(result); } - @Test - public void testIsEmptyString() throws Exception { - String str = ""; - boolean result; + public void validateIsEmptyStringReturnFalseIfStringIsNotEmptyAndSurroundedWithWightSpaces() { + + final String empty = " \ttest "; - // default test - result = GeneralUtility.isEmptyString(str); + boolean result = GeneralUtility.isEmptyString(empty); + + assertFalse(result); } + } diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GsonFactoryTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GsonFactoryTest.java index 3ed0904fba..4099a0bab0 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GsonFactoryTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GsonFactoryTest.java @@ -24,19 +24,15 @@ import org.junit.Test; import com.google.gson.Gson; +import static junit.framework.TestCase.assertEquals; -public class GsonFactoryTest { - - private GsonFactory createTestSubject() { - return new GsonFactory(); - } +public class GsonFactoryTest { @Test - public void testGetGson() throws Exception { - Gson result; + public void testGetGson() { + Gson result = GsonFactory.getGson(); - // default test - result = GsonFactory.getGson(); + assertEquals( result.getClass(), Gson.class); } } diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/HealthCheckUtilTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/HealthCheckUtilTest.java new file mode 100644 index 0000000000..3fc025eb22 --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/HealthCheckUtilTest.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.sdc.common.util; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.common.api.HealthCheckInfo; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class HealthCheckUtilTest { + + private HealthCheckUtil healthCheckUtil; + + private final String testComponent = "service"; + + @Mock + private HealthCheckInfo healthCheckInfo; + + private List<HealthCheckInfo> healthCheckInfos; + + @Before + public void setUp() { + healthCheckUtil = new HealthCheckUtil(); + healthCheckInfos = Collections.singletonList(healthCheckInfo); + when(healthCheckInfo.getHealthCheckComponent()).thenReturn(testComponent); + } + + @Test + public void validateGetAggregateStatusReturnsTrue() { + final Collection<String> excludes = Collections.emptyList(); + when(healthCheckInfo.getHealthCheckStatus()).thenReturn(HealthCheckInfo.HealthCheckStatus.UP); + + final boolean result = healthCheckUtil.getAggregateStatus(healthCheckInfos, excludes); + + assertTrue(result); + } + + @Test + public void validateGetAggregateStatusReturnsFalseIfStatusIsDown() { + final Collection<String> excludes = Collections.emptyList(); + when(healthCheckInfo.getHealthCheckStatus()).thenReturn(HealthCheckInfo.HealthCheckStatus.DOWN); + + final boolean result = healthCheckUtil.getAggregateStatus(healthCheckInfos, excludes); + + assertFalse(result); + } + + @Test + public void validateGetAggregateDescriptionReturnsProperDescription() { + final String parentDescription = ""; + when(healthCheckInfo.getHealthCheckStatus()).thenReturn(HealthCheckInfo.HealthCheckStatus.DOWN); + + final String result = healthCheckUtil.getAggregateDescription(healthCheckInfos, parentDescription); + + assertTrue(result.contains(testComponent)); + assertTrue(result.contains("Down")); + } + +} diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/HtmlCleanerTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/HtmlCleanerTest.java new file mode 100644 index 0000000000..d479bf7f05 --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/HtmlCleanerTest.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.sdc.common.util; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HtmlCleanerTest { + + @Test + public void validateStripHtmlReturnsStringWithNoHtml() { + final String testInput = " <div>testing <p>element</p> &value</div> "; + + String result = HtmlCleaner.stripHtml(testInput); + + assertEquals(result, " testing element &value "); + } + + @Test + public void validateStripHtmlReturnsInputIfIsEmpty() { + final String testInput = ""; + + String result = HtmlCleaner.stripHtml(testInput); + + assertEquals(result, testInput); + } + + @Test + public void validateStripHtmlReturnsInputIfTagsAreEmpty() { + final String testInput = "<>emptyTags<>"; + + String result = HtmlCleaner.stripHtml(testInput); + + assertEquals(result, testInput); + } + + @Test + public void validateStripHtmlReturnsStripedHtmlWithEscapeTrue() { + final String testInput = " <div>testing <p>element</p> &value</div> "; + + String result = HtmlCleaner.stripHtml(testInput, true); + + assertEquals(result, " testing element &value "); + } +} diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/HttpUtilTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/HttpUtilTest.java new file mode 100644 index 0000000000..c5920d1f3a --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/HttpUtilTest.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.sdc.common.util; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; +import fj.data.Either; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.sdc.fe.config.Configuration; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import static org.mockito.Mockito.when; + +public class HttpUtilTest { + + @Test + public void validateGetObjectFromJsonReturnsValidObjectFromValidJason() throws IOException { + + final Configuration testObject = new Configuration(); + testObject.setVersion("1.0.test"); + testObject.setThreadpoolSize(5); + + final String testObjectAsJson = new GsonBuilder().setPrettyPrinting().create().toJson(testObject); + + final ServletInputStream servletInputStream = new TestServletInputStream(testObjectAsJson); + + final HttpServletRequest request = + Mockito.mock(HttpServletRequest.class); + when(request.getInputStream()).thenReturn(servletInputStream); + + + Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class); + + assertTrue(result.isLeft()); + + Configuration returnedConfiguration = result.left().value(); + + assertEquals(returnedConfiguration.getVersion(),testObject.getVersion()); + assertEquals(returnedConfiguration.getBeProtocol(),testObject.getBeProtocol()); + assertEquals(returnedConfiguration.getThreadpoolSize(),testObject.getThreadpoolSize()); + } + + @Test + public void validateGetObjectFromJsonReturnsExceptionIfStreamThrowIOException() throws IOException { + + final String testException = "test exception"; + + final HttpServletRequest request = + Mockito.mock(HttpServletRequest.class); + when(request.getInputStream()).thenThrow(new IOException(testException)); + + Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class); + + assertTrue(result.isRight()); + assertEquals(result.right().value().getMessage(), testException); + } + + @Test + public void validateGetObjectFromJsonReturnsExceptionIfInputStringIsInvalid() throws IOException { + final ServletInputStream servletInputStream = new TestServletInputStream("Wrong Json Object"); + + final HttpServletRequest request = + Mockito.mock(HttpServletRequest.class); + when(request.getInputStream()).thenReturn(servletInputStream); + + + Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class); + + assertTrue(result.isRight()); + assertEquals(result.right().value().getClass(), JsonSyntaxException.class); + } + + @Test + public void validateGetObjectFromJsonReturnsExceptionIfInputIsNull() throws IOException { + Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(null, Configuration.class); + + assertTrue(result.isRight()); + assertEquals(result.right().value().getClass(), NullPointerException.class); + } + + class TestServletInputStream extends ServletInputStream { + + private InputStream testStream; + + TestServletInputStream(String testJson) { + testStream = new ByteArrayInputStream(testJson.getBytes()); + } + + + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setReadListener(ReadListener readListener) { } + + @Override + public int read() throws IOException { + return testStream.read(); + } + } +} diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/JsonUtilsTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/JsonUtilsTest.java new file mode 100644 index 0000000000..6021b75c40 --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/JsonUtilsTest.java @@ -0,0 +1,128 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.sdc.common.util; + +import com.google.gson.JsonArray; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class JsonUtilsTest { + + private String testProperty01 = "testProperty01"; + private String testValue01 = "testValue01"; + private String testProperty02 = "testProperty02"; + private String testValue02 = "testValue02"; + private String expectedJsonObject = "" + + "{" + + "\""+testProperty01+"\":\""+testValue01+"\"," + + "\""+testProperty02+"\":\""+testValue02+"\"" + + "}"; + + @Test + public void validateToStringConvertsJsonObjectToValidString() { + String result = JsonUtils.toString(generateJsonObject()); + + assertEquals(result, expectedJsonObject); + } + + @Test + public void validateToStringReturnsNullIfJsonElementIsNull() { + String result = JsonUtils.toString(null); + + assertNull(result); + } + + @Test(expected = UnsupportedOperationException.class) + public void validateToStringFromJsonArrayThrowsUnsupportedOperationException() { + JsonUtils.toString(generateJsonArray()); + } + + @Test + public void validateToStringReturnsNullIfJsonElementIsJsonNull() { + String result = JsonUtils.toString(new JsonNull()); + + assertNull(result); + } + + @Test + public void validateContainsEntryReturnsTrueIfKeyIsPresentInJsonObject() { + boolean result = JsonUtils.containsEntry(generateJsonObject(),testProperty01); + + assertTrue(result); + } + + @Test + public void validateContainsEntryReturnsFalseIfKeyIsNotPresentInJsonObject() { + boolean result = JsonUtils.containsEntry(new JsonObject(),testProperty01); + + assertFalse(result); + } + + @Test + public void validateIsEmptyJsonReturnsTrueIfInputIsEmpty() { + boolean result = JsonUtils.isEmptyJson(new JsonObject()); + + assertTrue(result); + } + + @Test + public void validateIsEmptyJsonReturnsFalseIfInputIsNotEmpty() { + boolean result = JsonUtils.isEmptyJson(generateJsonArray().get(0)); + + assertFalse(result); + } + + @Test + public void validateIsNullOrEmptyReturnsTrueIfInputIsEmpty() { + boolean result = JsonUtils.isJsonNullOrEmpty(new JsonObject()); + + assertTrue(result); + } + + @Test + public void validateIsNullOrEmptyReturnsFalseIfInputIsNotNull() { + boolean result = JsonUtils.isJsonNullOrEmpty(generateJsonObject()); + + assertFalse(result); + } + + private JsonObject generateJsonObject() { + final JsonObject testObject = new JsonObject(); + testObject.addProperty(testProperty01,testValue01); + testObject.addProperty(testProperty02,testValue02); + + return testObject; + } + + private JsonArray generateJsonArray() { + final JsonArray testArray = new JsonArray(); + testArray.add(generateJsonObject()); + + return testArray; + } + +} diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/PairUtilsTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/PairUtilsTest.java new file mode 100644 index 0000000000..999c76144e --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/PairUtilsTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.sdc.common.util; + +import com.google.common.collect.Lists; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertTrue; + +public class PairUtilsTest { + + final ImmutablePair<String,String> immutablePair01 = + new ImmutablePair<>("leftValue01","rightValue01"); + final ImmutablePair<String,String> immutablePair02 = + new ImmutablePair<>("leftValue02","rightValue02"); + final ImmutablePair<String,String> immutablePair03 = + new ImmutablePair<>("leftValue03","rightValue03"); + + @Test + public void validateLeftSequenceReturnsListOfLeftElements() { + + List<String> result = PairUtils.leftSequence(generateImmutableListOfPairs()); + + assertTrue(result.containsAll( + Lists.newArrayList( + immutablePair01.left, + immutablePair02.left, + immutablePair03.left + )) + ); + } + + @Test + public void validateRightSequenceReturnsListOfRightElements() { + + List<String> result = PairUtils.rightSequence(generateListOfPairs()); + + assertTrue(result.containsAll( + Lists.newArrayList( + immutablePair01.right, + immutablePair02.right, + immutablePair03.right + )) + ); + } + + private List<Pair<String,String>> generateListOfPairs() { + return Lists.newArrayList( + immutablePair01, + immutablePair02, + immutablePair03 + ); + } + + private List<ImmutablePair<String,String>> generateImmutableListOfPairs() { + return Lists.newArrayList( + immutablePair01, + immutablePair02, + immutablePair03 + ); + } +} diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/SerializationUtilsTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/SerializationUtilsTest.java index bcdce19c9c..0e5c1a9575 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/util/SerializationUtilsTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/SerializationUtilsTest.java @@ -22,38 +22,88 @@ package org.openecomp.sdc.common.util; import fj.data.Either; import org.junit.Test; +import org.openecomp.sdc.fe.config.Configuration; + +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class SerializationUtilsTest { - private SerializationUtils createTestSubject() { - return new SerializationUtils(); + @Test + public void testSerializeAndDeserializeReturnsCorrectObject() { + + final List<String> list = Collections.singletonList("testList"); + + Either<byte[], Boolean> serializeResult = SerializationUtils.serialize(list); + assertTrue(serializeResult.isLeft()); + byte[] serializeList = serializeResult .left().value(); + + Either<Object, Boolean> deserializeResult = SerializationUtils.deserialize(serializeList); + assertTrue(deserializeResult.isLeft()); + List<String> deserializeList = (List<String>) deserializeResult.left().value(); + + assertEquals(list, deserializeList); } @Test - public void testSerialize() throws Exception { - Object object = null; - Either<byte[], Boolean> result; + public void testSerializeReturnsFalseIfObjectIsNotSerializable() { + + final Configuration configuration = new Configuration(); - // default test - result = SerializationUtils.serialize(object); + Either<byte[], Boolean> serializeResult = SerializationUtils.serialize(configuration); + assertTrue(serializeResult.isRight()); + assertFalse(serializeResult.right().value()); } @Test - public void testDeserialize() throws Exception { - byte[] bytes = new byte[] { ' ' }; - Either<Object, Boolean> result; + public void testDeserializeReturnsFalseIfObjectIsNotSerializable() { + + String testBytes = "wrongBytesToDeserialize"; - // default test - result = SerializationUtils.deserialize(bytes); + Either<Object, Boolean> serializeResult = SerializationUtils.deserialize(testBytes.getBytes()); + assertTrue(serializeResult.isRight()); + assertFalse(serializeResult.right().value()); } @Test - public void testSerializeExt() throws Exception { - Object object = null; - Either<byte[], Boolean> result; + public void testSerializeExtAndDeserializeExtReturnsCorrectObject() { + + final List<String> list = Collections.singletonList("testList"); + + Either<byte[], Boolean> serializeResult = SerializationUtils.serializeExt(list); + assertTrue(serializeResult.isLeft()); + byte[] serializeList = serializeResult .left().value(); - // default test - result = SerializationUtils.serializeExt(object); + Either<List, Boolean> deserializeResult = + SerializationUtils.deserializeExt(serializeList,List.class, "testComponent"); + assertTrue(deserializeResult.isLeft()); + List<String> deserializeList = deserializeResult.left().value(); + + assertEquals(list, deserializeList); + } + + @Test + public void testSerializeExtReturnsFalseIfObjectIsNotSerializable() { + + final Configuration configuration = new Configuration(); + + Either<byte[], Boolean> serializeResult = SerializationUtils.serializeExt(configuration); + assertTrue(serializeResult.isRight()); + assertFalse(serializeResult.right().value()); } + @Test + public void testDeserializeExtReturnsFalseIfObjectIsNotSerializable() { + + String testBytes = "wrongBytesToDeserialize"; + + Either<List, Boolean> serializeResult = + SerializationUtils.deserializeExt(testBytes.getBytes(),List.class, "testComponent"); + assertTrue(serializeResult.isRight()); + assertFalse(serializeResult.right().value()); + } } diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java new file mode 100644 index 0000000000..c8b2920f81 --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.sdc.common.util; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ThreadLocalsHolderTest { + + @Test + public void validateSetAngGetMdcProcessReturnsValidThreadLocalBoolean() { + final boolean testBoolean01 = false; + final boolean testBoolean02 = true; + + ThreadLocalsHolder.setMdcProcessed(testBoolean01); + assertEquals(ThreadLocalsHolder.isMdcProcessed(), testBoolean01); + ThreadLocalsHolder.setMdcProcessed(testBoolean02); + assertEquals(ThreadLocalsHolder.isMdcProcessed(), testBoolean02); + } + + @Test + public void validateSetAngGetUUIDReturnsValidThreadLocalString() { + final String UUID01 = "testId01"; + final String UUID02 = "testId02"; + + ThreadLocalsHolder.setUuid(UUID01); + assertEquals(ThreadLocalsHolder.getUuid(), UUID01); + ThreadLocalsHolder.setUuid(UUID02); + assertEquals(ThreadLocalsHolder.getUuid(), UUID02); + } + + @Test + public void validateSetAngGetRequestStartTimeReturnsValidThreadLocalString() { + final Long requestStartTime01 = 10L; + final Long requestStartTime02 = 50L; + + ThreadLocalsHolder.setRequestStartTime(requestStartTime01); + assertEquals(ThreadLocalsHolder.getRequestStartTime(), requestStartTime01); + ThreadLocalsHolder.setRequestStartTime(requestStartTime02); + assertEquals(ThreadLocalsHolder.getRequestStartTime(), requestStartTime02); + } + + @Test + public void validateCleanupResetsAllParameters() { + final Long requestStartTime = 10L; + final String UUID = "testId01"; + final boolean testBoolean = true; + + ThreadLocalsHolder.setMdcProcessed(testBoolean); + ThreadLocalsHolder.setUuid(UUID); + ThreadLocalsHolder.setRequestStartTime(requestStartTime); + + ThreadLocalsHolder.cleanup(); + + assertNull(ThreadLocalsHolder.getRequestStartTime()); + assertNull(ThreadLocalsHolder.getUuid()); + assertEquals(ThreadLocalsHolder.isMdcProcessed(), false); + } +} |