diff options
17 files changed, 381 insertions, 64 deletions
@@ -42,6 +42,7 @@ <snakeyaml.version>1.14</snakeyaml.version> <guava.version>21.0</guava.version> <jetty.version>9.4.18.v20190429</jetty.version> + <bean-matchers.version>0.11</bean-matchers.version> <!--<sonar.skipDesign>true</sonar.skipDesign>--> diff --git a/sdc-distribution-ci/pom.xml b/sdc-distribution-ci/pom.xml index 1914d07..7b32b37 100644 --- a/sdc-distribution-ci/pom.xml +++ b/sdc-distribution-ci/pom.xml @@ -45,13 +45,6 @@ </dependency> <dependency> - <groupId>commons-codec</groupId> - <artifactId>commons-codec</artifactId> - <version>1.9</version> - <scope>compile</scope> - </dependency> - - <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> diff --git a/sdc-distribution-ci/src/main/java/org/onap/test/Decoder.java b/sdc-distribution-ci/src/main/java/org/onap/test/Decoder.java index 384d26d..fc029ff 100644 --- a/sdc-distribution-ci/src/main/java/org/onap/test/Decoder.java +++ b/sdc-distribution-ci/src/main/java/org/onap/test/Decoder.java @@ -3,13 +3,14 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -23,22 +24,21 @@ package org.onap.test; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; -import org.apache.commons.codec.binary.Base64; +import java.util.Base64; public class Decoder { public static String encode(byte[] byteArrayToEncode) { - return new String(Base64.encodeBase64(byteArrayToEncode)); + return new String(Base64.getEncoder().encode(byteArrayToEncode)); } public static String decode(String strEncoded) { - return new String(Base64.decodeBase64(strEncoded)); + return new String(Base64.getDecoder().decode(strEncoded)); } public static String readFileToString(String file) throws IOException { - try (FileReader fileReader = new FileReader(file); - BufferedReader reader = new BufferedReader(fileReader)) { + try (FileReader fileReader = new FileReader(file); BufferedReader reader = new BufferedReader(fileReader)) { String line; StringBuilder stringBuilder = new StringBuilder(); String ls = System.getProperty("line.separator"); @@ -47,7 +47,7 @@ public class Decoder { stringBuilder.append(line); stringBuilder.append(ls); } - + reader.close(); fileReader.close(); return stringBuilder.toString(); diff --git a/sdc-distribution-client/pom.xml b/sdc-distribution-client/pom.xml index a1999f3..00a1149 100644 --- a/sdc-distribution-client/pom.xml +++ b/sdc-distribution-client/pom.xml @@ -62,13 +62,6 @@ <version>2.5</version> </dependency> - <dependency> - <groupId>commons-codec</groupId> - <artifactId>commons-codec</artifactId> - <version>1.9</version> - <scope>compile</scope> - </dependency> - <!-- http client --> <dependency> <groupId>org.apache.httpcomponents</groupId> @@ -100,6 +93,12 @@ <scope>compile</scope> </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>${guava.version}</version> + </dependency> + <!-- TEST --> <dependency> <groupId>org.eclipse.jetty</groupId> @@ -130,9 +129,9 @@ </dependency> <dependency> - <groupId>com.google.guava</groupId> - <artifactId>guava</artifactId> - <version>${guava.version}</version> + <groupId>com.google.code.bean-matchers</groupId> + <artifactId>bean-matchers</artifactId> + <version>${bean-matchers.version}</version> <scope>test</scope> </dependency> diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java b/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java index a93b4a7..7871816 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java @@ -3,6 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -29,6 +30,7 @@ import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; +import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -41,7 +43,6 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; -import org.apache.commons.codec.binary.Base64; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; @@ -83,7 +84,7 @@ public class HttpAsdcClient implements IHttpAsdcClient { initSSL(username, password, configuraion.getKeyStorePath(), configuraion.getKeyStorePassword(), configuraion.activateServerTLSAuth()); String userNameAndPassword = username + ":" + password; - this.authHeaderValue = "Basic " + Base64.encodeBase64String(userNameAndPassword.getBytes()); + this.authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(userNameAndPassword.getBytes()); } // @SuppressWarnings("deprecation") diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java index 1d46186..de5d1ec 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java @@ -3,6 +3,7 @@ * SDC * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -27,7 +28,7 @@ public class StatusDataImpl implements IStatusData { private String distributionID; private String consumerID; - private long timestamp; + private Long timestamp; private String artifactURL; private DistributionStatusEnum status; private String componentName; @@ -56,7 +57,7 @@ public class StatusDataImpl implements IStatusData { return timestamp; } - public void setTimestamp(long timestamp) { + public void setTimestamp(Long timestamp) { this.timestamp = timestamp; } @@ -81,7 +82,7 @@ public class StatusDataImpl implements IStatusData { @Override public String toString() { - return "StatusDataImpl [distributionId=" + distributionID + ", consumerId=" + consumerID + ", timestamp=" + timestamp + ", artifactURL=" + artifactURL + ", status=" + status + ", errorReason=" + errorReason + "]"; + return "StatusDataImpl [distributionID=" + distributionID + ", consumerID=" + consumerID + ", timestamp=" + timestamp + ", artifactURL=" + artifactURL + ", status=" + status + ", errorReason=" + errorReason + "]"; } @Override diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java index ac2a904..d550b74 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/GeneralUtils.java @@ -3,6 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -20,13 +21,15 @@ package org.onap.sdc.utils; +import com.google.common.base.Charsets; +import com.google.common.hash.Hashing; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Base64; import java.util.List; import java.util.regex.Pattern; -import org.apache.commons.codec.binary.Base64; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.impl.DistributionClientResultImpl; import org.slf4j.Logger; @@ -36,24 +39,21 @@ import fj.data.Either; public class GeneralUtils { private static final Logger LOGGER = LoggerFactory.getLogger(GeneralUtils.class.getName()); - public static final int STRING_LENGTH_DIVIDER = 4; + private static final int STRING_LENGTH_DIVIDER = 4; private GeneralUtils() { - } public static String calculateMD5(String data) { - String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data); + String calculatedMd5 = Hashing.md5().hashString(data, Charsets.UTF_8).toString(); // encode base-64 result - byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes()); - String encodeBase64Str = new String(encodeBase64); - return encodeBase64Str; - + byte[] encodeBase64 = Base64.getEncoder().encode(calculatedMd5.getBytes()); + return new String(encodeBase64); } public static String calculateMD5(byte[] decodedPayload) { - String decodedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(decodedPayload); - byte[] encodeMd5 = Base64.encodeBase64(decodedMd5.getBytes()); + String decodedMd5 = Hashing.md5().hashBytes(decodedPayload).toString(); + byte[] encodeMd5 = Base64.getEncoder().encode(decodedMd5.getBytes()); return new String(encodeMd5); } @@ -61,7 +61,7 @@ public class GeneralUtils { boolean isEncoded = false; try { // If no exception is caught, then it is possibly a base64 encoded string - byte[] data = Base64.decodeBase64(str); + byte[] data = Base64.getDecoder().decode(str); // checks if the string was properly padded to the isEncoded = ((str.length() % STRING_LENGTH_DIVIDER == 0) && (Pattern.matches("\\A[a-zA-Z0-9/+]+={1,2}\\z", str))); diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/api/asdc/RegistrationRequestTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/api/asdc/RegistrationRequestTest.java new file mode 100644 index 0000000..50ff94c --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/api/asdc/RegistrationRequestTest.java @@ -0,0 +1,45 @@ +/*- + * ============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.onap.sdc.api.asdc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; +import java.util.List; +import org.junit.Test; + +public class RegistrationRequestTest { + + private static final List<String> DIST_ENV_END_POINTS = Collections.emptyList(); + private static final boolean IS_CONSUMER_TO_SDC_DISTR_STATUS_TOPIC = true; + private static final String ENV_NAME = "ENV_NAME"; + private static final String API_KEY = "API_KEY"; + + @Test + public void testConstructorShouldSetProperties() { + RegistrationRequest registrationRequest = + new RegistrationRequest(API_KEY, ENV_NAME, IS_CONSUMER_TO_SDC_DISTR_STATUS_TOPIC, DIST_ENV_END_POINTS); + assertEquals(registrationRequest.getApiPublicKey(), API_KEY); + assertEquals(registrationRequest.getDistEnvEndPoints(), DIST_ENV_END_POINTS); + assertEquals(registrationRequest.getDistrEnvName(), ENV_NAME); + assertTrue(registrationRequest.getIsConsumerToSdcDistrStatusTopic()); + } +}
\ No newline at end of file diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/api/asdc/ServerListResponseTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/api/asdc/ServerListResponseTest.java new file mode 100644 index 0000000..449eca8 --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/api/asdc/ServerListResponseTest.java @@ -0,0 +1,32 @@ +/*- + * ============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.onap.sdc.api.asdc; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class ServerListResponseTest { + @Test + public void shouldHaveValidGettersAndSetters() { + assertThat(ServerListResponse.class, hasValidGettersAndSetters()); + } +}
\ No newline at end of file diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientFactory.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientFactory.java index f7a0041..6262f06 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientFactory.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientFactory.java @@ -3,6 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -21,7 +22,6 @@ package org.onap.sdc.impl; import org.onap.sdc.api.IDistributionClient; -import org.onap.sdc.impl.mock.DistributionClientStubImpl; public class DistributionClientFactory { @@ -32,7 +32,4 @@ public class DistributionClientFactory { return new DistributionClientImpl(); } - public static IDistributionClient createMockDistributionClient() { - return new DistributionClientStubImpl(); - } } diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionStatusMessageImplTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionStatusMessageImplTest.java new file mode 100644 index 0000000..bf78a2a --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionStatusMessageImplTest.java @@ -0,0 +1,138 @@ +/*- + * ============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.onap.sdc.impl; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.sdc.api.consumer.IComponentDoneStatusMessage; +import org.onap.sdc.api.consumer.IDistributionStatusMessage; +import org.onap.sdc.api.consumer.IFinalDistrStatusMessage; +import org.onap.sdc.utils.DistributionStatusEnum; + +public class DistributionStatusMessageImplTest { + + private static final String ARTIFACT = "ARTIFACT"; + private static final String DISTRIBUTION_ID = "DISTRIBUTION_ID"; + private static final String CONSUMER_ID = "CONSUMER_ID"; + private static final String COMPONENT_NAME = "COMPONENT_NAME"; + + @Test + public void shouldProperlySetPropertiesFromIDistributionStatusMessage() { + IDistributionStatusMessage copyFrom = new IDistributionStatusMessage() { + @Override + public String getArtifactURL() { + return ARTIFACT; + } + + @Override + public String getDistributionID() { + return DISTRIBUTION_ID; + } + + @Override + public String getConsumerID() { + return CONSUMER_ID; + } + + @Override + public long getTimestamp() { + return 0; + } + + @Override + public DistributionStatusEnum getStatus() { + return DistributionStatusEnum.ALREADY_DEPLOYED; + } + }; + + DistributionStatusMessageImpl result = new DistributionStatusMessageImpl(copyFrom); + assertEquals(result.getArtifactURL(), copyFrom.getArtifactURL()); + assertEquals(result.getConsumerID(), copyFrom.getConsumerID()); + assertEquals(result.getDistributionID(), copyFrom.getDistributionID()); + assertEquals(result.getStatus(), copyFrom.getStatus()); + assertEquals(result.getTimestamp(), copyFrom.getTimestamp()); + } + + @Test + public void shouldProperlySetPropertiesFromIComponentDoneStatusMessage() { + IComponentDoneStatusMessage copyFrom = new IComponentDoneStatusMessage() { + @Override + public String getComponentName() { + return COMPONENT_NAME; + } + + @Override + public String getDistributionID() { + return DISTRIBUTION_ID; + } + + @Override + public String getConsumerID() { + return CONSUMER_ID; + } + + @Override + public long getTimestamp() { + return 0; + } + + @Override + public DistributionStatusEnum getStatus() { + return DistributionStatusEnum.ALREADY_DEPLOYED; + } + }; + + DistributionStatusMessageImpl result = new DistributionStatusMessageImpl(copyFrom); + assertEquals(result.getArtifactURL(), copyFrom.getArtifactURL()); + assertEquals(result.getConsumerID(), copyFrom.getConsumerID()); + assertEquals(result.getDistributionID(), copyFrom.getDistributionID()); + assertEquals(result.getStatus(), copyFrom.getStatus()); + assertEquals(result.getTimestamp(), copyFrom.getTimestamp()); + } + + @Test + public void shouldProperlySetPropertiesFromIFinalDistrStatusMessage() { + IFinalDistrStatusMessage copyFrom = new IFinalDistrStatusMessage() { + + @Override + public String getDistributionID() { + return DISTRIBUTION_ID; + } + + @Override + public long getTimestamp() { + return 0; + } + + @Override + public DistributionStatusEnum getStatus() { + return DistributionStatusEnum.ALREADY_DEPLOYED; + } + }; + + DistributionStatusMessageImpl result = new DistributionStatusMessageImpl(copyFrom); + assertEquals(result.getConsumerID(), copyFrom.getConsumerID()); + assertEquals(result.getDistributionID(), copyFrom.getDistributionID()); + assertEquals(result.getStatus(), copyFrom.getStatus()); + assertEquals(result.getTimestamp(), copyFrom.getTimestamp()); + } + +}
\ No newline at end of file diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/StatusDataImplTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/StatusDataImplTest.java new file mode 100644 index 0000000..82416fa --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/StatusDataImplTest.java @@ -0,0 +1,42 @@ +/*- + * ============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.onap.sdc.impl; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanToStringExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class StatusDataImplTest { + + private static final String COMPONENT_NAME = "componentName"; + private static final String ERROR_REASON = "errorReason"; + + @Test + public void shouldHaveValidGettersAndSetters() { + assertThat(StatusDataImpl.class, hasValidGettersAndSettersExcluding(COMPONENT_NAME, ERROR_REASON)); + } + + @Test + public void shouldHaveValidToString() { + assertThat(StatusDataImpl.class, hasValidBeanToStringExcluding(COMPONENT_NAME, ERROR_REASON)); + } +}
\ No newline at end of file diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/mock/DistributionClientDownloadResultStubImpl.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientDownloadResultStubImpl.java index 5ffb655..5ffb655 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/mock/DistributionClientDownloadResultStubImpl.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientDownloadResultStubImpl.java diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/mock/DistributionClientResultStubImpl.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientResultStubImpl.java index d176608..d176608 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/mock/DistributionClientResultStubImpl.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientResultStubImpl.java diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java index 40e1869..40e1869 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/utils/ArtifactsUtils.java b/sdc-distribution-client/src/test/java/org/onap/sdc/utils/ArtifactsUtils.java index b562ab7..dad6d4a 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/utils/ArtifactsUtils.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/utils/ArtifactsUtils.java @@ -3,13 +3,14 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -21,26 +22,27 @@ package org.onap.sdc.utils; - -import org.apache.commons.codec.binary.Base64; +import java.util.Base64; import org.onap.sdc.impl.mock.DistributionClientDownloadResultStubImpl; public class ArtifactsUtils { - static DistributionClientDownloadResultStubImpl distributionClientDownloadResultStubImpl = new DistributionClientDownloadResultStubImpl(); - - public static byte [] getArtifactPayload(){ - return distributionClientDownloadResultStubImpl.getArtifactPayload(); - } - - public static String getValidChecksum(){ - - String payloadStr = new String(distributionClientDownloadResultStubImpl.getArtifactPayload()); - - byte[] decodedPayload = Base64.decodeBase64(payloadStr); - String checkSum = GeneralUtils.calculateMD5 (new String(decodedPayload)); - - return checkSum; - } + + static DistributionClientDownloadResultStubImpl distributionClientDownloadResultStubImpl = + new DistributionClientDownloadResultStubImpl(); + + public static byte[] getArtifactPayload() { + return distributionClientDownloadResultStubImpl.getArtifactPayload(); + } + + public static String getValidChecksum() { + + String payloadStr = new String(distributionClientDownloadResultStubImpl.getArtifactPayload()); + + byte[] decodedPayload = Base64.getDecoder().decode(payloadStr); + String checkSum = GeneralUtils.calculateMD5(new String(decodedPayload)); + + return checkSum; + } } diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/utils/GeneralUtilsTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/utils/GeneralUtilsTest.java new file mode 100644 index 0000000..3ed80c3 --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/utils/GeneralUtilsTest.java @@ -0,0 +1,66 @@ +/*- + * ============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.onap.sdc.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.google.common.base.Charsets; +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import java.util.Base64; +import org.junit.Test; + +public class GeneralUtilsTest { + + private static final String TEXT_TO_CODE = "This is example text."; + + @Test + public void shouldCalculateMD5ForBytes() { + String hashed = GeneralUtils.calculateMD5(TEXT_TO_CODE.getBytes()); + byte[] decoded = Base64.getDecoder().decode(hashed); + HashCode expected = Hashing.md5().hashString(TEXT_TO_CODE, Charsets.UTF_8); + assertEquals(expected.toString(), new String(decoded)); + } + + @Test + public void shouldCalculateMD5ForString() { + String hashed = GeneralUtils.calculateMD5(TEXT_TO_CODE); + byte[] decoded = Base64.getDecoder().decode(hashed); + HashCode expected = Hashing.md5().hashString(TEXT_TO_CODE, Charsets.UTF_8); + assertEquals(expected.toString(), new String(decoded)); + } + + @Test + public void shouldValidateBase64EncodedString() { + HashCode expected = Hashing.md5().hashString(TEXT_TO_CODE, Charsets.UTF_8); + String base64String = Base64.getEncoder().encodeToString(expected.asBytes()); + assertTrue(GeneralUtils.isBase64Encoded(base64String)); + } + + @Test + public void shouldInvalidateBase64EncodedString() { + String base64String = Base64.getEncoder().encodeToString(TEXT_TO_CODE.getBytes()); + assertFalse(GeneralUtils.isBase64Encoded(base64String)); + } + +}
\ No newline at end of file |