### Question:
MetadataService extends AbstractService { @SuppressWarnings("unchecked") public <T extends Updatable> T updateObj(T obj) { notNull(obj, "obj"); notNull(obj.getUri(), "obj.uri"); try { restTemplate.put(obj.getUri(), obj); return getObjByUri(obj.getUri(), (Class<T>) obj.getClass()); } catch (GoodDataException | RestClientException e) { throw new ObjUpdateException(obj, 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 = ObjUpdateException.class) public void testUpdateObjRestException() { final Updatable obj = mock(Updatable.class); when(obj.getUri()).thenReturn(URI); doThrow(RestClientException.class).when(restTemplate).put(URI, obj); service.updateObj(obj); }
@Test public void testUpdateObj() { final Updatable obj = mock(Updatable.class); final Updatable resultObj = mock(Updatable.class); when(obj.getUri()).thenReturn(URI); final Updatable forObject = restTemplate.getForObject(URI, obj.getClass()); when(forObject).thenReturn(resultObj); final Obj result = service.updateObj(obj); verify(restTemplate).put(obj.getUri(), obj); assertThat(result, is(notNullValue())); }
@Test(expectedExceptions = ObjUpdateException.class) public void testUpdateObjNotFound() { final Updatable obj = mock(Updatable.class); final GoodDataRestException restException = mock(GoodDataRestException.class); when(restException.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND.value()); when(obj.getUri()).thenReturn(URI); when(restTemplate.getForObject(URI, obj.getClass())).thenThrow(restException); service.updateObj(obj); }