SimpleIrdRoute implements AltoNorthboundRoute { @Path("{path:.+}") @GET @Produces({ALTO_IRD, ALTO_ERROR}) public Response route(@Context HttpServletRequest req, @PathParam("path") String path) { if (provider == null) { return Response.status(Response.Status.NOT_FOUND).build(); } IrdInstance ird = provider.getInstance(new ResourceId(path)); if (ird == null) { return Response.status(Response.Status.NOT_FOUND).build(); } try { Rfc7285Ird rfcIrd = convert(ird, req); return Response.ok(rfcIrd, ALTO_IRD).build(); } catch (Exception e) { e.printStackTrace(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } SimpleIrdRoute(AltoSimpleIrdProvider provider); @Path("{path:.+}") @GET @Produces({ALTO_IRD, ALTO_ERROR}) Response route(@Context HttpServletRequest req, @PathParam("path") String path); static final String ALTO_IRD; }
@Test public void testRoute() { when(m_provider.getInstance(new ResourceId("helloworld"))).thenReturn(mockIrdInstance); List<CostType> costTypeList = new LinkedList<>(); CostTypeBuilder costTypeBuilder = new CostTypeBuilder() .setCostMetric(new CostMetric("routingcost")) .setCostMode("ordinal") .setName("num-routing"); costTypeList.add(costTypeBuilder.build()); MetaBuilder metaBuilder = new MetaBuilder() .setCostType(costTypeList) .setDefaultNetworkMap(new ResourceId("my-default-alto-network-map")); Rfc7285IrdMetadata meta = new Rfc7285IrdMetadataBuilder() .setMeta(metaBuilder.build()) .build(); List<IrdEntry> irdEntryList = new LinkedList<>(); List<String> listCostTypeName = new LinkedList<>(); List<ResourceId> listResourceId = new LinkedList<>(); listResourceId.add(new ResourceId("my-default-alto-network-map")); listCostTypeName.add("num-routing"); EntryCapabilitiesBuilder ecb = new EntryCapabilitiesBuilder() .addAugmentation(Rfc7285CostTypeCapabilities.class, new Rfc7285CostTypeCapabilitiesBuilder() .setCostTypeNames(listCostTypeName) .setCostConstraints(true).build() ); IrdEntryBuilder irdEntryBuilder = new IrdEntryBuilder() .setLocation(new FixedUrlBuilder().setUri(new Uri("http: .setMediaType("application/alto-costmap+json") .setEntryCapabilities(ecb.build()) .setUses(listResourceId) .setEntryId(new ResourceId("default-ird")); irdEntryList.add(irdEntryBuilder.build()); irdEntryBuilder .setLocation(new RelativePathBuilder().setPath(new Uri("/costmap2/num/routingcost")).build()) .setMediaType("application/alto-costmap+json") .setEntryCapabilities(ecb.build()) .setUses(listResourceId) .setEntryId(new ResourceId("another-ird")); irdEntryList.add(irdEntryBuilder.build()); when(mockIrdInstance.getAugmentation(Rfc7285IrdMetadata.class)).thenReturn(meta); when(mockIrdInstance.getIrdEntry()).thenReturn(irdEntryList); when(mockHttpServletRequest.getScheme()).thenReturn("http"); when(mockHttpServletRequest.getLocalName()).thenReturn("alto.example.com"); when(mockHttpServletRequest.getLocalPort()).thenReturn(80); Response resultResponse = this.sirSpy.route(mockHttpServletRequest, "helloworld"); SimpleIrdRoute.Rfc7285Ird ird = new SimpleIrdRoute.Rfc7285Ird(); Map<String, Object> costType = new HashMap<String, Object>(); costType.put("cost-mode", "ordinal"); costType.put("cost-metric", "routingcost"); Map<String, Object> costTypes = new HashMap<>(); costTypes.put("num-routing", costType); ird.meta.put("default-alto-network-map", "my-default-alto-network-map"); ird.meta.put("cost-types", costTypes); Map<String, Object> resource = new HashMap<>(); resource.put("uri", "http: resource.put("media-type", "application/alto-costmap+json"); Map<String, Object> capabilityMap = new HashMap<>(); capabilityMap.put("cost-type-names", listCostTypeName); capabilityMap.put("cost-constraints", true); resource.put("capabilities", capabilityMap); List<String> uses = new LinkedList<>(); uses.add("my-default-alto-network-map"); resource.put("uses", uses); ird.resources.put("default-ird", resource); resource.put("uri", "http: ird.resources.put("another-ird", resource); assertEquals(Maps.difference(ird.meta, ((SimpleIrdRoute.Rfc7285Ird)resultResponse.getEntity()).meta).areEqual(), true); SimpleIrdRoute nullSimpleIrdRoute = new SimpleIrdRoute(null); SimpleIrdRoute sirSpy = spy(nullSimpleIrdRoute); assertEquals(sirSpy.route(mockHttpServletRequest, "helloworld").getStatus(), Response.status(Response.Status.NOT_FOUND).build().getStatus()); }