From a316844b9606b36e77c14531b37a94ae6aaa040c Mon Sep 17 00:00:00 2001 From: Eric Kerwin Date: Fri, 22 Sep 2017 14:53:53 -0400 Subject: [PATCH] creating a more pure api --- pom.xml | 17 +- .../phonehome/PhoneHomeClient.java | 78 ------ .../phonehome/PhoneHomeRequestBody.java | 4 +- .../PhoneHomeRequestBodyBuilder.java | 175 ------------- .../PhoneHomeRequestBodyValidator.java | 129 ---------- .../phonehome/enums/BlackDuckName.java | 1 + .../enums/PhoneHomeRequestFieldEnum.java | 46 ---- .../exception/PhoneHomeException.java | 44 ---- ...g.restlet.engine.converter.ConverterHelper | 1 - .../phonehome/PhoneHomeClientUnitTest.java | 235 ------------------ 10 files changed, 3 insertions(+), 727 deletions(-) delete mode 100644 src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeClient.java delete mode 100644 src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyBuilder.java delete mode 100644 src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyValidator.java delete mode 100644 src/main/java/com/blackducksoftware/integration/phonehome/enums/PhoneHomeRequestFieldEnum.java delete mode 100644 src/main/java/com/blackducksoftware/integration/phonehome/exception/PhoneHomeException.java delete mode 100644 src/main/resources/META-INF/services/org.restlet.engine.converter.ConverterHelper delete mode 100644 src/test/java/com/blackducksoftware/integration/phonehome/PhoneHomeClientUnitTest.java diff --git a/pom.xml b/pom.xml index 95a5ef3..0685dc4 100644 --- a/pom.xml +++ b/pom.xml @@ -9,22 +9,7 @@ phone-home-api - 4.2.2-SNAPSHOT + 5.0.0 Phone Home API - http://maven.apache.org - - - - com.blackducksoftware.integration - hub-common-rest - 2.4.1 - - - org.mock-server - mockserver-netty - 3.10.4 - test - - diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeClient.java b/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeClient.java deleted file mode 100644 index daf4393..0000000 --- a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeClient.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Phone Home API - * - * Copyright (C) 2017 Black Duck Software, Inc. - * http://www.blackducksoftware.com/ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package com.blackducksoftware.integration.phonehome; - -import java.net.MalformedURLException; -import java.net.URL; - -import com.blackducksoftware.integration.exception.IntegrationException; -import com.blackducksoftware.integration.hub.request.HubRequest; -import com.blackducksoftware.integration.hub.rest.RestConnection; -import com.blackducksoftware.integration.hub.rest.UnauthenticatedRestConnection; -import com.blackducksoftware.integration.log.IntLogger; -import com.blackducksoftware.integration.phonehome.exception.PhoneHomeException; - -public class PhoneHomeClient { - public static final String PHONE_HOME_BACKEND = "https://collect.blackducksoftware.com"; - private final IntLogger logger; - private URL phoneHomeBackendUrl; - private final RestConnection baseConnection; - - public PhoneHomeClient(final IntLogger logger, final RestConnection restConnection) { - this.logger = logger; - try { - this.phoneHomeBackendUrl = new URL(PHONE_HOME_BACKEND); - } catch (final MalformedURLException e) { - phoneHomeBackendUrl = null; - } - this.baseConnection = restConnection; - } - - public PhoneHomeClient(final IntLogger logger, final URL phoneHomeBackendUrl, final RestConnection restConnection) { - this.logger = logger; - this.phoneHomeBackendUrl = phoneHomeBackendUrl; - this.baseConnection = restConnection; - } - - public void postPhoneHomeRequest(final PhoneHomeRequestBody phoneHomeRequestBody) throws PhoneHomeException { - if (phoneHomeBackendUrl == null) { - throw new PhoneHomeException("No phone home server found."); - } - logger.debug("Phoning home to " + phoneHomeBackendUrl); - final RestConnection restConnection = new UnauthenticatedRestConnection(logger, phoneHomeBackendUrl, baseConnection.timeout); - restConnection.proxyHost = baseConnection.proxyHost; - restConnection.proxyPort = baseConnection.proxyPort; - restConnection.proxyNoHosts = baseConnection.proxyNoHosts; - restConnection.proxyUsername = baseConnection.proxyUsername; - restConnection.proxyPassword = baseConnection.proxyPassword; - restConnection.alwaysTrustServerCertificate = baseConnection.alwaysTrustServerCertificate; - final HubRequest request = new HubRequest(restConnection); - try { - request.executePost(restConnection.gson.toJson(phoneHomeRequestBody)); - } catch (final IntegrationException e) { - throw new PhoneHomeException(e.getMessage(), e); - } - } - -} diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBody.java b/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBody.java index f09935c..325201f 100644 --- a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBody.java +++ b/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBody.java @@ -23,12 +23,10 @@ */ package com.blackducksoftware.integration.phonehome; -import java.io.Serializable; import java.util.Map; import java.util.Objects; -public class PhoneHomeRequestBody implements Serializable { - private static final long serialVersionUID = 5604676370200060866L; +public class PhoneHomeRequestBody { public static final PhoneHomeRequestBody DO_NOT_PHONE_HOME = null; private final String regId; private final String source; diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyBuilder.java b/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyBuilder.java deleted file mode 100644 index 69fb161..0000000 --- a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyBuilder.java +++ /dev/null @@ -1,175 +0,0 @@ -/** - * Phone Home API - * - * Copyright (C) 2017 Black Duck Software, Inc. - * http://www.blackducksoftware.com/ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package com.blackducksoftware.integration.phonehome; - -import java.io.UnsupportedEncodingException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.codec.digest.MessageDigestAlgorithms; - -import com.blackducksoftware.integration.builder.AbstractBuilder; -import com.blackducksoftware.integration.phonehome.enums.BlackDuckName; -import com.blackducksoftware.integration.phonehome.enums.PhoneHomeRequestFieldEnum; -import com.blackducksoftware.integration.phonehome.enums.PhoneHomeSource; -import com.blackducksoftware.integration.phonehome.enums.ThirdPartyName; - -public class PhoneHomeRequestBodyBuilder extends AbstractBuilder{ - private String registrationId; - private String hostName; - private String blackDuckName; - private String blackDuckVersion; - private String thirdPartyName; - private String thirdPartyVersion; - private String pluginVersion; - private String source; - private final Map metaDataMap = new HashMap<>(); - - @Override - public PhoneHomeRequestBody buildObject() { - String hubIdentifier; - try { - hubIdentifier = registrationId == null ? md5Hash(hostName) : registrationId; - } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { - hubIdentifier = null; - } - final Map infoMap = metaDataMap; - infoMap.put(PhoneHomeRequestFieldEnum.BLACKDUCKNAME.getKey(), blackDuckName); - infoMap.put(PhoneHomeRequestFieldEnum.BLACKDUCKVERSION.getKey(), blackDuckVersion); - infoMap.put(PhoneHomeRequestFieldEnum.THIRDPARTYNAME.getKey(), thirdPartyName); - infoMap.put(PhoneHomeRequestFieldEnum.THIRDPARTYVERSION.getKey(), thirdPartyVersion); - infoMap.put(PhoneHomeRequestFieldEnum.PLUGINVERSION.getKey(), pluginVersion); - final PhoneHomeRequestBody info = new PhoneHomeRequestBody(hubIdentifier, source, infoMap); - return info; - } - - @Override - public PhoneHomeRequestBodyValidator createValidator() { - final PhoneHomeRequestBodyValidator phoneHomeRequestValidator = new PhoneHomeRequestBodyValidator(); - phoneHomeRequestValidator.setBlackDuckName(blackDuckName); - phoneHomeRequestValidator.setBlackDuckVersion(blackDuckVersion); - phoneHomeRequestValidator.setHostName(hostName); - phoneHomeRequestValidator.setPluginVersion(pluginVersion); - phoneHomeRequestValidator.setRegistrationId(registrationId); - phoneHomeRequestValidator.setThirdPartyName(thirdPartyName); - phoneHomeRequestValidator.setThirdPartyVersion(thirdPartyVersion); - phoneHomeRequestValidator.setSource(source); - return phoneHomeRequestValidator; - } - - public String md5Hash(final String string) throws NoSuchAlgorithmException, UnsupportedEncodingException { - final MessageDigest md = MessageDigest.getInstance(MessageDigestAlgorithms.MD5); - final byte[] hashedBytes = md.digest(string.getBytes("UTF-8")); - return DigestUtils.md5Hex(hashedBytes); - } - - public Map getMetaDataMap() { - return metaDataMap; - } - - public void addToMetaDataMap(final String key, final String value){ - metaDataMap.put(key, value); - } - - - public String getRegistrationId() { - return registrationId; - } - - public void setRegistrationId(final String registrationId) { - this.registrationId = registrationId; - } - - public String getHostName() { - return hostName; - } - - public void setHostName(final String hostName) { - this.hostName = hostName; - } - - public String getBlackDuckName() { - return blackDuckName; - } - - public void setBlackDuckName(final BlackDuckName blackDuckName) { - this.blackDuckName = blackDuckName.getName(); - } - - public void setBlackDuckName(final String blackDuckName) { - this.blackDuckName = blackDuckName; - } - - public String getBlackDuckVersion() { - return blackDuckVersion; - } - - public void setBlackDuckVersion(final String blackDuckVersion) { - this.blackDuckVersion = blackDuckVersion; - } - - public String getThirdPartyName() { - return thirdPartyName; - } - - public void setThirdPartyName(final ThirdPartyName thirdPartyName) { - this.thirdPartyName = thirdPartyName.getName(); - } - - public void setThirdPartyName(final String thirdPartyName) { - this.thirdPartyName = thirdPartyName; - } - - public String getThirdPartyVersion() { - return thirdPartyVersion; - } - - public void setThirdPartyVersion(final String thirdPartyVersion) { - this.thirdPartyVersion = thirdPartyVersion; - } - - public String getPluginVersion() { - return pluginVersion; - } - - public void setPluginVersion(final String pluginVersion) { - this.pluginVersion = pluginVersion; - } - - public String getSource() { - return source; - } - - public void setSource(final PhoneHomeSource source) { - this.source = source.getName(); - } - - public void setSource(final String source) { - this.source = source; - } - -} diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyValidator.java b/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyValidator.java deleted file mode 100644 index c70a06c..0000000 --- a/src/main/java/com/blackducksoftware/integration/phonehome/PhoneHomeRequestBodyValidator.java +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Phone Home API - * - * Copyright (C) 2017 Black Duck Software, Inc. - * http://www.blackducksoftware.com/ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package com.blackducksoftware.integration.phonehome; - -import org.apache.commons.lang3.StringUtils; - -import com.blackducksoftware.integration.phonehome.enums.PhoneHomeRequestFieldEnum; -import com.blackducksoftware.integration.validator.AbstractValidator; -import com.blackducksoftware.integration.validator.ValidationResult; -import com.blackducksoftware.integration.validator.ValidationResultEnum; -import com.blackducksoftware.integration.validator.ValidationResults; - -public class PhoneHomeRequestBodyValidator extends AbstractValidator{ - private String registrationId; - private String hostName; - private String blackDuckName; - private String blackDuckVersion; - private String thirdPartyName; - private String thirdPartyVersion; - private String pluginVersion; - private String source; - - @Override - public ValidationResults assertValid() { - final ValidationResults result = new ValidationResults(); - validateHubServerIdentifier(result); - validateBlackDuckProductInfo(result); - validateThirdPartyProductInfo(result); - validateIntegrationInfo(result); - validateSource(result); - return result; - } - - public void validateHubServerIdentifier(final ValidationResults result){ - if (StringUtils.isBlank(registrationId) && StringUtils.isBlank(hostName)) { - result.addResult(PhoneHomeRequestFieldEnum.REGID, - new ValidationResult(ValidationResultEnum.ERROR, "No Hub server identifier was found.")); - } - } - - public void validateBlackDuckProductInfo(final ValidationResults result){ - if (blackDuckName == null || StringUtils.isBlank(blackDuckName)) { - result.addResult(PhoneHomeRequestFieldEnum.BLACKDUCKNAME, - new ValidationResult(ValidationResultEnum.ERROR, "No Black Duck product name was found.")); - }else if (StringUtils.isBlank(blackDuckVersion)) { - result.addResult(PhoneHomeRequestFieldEnum.BLACKDUCKVERSION, - new ValidationResult(ValidationResultEnum.ERROR, String.format("No version of %s was found.", blackDuckName))); - } - } - - public void validateThirdPartyProductInfo(final ValidationResults result){ - if (thirdPartyName == null || StringUtils.isBlank(thirdPartyName)) { - result.addResult(PhoneHomeRequestFieldEnum.THIRDPARTYNAME, - new ValidationResult(ValidationResultEnum.ERROR, "No third party name was found.")); - } else if (StringUtils.isBlank(thirdPartyVersion)) { - result.addResult(PhoneHomeRequestFieldEnum.THIRDPARTYVERSION, - new ValidationResult(ValidationResultEnum.ERROR, String.format("No version of %s was found.", thirdPartyName))); - } - } - - public void validateIntegrationInfo(final ValidationResults result){ - if (StringUtils.isBlank(pluginVersion)) { - result.addResult(PhoneHomeRequestFieldEnum.PLUGINVERSION, - new ValidationResult(ValidationResultEnum.ERROR, "No plugin version was found.")); - } - } - - public void validateSource(final ValidationResults result){ - if (source == null || StringUtils.isBlank(source)) { - result.addResult(PhoneHomeRequestFieldEnum.PLUGINVERSION, - new ValidationResult(ValidationResultEnum.ERROR, "No source was found.")); - } - } - - - public void setRegistrationId(final String registrationId) { - this.registrationId = registrationId; - } - - public void setHostName(final String hostName) { - this.hostName = hostName; - } - - public void setBlackDuckName(final String blackDuckName) { - this.blackDuckName = blackDuckName; - } - - public void setBlackDuckVersion(final String blackDuckVersion) { - this.blackDuckVersion = blackDuckVersion; - } - - public void setThirdPartyName(final String thirdPartyName) { - this.thirdPartyName = thirdPartyName; - } - - public void setThirdPartyVersion(final String thirdPartyVersion) { - this.thirdPartyVersion = thirdPartyVersion; - } - - public void setPluginVersion(final String pluginVersion) { - this.pluginVersion = pluginVersion; - } - - public void setSource(final String source) { - this.source = source; - } - -} diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/enums/BlackDuckName.java b/src/main/java/com/blackducksoftware/integration/phonehome/enums/BlackDuckName.java index 36f69a5..92d139a 100644 --- a/src/main/java/com/blackducksoftware/integration/phonehome/enums/BlackDuckName.java +++ b/src/main/java/com/blackducksoftware/integration/phonehome/enums/BlackDuckName.java @@ -37,4 +37,5 @@ private BlackDuckName(final String name) { public String getName() { return this.name; } + } diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/enums/PhoneHomeRequestFieldEnum.java b/src/main/java/com/blackducksoftware/integration/phonehome/enums/PhoneHomeRequestFieldEnum.java deleted file mode 100644 index 86c5786..0000000 --- a/src/main/java/com/blackducksoftware/integration/phonehome/enums/PhoneHomeRequestFieldEnum.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Phone Home API - * - * Copyright (C) 2017 Black Duck Software, Inc. - * http://www.blackducksoftware.com/ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package com.blackducksoftware.integration.phonehome.enums; - -import com.blackducksoftware.integration.validator.FieldEnum; - -public enum PhoneHomeRequestFieldEnum implements FieldEnum{ - BLACKDUCKNAME("blackDuckName"), - BLACKDUCKVERSION("blackDuckVersion"), - PLUGINVERSION("pluginVersion"), - REGID("regId"), - THIRDPARTYNAME("thirdPartyName"), - THIRDPARTYVERSION("thirdPartyVersion"); - - private String key; - - private PhoneHomeRequestFieldEnum(final String key) { - this.key = key; - } - - @Override - public String getKey() { - return key; - } -} diff --git a/src/main/java/com/blackducksoftware/integration/phonehome/exception/PhoneHomeException.java b/src/main/java/com/blackducksoftware/integration/phonehome/exception/PhoneHomeException.java deleted file mode 100644 index 9a5b4e6..0000000 --- a/src/main/java/com/blackducksoftware/integration/phonehome/exception/PhoneHomeException.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Phone Home API - * - * Copyright (C) 2017 Black Duck Software, Inc. - * http://www.blackducksoftware.com/ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package com.blackducksoftware.integration.phonehome.exception; - -public class PhoneHomeException extends Exception { - private static final long serialVersionUID = 678249589814131943L; - - public PhoneHomeException() { - super(); - } - - public PhoneHomeException(final String message) { - super(message); - } - - public PhoneHomeException(final Throwable cause) { - super(cause); - } - - public PhoneHomeException(final String message, final Throwable e) { - super(message, e); - } -} diff --git a/src/main/resources/META-INF/services/org.restlet.engine.converter.ConverterHelper b/src/main/resources/META-INF/services/org.restlet.engine.converter.ConverterHelper deleted file mode 100644 index fabd33a..0000000 --- a/src/main/resources/META-INF/services/org.restlet.engine.converter.ConverterHelper +++ /dev/null @@ -1 +0,0 @@ -org.restlet.ext.gson.GsonConverter \ No newline at end of file diff --git a/src/test/java/com/blackducksoftware/integration/phonehome/PhoneHomeClientUnitTest.java b/src/test/java/com/blackducksoftware/integration/phonehome/PhoneHomeClientUnitTest.java deleted file mode 100644 index 4867653..0000000 --- a/src/test/java/com/blackducksoftware/integration/phonehome/PhoneHomeClientUnitTest.java +++ /dev/null @@ -1,235 +0,0 @@ -/** - * Phone Home API - * - * Copyright (C) 2017 Black Duck Software, Inc. - * http://www.blackducksoftware.com/ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package com.blackducksoftware.integration.phonehome; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.net.URL; -import java.util.HashMap; -import java.util.Map; - -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.junit.MockServerRule; -import org.mockserver.model.Header; -import org.mockserver.model.HttpRequest; -import org.mockserver.model.HttpResponse; - -import com.blackducksoftware.integration.hub.rest.RestConnection; -import com.blackducksoftware.integration.hub.rest.UnauthenticatedRestConnection; -import com.blackducksoftware.integration.log.IntBufferedLogger; -import com.blackducksoftware.integration.phonehome.enums.BlackDuckName; -import com.blackducksoftware.integration.phonehome.enums.PhoneHomeRequestFieldEnum; -import com.blackducksoftware.integration.phonehome.enums.PhoneHomeSource; -import com.blackducksoftware.integration.phonehome.enums.ThirdPartyName; -import com.blackducksoftware.integration.phonehome.exception.PhoneHomeException; - -public class PhoneHomeClientUnitTest { - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Rule - public final MockServerRule msRule = new MockServerRule(this); - - public static final String LOCALHOST = "127.0.0.1"; - public static final int TIMEOUT = 5; - - private final MockServerClient msClient = new MockServerClient(LOCALHOST, msRule.getPort()); - private final int port = msRule.getPort(); - - @Before - public void startProxy(){ - msClient - .when(new HttpRequest().withPath("/test")) - .respond(new HttpResponse().withHeader(new Header("Content-Type", "json"))); - } - - @After - public void stopProxy() { - // Intentionally left blank - } - - @Test - public void callHomeInvalidUrl() throws Exception { - exception.expect(PhoneHomeException.class); - final String targetUrl = "http://example.com:"+this.port+"/test"; - final URL url = new URL(targetUrl); - final RestConnection restConnection = new UnauthenticatedRestConnection(new IntBufferedLogger(), url, TIMEOUT); - final PhoneHomeClient phClient = new PhoneHomeClient(new IntBufferedLogger(), url, restConnection); - - final String regId = "regId"; - final PhoneHomeSource source = PhoneHomeSource.INTEGRATIONS; - final Map infoMap = new HashMap<>(); - final PhoneHomeRequestBody phoneHomeRequest = new PhoneHomeRequestBody(regId, source.getName(), infoMap); - - phClient.postPhoneHomeRequest(phoneHomeRequest); - } - - @Test - public void callHomeValidUrl() throws Exception { - final String targetUrl = "http://"+LOCALHOST + ":" + this.port + "/test"; - final URL url = new URL(targetUrl); - final RestConnection restConnection = new UnauthenticatedRestConnection(new IntBufferedLogger(), url, TIMEOUT); - final PhoneHomeClient phClient = new PhoneHomeClient(new IntBufferedLogger(), url, restConnection); - final String regId = "regId"; - final PhoneHomeSource source = PhoneHomeSource.INTEGRATIONS; - final Map infoMap = new HashMap<>(); - final PhoneHomeRequestBody phoneHomeRequest = new PhoneHomeRequestBody(regId, source.getName(), infoMap); - - phClient.postPhoneHomeRequest(phoneHomeRequest); - } - - @Test - public void callHomeIntegrationsTest() throws Exception { - final String targetUrl = "http://"+LOCALHOST + ":" + this.port + "/test"; - final URL url = new URL(targetUrl); - final RestConnection restConnection = new UnauthenticatedRestConnection(new IntBufferedLogger(), url, TIMEOUT); - final PhoneHomeClient phClient = new PhoneHomeClient(new IntBufferedLogger(), url, restConnection); - - final PhoneHomeRequestBodyBuilder phoneHomeRequestBuilder = new PhoneHomeRequestBodyBuilder(); - phoneHomeRequestBuilder.setRegistrationId("regKey"); - phoneHomeRequestBuilder.setHostName(null); - phoneHomeRequestBuilder.setBlackDuckName(BlackDuckName.HUB); - phoneHomeRequestBuilder.setBlackDuckVersion("blackDuckVersion"); - phoneHomeRequestBuilder.setPluginVersion("pluginVersion"); - phoneHomeRequestBuilder.setThirdPartyName(ThirdPartyName.JENKINS); - phoneHomeRequestBuilder.setThirdPartyVersion("thirdPartyVersion"); - phoneHomeRequestBuilder.setSource(PhoneHomeSource.INTEGRATIONS); - final PhoneHomeRequestBody phoneHomeRequest = phoneHomeRequestBuilder.build(); - - phClient.postPhoneHomeRequest(phoneHomeRequest); - } - - @Test - public void callHomeIntegrationsTestWithHostName() throws Exception { - final String targetUrl = "http://"+LOCALHOST + ":" + this.port + "/test"; - final URL url = new URL(targetUrl); - final RestConnection restConnection = new UnauthenticatedRestConnection(new IntBufferedLogger(), url, TIMEOUT); - final PhoneHomeClient phClient = new PhoneHomeClient(new IntBufferedLogger(), url, restConnection); - - final PhoneHomeRequestBodyBuilder phoneHomeRequestBuilder = new PhoneHomeRequestBodyBuilder(); - phoneHomeRequestBuilder.setRegistrationId(null); - phoneHomeRequestBuilder.setHostName("hostName"); - phoneHomeRequestBuilder.setBlackDuckName(BlackDuckName.HUB); - phoneHomeRequestBuilder.setBlackDuckVersion("blackDuckVersion"); - phoneHomeRequestBuilder.setPluginVersion("pluginVersion"); - phoneHomeRequestBuilder.setThirdPartyName(ThirdPartyName.JENKINS); - phoneHomeRequestBuilder.setThirdPartyVersion("thirdPartyVersion"); - phoneHomeRequestBuilder.setSource(PhoneHomeSource.INTEGRATIONS); - final PhoneHomeRequestBody phoneHomeRequest = phoneHomeRequestBuilder.build(); - - phClient.postPhoneHomeRequest(phoneHomeRequest); - } - - @Test - public void testPhoneHomeException() { - final PhoneHomeException emptyException = new PhoneHomeException(); - final PhoneHomeException exceptionException = new PhoneHomeException(emptyException); - final PhoneHomeException messageException = new PhoneHomeException("message"); - final PhoneHomeException exceptionAndMessageException = new PhoneHomeException("message", emptyException); - for(final PhoneHomeException phoneHomeException : new PhoneHomeException[]{emptyException, exceptionException, messageException, exceptionAndMessageException}){ - try{ - throw phoneHomeException; - }catch(final PhoneHomeException e){ - //Do nothing - } - } - } - - @Test - public void testPostBadPhoneHomeRequestBuilding() throws Exception{ - final PhoneHomeRequestBodyBuilder phoneHomeRequestBuilder = new PhoneHomeRequestBodyBuilder(); - try{ - phoneHomeRequestBuilder.build(); - fail("Illegal state exception not thrown"); - }catch(final IllegalStateException e){ - //Do nothing - } - try{ - phoneHomeRequestBuilder.setBlackDuckName(BlackDuckName.HUB); - phoneHomeRequestBuilder.setThirdPartyName(ThirdPartyName.ARTIFACTORY); - phoneHomeRequestBuilder.build(); - fail("Illegal state exception not thrown"); - }catch(final IllegalStateException e){ - //Do nothing - } - } - - @Test - public void validatePhoneHomeRequestBuilding() throws Exception{ - final PhoneHomeRequestBodyBuilder phoneHomeRequestBuilder = new PhoneHomeRequestBodyBuilder(); - phoneHomeRequestBuilder.setRegistrationId("regKey"); - phoneHomeRequestBuilder.setHostName(null); - phoneHomeRequestBuilder.setBlackDuckName(BlackDuckName.HUB); - phoneHomeRequestBuilder.setBlackDuckVersion("blackDuckVersion"); - phoneHomeRequestBuilder.setPluginVersion("pluginVersion"); - phoneHomeRequestBuilder.setThirdPartyName(ThirdPartyName.JENKINS); - phoneHomeRequestBuilder.setThirdPartyVersion("thirdPartyVersion"); - phoneHomeRequestBuilder.setSource(PhoneHomeSource.INTEGRATIONS); - phoneHomeRequestBuilder.addToMetaDataMap("some", "metadata"); - final Map builderInfoMap = phoneHomeRequestBuilder.getMetaDataMap(); - builderInfoMap.put(PhoneHomeRequestFieldEnum.BLACKDUCKNAME.getKey(), phoneHomeRequestBuilder.getBlackDuckName()); - builderInfoMap.put(PhoneHomeRequestFieldEnum.BLACKDUCKVERSION.getKey(), phoneHomeRequestBuilder.getBlackDuckVersion()); - builderInfoMap.put(PhoneHomeRequestFieldEnum.THIRDPARTYNAME.getKey(), phoneHomeRequestBuilder.getThirdPartyName()); - builderInfoMap.put(PhoneHomeRequestFieldEnum.THIRDPARTYVERSION.getKey(), phoneHomeRequestBuilder.getThirdPartyVersion()); - builderInfoMap.put(PhoneHomeRequestFieldEnum.PLUGINVERSION.getKey(), phoneHomeRequestBuilder.getPluginVersion()); - - final PhoneHomeRequestBody phoneHomeRequest = phoneHomeRequestBuilder.build(); - - assertTrue(phoneHomeRequestBuilder.getRegistrationId().equals(phoneHomeRequest.getRegId())); - assertTrue(phoneHomeRequestBuilder.getSource().equals(phoneHomeRequest.getSource())); - assertTrue(builderInfoMap.equals((phoneHomeRequest.getInfoMap()))); - - phoneHomeRequestBuilder.setRegistrationId(null); - phoneHomeRequestBuilder.setHostName(LOCALHOST); - final PhoneHomeRequestBody phoneHomeRequestWithHost = phoneHomeRequestBuilder.build(); - assertTrue(phoneHomeRequestBuilder.md5Hash(phoneHomeRequestBuilder.getHostName()).equals(phoneHomeRequestWithHost.getRegId())); - } - - @Test - public void validateBadPhoneHomeBackend() throws Exception{ - final PhoneHomeClient phClient = new PhoneHomeClient(null, null, null); - try{ - phClient.postPhoneHomeRequest(null); - fail("Phone home exception not thrown"); - }catch(final PhoneHomeException e){ - //Do nothing - } - } - - @Test - public void constructPhoneHomeClientWithoutUrl() throws Exception{ - final String targetUrl = "http://example.com:"+this.port+"/test"; - final URL url = new URL(targetUrl); - final RestConnection restConnection = new UnauthenticatedRestConnection(new IntBufferedLogger(), url, TIMEOUT); - new PhoneHomeClient(new IntBufferedLogger(), restConnection); - //Cannot test this meaningfully without phoning home to an actual server, which is bad. - } - -}