private boolean isReplicaAvailable(String myAppName, String url) { try { String givenHostName = new URI(url).getHost(); Application app = PeerAwareInstanceRegistry.getInstance().getApplication(myAppName, false); for (InstanceInfo info : app.getInstances()) { if (info.getHostName().equals(givenHostName)) { return true; } } } catch (Throwable e) { logger.error("Could not determine if the replica is available ", e); } return false; }
@Test public void testBadRegistration() throws Exception { InstanceInfo instanceInfo = spy(InstanceInfoGenerator.takeOne()); when(instanceInfo.getId()).thenReturn(null); Response response = applicationResource.addInstance(instanceInfo, false + ""); assertThat(response.getStatus(), is(400)); instanceInfo = spy(InstanceInfoGenerator.takeOne()); when(instanceInfo.getHostName()).thenReturn(null); response = applicationResource.addInstance(instanceInfo, false + ""); assertThat(response.getStatus(), is(400)); instanceInfo = spy(InstanceInfoGenerator.takeOne()); when(instanceInfo.getAppName()).thenReturn(""); response = applicationResource.addInstance(instanceInfo, false + ""); assertThat(response.getStatus(), is(400)); instanceInfo = spy(InstanceInfoGenerator.takeOne()); when(instanceInfo.getAppName()).thenReturn(applicationResource.getName() + "extraExtra"); response = applicationResource.addInstance(instanceInfo, false + ""); assertThat(response.getStatus(), is(400)); instanceInfo = spy(InstanceInfoGenerator.takeOne()); when(instanceInfo.getDataCenterInfo()).thenReturn(null); response = applicationResource.addInstance(instanceInfo, false + ""); assertThat(response.getStatus(), is(400)); instanceInfo = spy(InstanceInfoGenerator.takeOne()); when(instanceInfo.getDataCenterInfo()) .thenReturn( new DataCenterInfo() { @Override public Name getName() { return null; } }); response = applicationResource.addInstance(instanceInfo, false + ""); assertThat(response.getStatus(), is(400)); }
/* * (non-Javadoc) * * @see * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object * , com.thoughtworks.xstream.io.HierarchicalStreamWriter, * com.thoughtworks.xstream.converters.MarshallingContext) */ @Override public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context) { InstanceInfo info = (InstanceInfo) source; if (info.getInstanceId() != null) { writer.startNode(ELEM_INSTANCE_ID); writer.setValue(info.getInstanceId()); writer.endNode(); } writer.startNode(ELEM_HOST); writer.setValue(info.getHostName()); writer.endNode(); writer.startNode(ELEM_APP); writer.setValue(info.getAppName()); writer.endNode(); writer.startNode(ELEM_IP); writer.setValue(info.getIPAddr()); writer.endNode(); if (!("unknown".equals(info.getSID()) || "na".equals(info.getSID()))) { writer.startNode(ELEM_SID); writer.setValue(info.getSID()); writer.endNode(); } writer.startNode(ELEM_STATUS); writer.setValue(getStatus(info)); writer.endNode(); writer.startNode(ELEM_OVERRIDDEN_STATUS); writer.setValue(info.getOverriddenStatus().name()); writer.endNode(); writer.startNode(ELEM_PORT); writer.addAttribute(ATTR_ENABLED, String.valueOf(info.isPortEnabled(PortType.UNSECURE))); writer.setValue(String.valueOf(info.getPort())); writer.endNode(); writer.startNode(ELEM_SECURE_PORT); writer.addAttribute(ATTR_ENABLED, String.valueOf(info.isPortEnabled(PortType.SECURE))); writer.setValue(String.valueOf(info.getSecurePort())); writer.endNode(); writer.startNode(ELEM_COUNTRY_ID); writer.setValue(String.valueOf(info.getCountryId())); writer.endNode(); if (info.getDataCenterInfo() != null) { writer.startNode(NODE_DATACENTER); // This is needed for backward compat. for now. if (info.getDataCenterInfo().getName() == Name.Amazon) { writer.addAttribute("class", "com.netflix.appinfo.AmazonInfo"); } else { writer.addAttribute("class", "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"); } context.convertAnother(info.getDataCenterInfo()); writer.endNode(); } if (info.getLeaseInfo() != null) { writer.startNode(NODE_LEASE); context.convertAnother(info.getLeaseInfo()); writer.endNode(); } if (info.getMetadata() != null) { writer.startNode(NODE_METADATA); // for backward compat. for now if (info.getMetadata().size() == 0) { writer.addAttribute("class", "java.util.Collections$EmptyMap"); } context.convertAnother(info.getMetadata()); writer.endNode(); } autoMarshalEligible(source, writer); }