From 9b328ac216ca100be524259ee5efedeb81d14230 Mon Sep 17 00:00:00 2001 From: Philippe Khalife Date: Fri, 24 Nov 2017 09:47:48 -0500 Subject: [PATCH 1/2] Support user-provided Solace Messaging Credentials --- .../SolaceMessagingInfoCreator.java | 39 +++++++++++++++- ...SolaceMessagingServiceInfoCreatorTest.java | 46 +++++++++++++++++-- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingInfoCreator.java b/src/main/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingInfoCreator.java index 6a120a5..78fefd6 100644 --- a/src/main/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingInfoCreator.java +++ b/src/main/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingInfoCreator.java @@ -139,7 +139,6 @@ public SolaceMessagingInfo createServiceInfo(Map serviceData) { case "amqpTlsUris": amqpTlsUris = (List) value; break; - case "managementHostnames": managementHostnames = (List) value; break; @@ -167,4 +166,40 @@ public SolaceMessagingInfo createServiceInfo(Map serviceData) { return solMessagingInfo; } -} + + + public boolean accept(Map serviceData) { + return super.accept(serviceData) || (isUserProvided(serviceData) && isSolaceMessagingInfo(serviceData)); + } + + public boolean isUserProvided(Map serviceData) { + if( serviceData == null ) + return false; + + String label = (String) serviceData.get("label"); + if( "user-provided".startsWith(label)) { + return true; + } + return false; + } + + public boolean isSolaceMessagingInfo(Map serviceData) { + if( serviceData == null ) + return false; + + try { + SolaceMessagingInfo solMessagingInfo = createServiceInfo(serviceData); + // Good enough test + // Don't check for client or management username/password as they can be missing when using LDAP + // Not checking other Uri(s) especially that this can be a user-provided service. + if( solMessagingInfo != null && solMessagingInfo.getId() != null && solMessagingInfo.getMsgVpnName() != null ) + return true; + + } catch(Throwable t) { + return false; + } + + return false; + } + +} \ No newline at end of file diff --git a/src/test/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingServiceInfoCreatorTest.java b/src/test/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingServiceInfoCreatorTest.java index 9ae1a49..4b18276 100644 --- a/src/test/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingServiceInfoCreatorTest.java +++ b/src/test/java/com/solace/labs/spring/cloud/cloudfoundry/SolaceMessagingServiceInfoCreatorTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -55,6 +56,23 @@ public void basicSolaceMessagingServiceInfoCreationTest() { validateExampleSmi(smi); } + + + @Test + public void cupsSolaceMessagingServiceInfoCreationTest() { + + Map exVcapServices = createCUPSVcapMap(); + + SolaceMessagingInfoCreator smic = new SolaceMessagingInfoCreator(); + + assertTrue(smic.accept(exVcapServices)); + + SolaceMessagingInfo smi = smic.createServiceInfo(exVcapServices); + + validateExampleSmi(smi); + + } + @Test public void mismatchLabelTest() { @@ -134,17 +152,16 @@ public void loadCreatorFromMeta() { } try { Class z = Class.forName(solaceMessagingInfoCreatorClassName); - assertTrue(z != null); - assertTrue(z.equals(SolaceMessagingInfoCreator.class)); + assertNotNull(z); + assertEquals(z,SolaceMessagingInfoCreator.class); } catch (ClassNotFoundException e) { fail("Should not throw."); } } - private Map createVcapMap() { - Map exVcapServices = new HashMap(); - + private Map createCredentialsMap() { + Map exCred = new HashMap(); exCred.put("clientUsername", "sample-client-username"); @@ -169,7 +186,15 @@ private Map createVcapMap() { exCred.put("managementUsername", "sample-mgmt-username"); exCred.put("managementPassword", "sample-mgmt-password"); exCred.put("activeManagementHostname", "vmr-medium-web"); + + return exCred; + } + + private Map createVcapMap() { + Map exVcapServices = new HashMap(); + Map exCred = createCredentialsMap(); + exVcapServices.put("credentials", exCred); exVcapServices.put("label", "solace-messaging"); exVcapServices.put("name", "test-service-instance-name"); @@ -179,6 +204,17 @@ private Map createVcapMap() { exVcapServices.put("tags", Arrays.asList("solace", "rest", "mqtt", "mq", "queue", "jms", "messaging", "amqp")); return exVcapServices; } + + private Map createCUPSVcapMap() { + Map exVcapServices = new HashMap(); + Map exCred = createCredentialsMap(); + exVcapServices.put("credentials", exCred); + + exVcapServices.put("label", "user-provided"); + exVcapServices.put("name", "test-service-instance-name"); + exVcapServices.put("tags", Arrays.asList()); + return exVcapServices; + } private void validateExampleSmi(SolaceMessagingInfo smi) { // Validate it all got set correctly. From b6782a208778f003ed0ab61047b4261622b397e8 Mon Sep 17 00:00:00 2001 From: Philippe Khalife Date: Fri, 24 Nov 2017 12:06:05 -0500 Subject: [PATCH 2/2] Clean imports --- .../com/solace/labs/spring/cloud/core/SolaceMessagingInfo.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/solace/labs/spring/cloud/core/SolaceMessagingInfo.java b/src/main/java/com/solace/labs/spring/cloud/core/SolaceMessagingInfo.java index 89829d9..0485ddf 100644 --- a/src/main/java/com/solace/labs/spring/cloud/core/SolaceMessagingInfo.java +++ b/src/main/java/com/solace/labs/spring/cloud/core/SolaceMessagingInfo.java @@ -18,8 +18,6 @@ */ package com.solace.labs.spring.cloud.core; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.builder.ToStringBuilder;