### Question:
MetadataService extends AbstractService { public <T extends Obj> T getObjByUri(String uri, Class<T> cls) { notNull(uri, "uri"); notNull(cls, "cls"); try { final T result = restTemplate.getForObject(uri, cls); if (result != null) { return result; } else { throw new GoodDataException("Received empty response from API call."); } } catch (GoodDataRestException e) { if (HttpStatus.NOT_FOUND.value() == e.getStatusCode()) { throw new ObjNotFoundException(uri, cls, e); } else { throw e; } } catch (RestClientException e) { throw new GoodDataException("Unable to get " + cls.getSimpleName().toLowerCase() + " " + uri, e); } } MetadataService(final RestTemplate restTemplate, final GoodDataSettings settings); @SuppressWarnings("unchecked") T createObj(Project project, T obj); T getObjByUri(String uri, Class<T> cls); Collection<Obj> getObjsByUris(Project project, Collection<String> uris); @SuppressWarnings("unchecked") T updateObj(T obj); void removeObj(Obj obj); void removeObjByUri(String uri); T getObjById(Project project, String id, Class<T> cls); String getObjUri(Project project, Class<T> cls, Restriction... restrictions); T getObj(Project project, Class<T> cls, Restriction... restrictions); Collection<Entry> find(Project project, Class<T> cls, Restriction... restrictions); Collection<String> findUris(Project project,
                                                             Class<T> cls,
                                                             Restriction... restrictions); @SuppressWarnings("unchecked") Collection<Entry> usedBy(Project project, Obj obj, boolean nearest, Class<? extends Obj>... types); @SuppressWarnings("unchecked") Collection<Entry> usedBy(Project project, String uri, boolean nearest, Class<? extends Obj>... types); @SuppressWarnings("unchecked") Collection<Usage> usedBy(Project project, Collection<String> uris, boolean nearest, Class<? extends Obj>... types); Collection<String> findUris(Project project, Restriction... restrictions); Map<String, String> identifiersToUris(Project project, Collection<String> identifiers); List<AttributeElement> getAttributeElements(Attribute attribute); List<AttributeElement> getAttributeElements(DisplayForm displayForm); static final UriTemplate OBJ_TEMPLATE; }### Answer:
@Test(expectedExceptions = IllegalArgumentException.class) public void testGetObjByUriNullUri() { service.getObjByUri(null, Obj.class); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testGetObjByUriNullCls() { service.getObjByUri(URI, null); }
@Test(expectedExceptions = ObjNotFoundException.class) public void testGetObjByUriNotFound() { final GoodDataRestException restException = mock(GoodDataRestException.class); when(restException.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND.value()); when(restTemplate.getForObject(URI, Obj.class)).thenThrow(restException); service.getObjByUri(URI, Obj.class); }
@Test public void testGetObjByUri() { final Obj resultObj = mock(Obj.class); when(restTemplate.getForObject(URI, Obj.class)).thenReturn(resultObj); final Obj result = service.getObjByUri(URI, Obj.class); assertThat(result, is(resultObj)); }
@Test(expectedExceptions = GoodDataException.class) public void testGetObjByUriWithClientSideHTTPError() { when(restTemplate.getForObject(URI, Obj.class)).thenThrow(new RestClientException("")); service.getObjByUri(URI, Queryable.class); }
@Test(expectedExceptions = GoodDataRestException.class) public void testGetObjByUriWithServerSideHTTPError() { when(restTemplate.getForObject(URI, Obj.class)).thenThrow(new GoodDataRestException(500, "", "", "", "")); service.getObjByUri(URI, Obj.class); }
@Test(expectedExceptions = GoodDataException.class) public void testGetObjByUriWithNoResponseFromAPI() { service.getObjByUri(URI, Obj.class); }