diff --git a/src/main/java/cz/cvut/kbss/termit/rest/VocabularyController.java b/src/main/java/cz/cvut/kbss/termit/rest/VocabularyController.java index c4286bcbc..31590c906 100644 --- a/src/main/java/cz/cvut/kbss/termit/rest/VocabularyController.java +++ b/src/main/java/cz/cvut/kbss/termit/rest/VocabularyController.java @@ -351,9 +351,10 @@ public void removeVocabulary(@Parameter(description = ApiDoc.ID_LOCAL_NAME_DESCR @RequestParam(name = QueryParams.NAMESPACE, required = false) Optional namespace) { final URI identifier = resolveIdentifier(namespace.orElse(config.getNamespace().getVocabulary()), localName); - final Vocabulary toRemove = vocabularyService.getReference(identifier); - vocabularyService.remove(toRemove); - LOG.debug("Vocabulary {} removed.", toRemove); + vocabularyService.find(identifier).ifPresent(toRemove -> { + vocabularyService.remove(toRemove); + LOG.debug("Vocabulary {} removed.", toRemove); + }); } @Operation(description = "Validates the terms in a vocabulary with the specified identifier.") diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java b/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java index cc71adacb..2b5512d84 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java @@ -265,6 +265,7 @@ public long getLastModified() { @PreAuthorize("@vocabularyAuthorizationService.canRemove(#instance)") @CacheEvict(allEntries = true) + @Transactional @Override public void remove(Vocabulary instance) { if (instance.getDocument() != null) { diff --git a/src/test/java/cz/cvut/kbss/termit/rest/VocabularyControllerTest.java b/src/test/java/cz/cvut/kbss/termit/rest/VocabularyControllerTest.java index afc335516..962756539 100644 --- a/src/test/java/cz/cvut/kbss/termit/rest/VocabularyControllerTest.java +++ b/src/test/java/cz/cvut/kbss/termit/rest/VocabularyControllerTest.java @@ -265,9 +265,11 @@ void getByIdUsesSpecifiedNamespaceInsteadOfDefaultOneForResolvingIdentifier() th @Test void removeVocabularyReturns2xxForEmptyVocabulary() throws Exception { final Vocabulary vocabulary = Generator.generateVocabulary(); - vocabulary.setUri(Generator.generateUri()); - final String fragment = IdentifierResolver.extractIdentifierFragment(vocabulary.getUri()); - mockMvc.perform(delete(PATH + "/" + fragment)).andExpect(status().is2xxSuccessful()).andReturn(); + vocabulary.setUri(VOCABULARY_URI); + when(idResolverMock.resolveIdentifier(configMock.getNamespace().getVocabulary(), FRAGMENT)) + .thenReturn(VOCABULARY_URI); + when(serviceMock.find(VOCABULARY_URI)).thenReturn(Optional.of(vocabulary)); + mockMvc.perform(delete(PATH + "/" + FRAGMENT)).andExpect(status().is2xxSuccessful()).andReturn(); } @Test @@ -276,7 +278,10 @@ void removeVocabularyReturns4xxForNotRemovableVocabulary() throws Exception { .when(serviceMock).remove(any()); final Vocabulary vocabulary = Generator.generateVocabulary(); - vocabulary.setUri(Generator.generateUri()); + vocabulary.setUri(VOCABULARY_URI); + when(idResolverMock.resolveIdentifier(configMock.getNamespace().getVocabulary(), FRAGMENT)) + .thenReturn(VOCABULARY_URI); + when(serviceMock.find(vocabulary.getUri())).thenReturn(Optional.of(vocabulary)); final String fragment = IdentifierResolver.extractIdentifierFragment(vocabulary.getUri()); mockMvc.perform(delete(PATH + "/" + fragment)).andExpect(status().is4xxClientError()).andReturn(); } @@ -615,7 +620,7 @@ void updateAccessControlLevelThrowsBadRequestForRecordUriNotMatchingPath() throw record.setHolder(Generator.generateUserWithId()); mockMvc.perform(put(PATH + "/" + FRAGMENT + "/acl/records/" + Generator.randomInt()) - .content(toJson(record)).contentType(MediaType.APPLICATION_JSON)) + .content(toJson(record)).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()); verify(serviceMock, never()).updateAccessControlLevel(any(Vocabulary.class), any(AccessControlRecord.class)); }