CloudantServiceInfoCreator extends CloudFoundryServiceInfoCreator<CloudantServiceInfo> { @Override public CloudantServiceInfo createServiceInfo(final Map<String, Object> serviceData) { String id = null; String username = null; String password = null; String host = null; int port = 0; String url = null; Object credObject = serviceData.get("credentials"); Object idObj = serviceData.get("name"); if (idObj instanceof String) { id = (String) idObj; } if (credObject instanceof Map<?, ?>) { final Map<String, Object> credentials = (Map<String, Object>) credObject; final Object usernameObj = credentials.get("username"); final Object passwordObj = credentials.get("password"); final Object hostObj = credentials.get("host"); final Object portObj = credentials.get("port"); final Object urlObj = credentials.get("url"); if (usernameObj instanceof String) { username = (String) usernameObj; } if (passwordObj instanceof String) { password = (String) passwordObj; } if (hostObj instanceof String) { host = (String) hostObj; } if (portObj instanceof Integer) { port = (Integer) portObj; } if (urlObj instanceof String) { url = (String) urlObj; } } return new CloudantServiceInfo(id, username, password, host, port, url); } CloudantServiceInfoCreator(); @Override boolean accept(final Map<String, Object> serviceData); @Override CloudantServiceInfo createServiceInfo(final Map<String, Object> serviceData);  }
@Test public void testCreateServiceInfo() { Map<String, Object> empty = new HashMap<String, Object>(); Map<String, Object> badTypes = new HashMap<String, Object>(); badTypes.put("name", 1); Map<String, Object> badCredTypes = new HashMap<String, Object>(); badCredTypes.put("username", 1); badCredTypes.put("password", 1); badCredTypes.put("host", 1); badCredTypes.put("url", 1); badCredTypes.put("port", "1"); badTypes.put("credentials", badCredTypes); Map<String, Object> rawInfo = new HashMap<String, Object>(); rawInfo.put("name", "id"); Map<String, Object> rawInfoCreds = new HashMap<String, Object>(); rawInfoCreds.put("username", "username"); rawInfoCreds.put("password", "password"); rawInfoCreds.put("host", "host"); rawInfoCreds.put("url", "url"); rawInfoCreds.put("port", 443); rawInfo.put("credentials", rawInfoCreds); CloudantServiceInfo nullServiceInfo = new CloudantServiceInfo(null, null, null, null, 0, null); CloudantServiceInfo serviceInfo = new CloudantServiceInfo("id", "username", "password", "host", 443, "url"); assertEquals(nullServiceInfo, creator.createServiceInfo(empty)); assertEquals(nullServiceInfo, creator.createServiceInfo(badTypes)); assertEquals(serviceInfo, creator.createServiceInfo(rawInfo)); }