@Test public void getTest() { try { URI uri = new URI(dirFrontEnd + "/empresa/nifPrueba"); Resource<Empresa> resource = restTemplate .exchange( uri, HttpMethod.GET, null, new ParameterizedTypeReference<Resource<Empresa>>() {}) .getBody(); assertTrue(resource.getContent().getNombre().equals("nombrePrueba")); } catch (Exception e) { e.printStackTrace(); fail(); } }
/** * Returns whether the given {@link Resource} matches the given target {@link ResolvableType}. * We inspect the {@link Resource}'s value to determine the match. * * @param resource * @param target must not be {@literal null}. * @return whether the given {@link Resource} can be assigned to the given target {@link * ResolvableType} */ private static boolean isValueTypeMatch(Resource<?> resource, ResolvableType target) { if (resource == null || !isRawTypeAssignable(target, resource.getClass())) { return false; } Object content = resource.getContent(); if (content == null) { return false; } ResolvableType type = findGenericType(target, Resource.class); return type != null && type.getGeneric(0).isAssignableFrom(ResolvableType.forClass(content.getClass())); }
@Override public Resource<Property> toResource(Property term) { Resource<Property> resource = new Resource<Property>(term); try { String id = UriUtils.encode(term.getIri(), "UTF-8"); final ControllerLinkBuilder lb = ControllerLinkBuilder.linkTo( ControllerLinkBuilder.methodOn(OntologyPropertyController.class) .getProperty(term.getOntologyName(), id)); resource.add(lb.withSelfRel()); if (!term.isRoot()) { resource.add(lb.slash("parents").withRel("parents")); resource.add(lb.slash("ancestors").withRel("ancestors")); resource.add(lb.slash("jstree").withRel("jstree")); } if (term.hasChildren()) { resource.add(lb.slash("children").withRel("children")); resource.add(lb.slash("descendants").withRel("descendants")); } // other links } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return resource; }
@Test public void accessServiceUsingRestTemplate() { // Access root resource URI uri = URI.create(String.format(SERVICE_URI, port)); RequestEntity<Void> request = RequestEntity.get(uri).accept(HAL_JSON).build(); Resource<Object> rootLinks = restOperations.exchange(request, new ResourceType<Object>() {}).getBody(); Links links = new Links(rootLinks.getLinks()); // Follow stores link Link storesLink = links.getLink("stores").expand(); request = RequestEntity.get(URI.create(storesLink.getHref())).accept(HAL_JSON).build(); Resources<Store> stores = restOperations.exchange(request, new ResourcesType<Store>() {}).getBody(); stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address)); }
@Test public void getCompany() throws Exception { ResponseEntity<Resource<Company>> responseEntity = restTemplate.exchange( "http://localhost:{port}/api/companies/1", HttpMethod.GET, null, new ParameterizedTypeReference<Resource<Company>>() {}, port); if (responseEntity.getStatusCode() == HttpStatus.OK) { Resource<Company> companyResource = responseEntity.getBody(); Company company = companyResource.getContent(); assertEquals("Microsoft 0", company.getName()); // TODO the company object has no owners in its collection, how can I test this } else { fail(); } }
@RequestMapping(value = "/{id}", method = RequestMethod.GET) HttpEntity<Resource<Profile>> showCustomer(@PathVariable Long id) { Resource<Profile> resource = new Resource<Profile>(this.repository.findOne(id)); resource.add(this.entityLinks.linkToSingleResource(Profile.class, id)); return new ResponseEntity<Resource<Profile>>(resource, HttpStatus.OK); }