diff --git a/services/src/main/java/org/fao/geonet/api/ApiUtils.java b/services/src/main/java/org/fao/geonet/api/ApiUtils.java index 70bdd173e76..7a2f151d4ed 100644 --- a/services/src/main/java/org/fao/geonet/api/ApiUtils.java +++ b/services/src/main/java/org/fao/geonet/api/ApiUtils.java @@ -152,6 +152,7 @@ public static AbstractMetadata getRecord(String uuidOrInternalId) throws Resourc Log.trace(Geonet.DATA_MANAGER, "ApiUtils.getRecord(" + uuidOrInternalId + ") -> " + metadata); return metadata; } + } catch (NumberFormatException e) { } catch (InvalidDataAccessApiUsageException e) { } diff --git a/services/src/main/java/org/fao/geonet/api/categories/TagsApi.java b/services/src/main/java/org/fao/geonet/api/categories/TagsApi.java index 1f091fbaa05..82b36bb0b6d 100644 --- a/services/src/main/java/org/fao/geonet/api/categories/TagsApi.java +++ b/services/src/main/java/org/fao/geonet/api/categories/TagsApi.java @@ -48,6 +48,7 @@ import java.util.List; import java.util.Map; +import java.util.Optional; @RequestMapping(value = { "/{portal}/api/tags" @@ -106,8 +107,8 @@ public ResponseEntity putTag( @RequestBody MetadataCategory category ) throws Exception { - MetadataCategory existingCategory = categoryRepository.findById(category.getId()).get(); - if (existingCategory != null) { + Optional existingCategory = categoryRepository.findById(category.getId()); + if (existingCategory.isPresent()) { throw new IllegalArgumentException(String.format( "A tag with id '%d' already exist", category.getId() )); @@ -148,11 +149,11 @@ public org.fao.geonet.domain.MetadataCategory getTag( @PathVariable Integer tagIdentifier ) throws Exception { - org.fao.geonet.domain.MetadataCategory category = categoryRepository.findById(tagIdentifier).get(); - if (category == null) { + Optional category = categoryRepository.findById(tagIdentifier); + if (!category.isPresent()) { throw new ResourceNotFoundException("Category not found"); } - return category; + return category.get(); } @io.swagger.v3.oas.annotations.Operation( @@ -181,8 +182,8 @@ public ResponseEntity updateTag( @RequestBody MetadataCategory category ) throws Exception { - MetadataCategory existingCategory = categoryRepository.findById(tagIdentifier).get(); - if (existingCategory != null) { + Optional existingCategory = categoryRepository.findById(tagIdentifier); + if (existingCategory.isPresent()) { updateCategory(tagIdentifier, category); } else { throw new ResourceNotFoundException(String.format( @@ -227,13 +228,13 @@ public ResponseEntity deleteTag( @PathVariable Integer tagIdentifier ) throws Exception { - MetadataCategory category = categoryRepository.findById(tagIdentifier).get(); - if (category != null) { - long recordsCount = metadataRepository.count((Specification) MetadataSpecs.hasCategory(category)); + Optional category = categoryRepository.findById(tagIdentifier); + if (category.isPresent()) { + long recordsCount = metadataRepository.count((Specification) MetadataSpecs.hasCategory(category.get())); if (recordsCount > 0l) { throw new IllegalArgumentException(String.format( "Tag '%s' is assigned to %d records. Update records first in order to remove that tag.", - category.getName(), // TODO: Return in user language + category.get().getName(), // TODO: Return in user language recordsCount )); } else { diff --git a/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java b/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java index bf87595f371..0e4cf6bf1af 100644 --- a/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java +++ b/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java @@ -185,16 +185,16 @@ public void getGroupLogo( throw new RuntimeException("ServiceContext not available"); } - Group group = groupRepository.findById(groupId).get(); - if (group == null) { + Optional group = groupRepository.findById(groupId); + if (!group.isPresent()) { throw new ResourceNotFoundException(messages.getMessage("api.groups.group_not_found", new Object[]{groupId}, locale)); } try { final Resources resources = context.getBean(Resources.class); - final String logoUUID = group.getLogo(); + final String logoUUID = group.get().getLogo(); if (StringUtils.isNotBlank(logoUUID) && !logoUUID.startsWith("http://") && !logoUUID.startsWith("https//")) { - try (Resources.ResourceHolder image = getImage(resources, serviceContext, group)) { + try (Resources.ResourceHolder image = getImage(resources, serviceContext, group.get())) { if (image != null) { FileTime lastModifiedTime = image.getLastModifiedTime(); response.setDateHeader("Expires", System.currentTimeMillis() + SIX_HOURS * 1000L); @@ -357,14 +357,14 @@ public Group getGroup( @PathVariable Integer groupIdentifier ) throws Exception { - final Group group = groupRepository.findById(groupIdentifier).get(); + final Optional group = groupRepository.findById(groupIdentifier); - if (group == null) { + if (!group.isPresent()) { throw new ResourceNotFoundException(String.format( MSG_GROUP_WITH_IDENTIFIER_NOT_FOUND, groupIdentifier )); } - return group; + return group.get(); } @io.swagger.v3.oas.annotations.Operation( @@ -393,9 +393,9 @@ public List getGroupUsers( @PathVariable Integer groupIdentifier ) throws Exception { - final Group group = groupRepository.findById(groupIdentifier).get(); + final Optional group = groupRepository.findById(groupIdentifier); - if (group == null) { + if (!group.isPresent()) { throw new ResourceNotFoundException(String.format( MSG_GROUP_WITH_IDENTIFIER_NOT_FOUND, groupIdentifier )); @@ -436,8 +436,8 @@ public void updateGroup( @RequestBody Group group ) throws Exception { - final Group existing = groupRepository.findById(groupIdentifier).get(); - if (existing == null) { + final Optional existing = groupRepository.findById(groupIdentifier); + if (!existing.isPresent()) { throw new ResourceNotFoundException(String.format( MSG_GROUP_WITH_IDENTIFIER_NOT_FOUND, groupIdentifier )); @@ -484,9 +484,9 @@ public void deleteGroup( @Parameter(hidden = true) ServletRequest request ) throws Exception { - Group group = groupRepository.findById(groupIdentifier).get(); + Optional group = groupRepository.findById(groupIdentifier); - if (group != null) { + if (group.isPresent()) { List reindex = operationAllowedRepo.findAllIds(OperationAllowedSpecs.hasGroupId(groupIdentifier), OperationAllowedId_.metadataId); @@ -498,17 +498,17 @@ public void deleteGroup( } else if (reindex.size() > 0 && !force) { throw new NotAllowedException(String.format( "Group %s has privileges associated with %d record(s). Add 'force' parameter to remove it or remove privileges associated with that group first.", - group.getName(), reindex.size() + group.get().getName(), reindex.size() )); } - final List users = userGroupRepository.findUserIds(where(UserGroupSpecs.hasGroupId(group.getId()))); + final List users = userGroupRepository.findUserIds(where(UserGroupSpecs.hasGroupId(group.get().getId()))); if (users.size() > 0 && force) { userGroupRepository.deleteAllByIdAttribute(UserGroupId_.groupId, Arrays.asList(groupIdentifier)); } else if (users.size() > 0 && !force) { throw new NotAllowedException(String.format( "Group %s is associated with %d user(s). Add 'force' parameter to remove it or remove users associated with that group first.", - group.getName(), users.size() + group.get().getName(), users.size() )); } diff --git a/services/src/main/java/org/fao/geonet/api/identifiers/IdentifiersApi.java b/services/src/main/java/org/fao/geonet/api/identifiers/IdentifiersApi.java index 7020c3ab092..51e920a4f78 100644 --- a/services/src/main/java/org/fao/geonet/api/identifiers/IdentifiersApi.java +++ b/services/src/main/java/org/fao/geonet/api/identifiers/IdentifiersApi.java @@ -41,6 +41,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; @RequestMapping(value = { "/{portal}/api/identifiers" @@ -122,10 +123,10 @@ public ResponseEntity addIdentifier( @RequestBody MetadataIdentifierTemplate metadataIdentifierTemplate ) throws Exception { - final MetadataIdentifierTemplate existingId = metadataIdentifierTemplateRepository - .findById(metadataIdentifierTemplate.getId()).get(); + final Optional existingId = metadataIdentifierTemplateRepository + .findById(metadataIdentifierTemplate.getId()); - if (existingId != null) { + if (existingId.isPresent()) { throw new IllegalArgumentException(String.format( "A metadata identifier template with id '%d' already exist.", metadataIdentifierTemplate.getId() @@ -170,9 +171,9 @@ public void updateIdentifier( @RequestBody MetadataIdentifierTemplate metadataIdentifierTemplate ) throws Exception { - MetadataIdentifierTemplate existing = - metadataIdentifierTemplateRepository.findById(identifier).get(); - if (existing == null) { + Optional existing = + metadataIdentifierTemplateRepository.findById(identifier); + if (!existing.isPresent()) { throw new ResourceNotFoundException(String.format( MSG_NO_METADATA_IDENTIFIER_FOUND_WITH_ID, identifier @@ -210,9 +211,9 @@ public void deleteIdentifier( @PathVariable int identifier ) throws Exception { - MetadataIdentifierTemplate existing = - metadataIdentifierTemplateRepository.findById(identifier).get(); - if (existing == null) { + Optional existing = + metadataIdentifierTemplateRepository.findById(identifier); + if (!existing.isPresent()) { throw new ResourceNotFoundException(String.format( MSG_NO_METADATA_IDENTIFIER_FOUND_WITH_ID, identifier diff --git a/services/src/main/java/org/fao/geonet/api/records/formatters/FormatterApi.java b/services/src/main/java/org/fao/geonet/api/records/formatters/FormatterApi.java index 223af6d1f67..5cbe8e883f4 100644 --- a/services/src/main/java/org/fao/geonet/api/records/formatters/FormatterApi.java +++ b/services/src/main/java/org/fao/geonet/api/records/formatters/FormatterApi.java @@ -792,8 +792,8 @@ public StoreInfoAndDataLoadResult call() throws Exception { long changeDate = fparams.metadataInfo.getDataInfo().getChangeDate().toDate().getTime(); final Specification isPublished = OperationAllowedSpecs.isPublic(ReservedOperation.view); final Specification hasMdId = OperationAllowedSpecs.hasMetadataId(key.mdId); - final OperationAllowed one = serviceContext.getBean(OperationAllowedRepository.class).findOne(where(hasMdId).and(isPublished)).get(); - final boolean isPublishedMd = one != null; + final Optional one = serviceContext.getBean(OperationAllowedRepository.class).findOne(where(hasMdId).and(isPublished)); + final boolean isPublishedMd = one.isPresent(); Key withheldKey = null; FormatMetadata loadWithheld = null; diff --git a/services/src/main/java/org/fao/geonet/api/selections/UserSelectionsApi.java b/services/src/main/java/org/fao/geonet/api/selections/UserSelectionsApi.java index 0141ac0d1c8..037d5a5b78a 100644 --- a/services/src/main/java/org/fao/geonet/api/selections/UserSelectionsApi.java +++ b/services/src/main/java/org/fao/geonet/api/selections/UserSelectionsApi.java @@ -47,6 +47,7 @@ import javax.servlet.http.HttpSession; import java.util.List; import java.util.Map; +import java.util.Optional; /** * Select a list of elements stored in session. @@ -111,15 +112,15 @@ public ResponseEntity createPersistentSelectionType( Selection selection ) throws Exception { - Selection existingSelection = selectionRepository.findById(selection.getId()).get(); - if (existingSelection != null) { + Optional existingSelectionById = selectionRepository.findById(selection.getId()); + if (existingSelectionById.isPresent()) { throw new IllegalArgumentException(String.format( "A selection with id '%d' already exist. Choose another id or unset it.", selection.getId() )); } - existingSelection = selectionRepository.findOneByName(selection.getName()); + Selection existingSelection = selectionRepository.findOneByName(selection.getName()); if (existingSelection != null) { throw new IllegalArgumentException(String.format( "A selection with name '%s' already exist. Choose another name.", selection.getName() @@ -164,8 +165,8 @@ public ResponseEntity updateUserSelection( @RequestBody Selection selection ) throws Exception { - Selection existingSelection = selectionRepository.findById(selectionIdentifier).get(); - if (existingSelection != null) { + Optional existingSelection = selectionRepository.findById(selectionIdentifier); + if (existingSelection.isPresent()) { selection.setId(selectionIdentifier); selectionRepository.save(selection); // selectionRepository.update(selectionIdentifier, entity -> { @@ -208,8 +209,8 @@ public ResponseEntity deleteUserSelection( @PathVariable Integer selectionIdentifier ) throws Exception { - Selection selection = selectionRepository.findById(selectionIdentifier).get(); - if (selection != null) { + Optional selection = selectionRepository.findById(selectionIdentifier); + if (selection.isPresent()) { umsRepository.deleteAllBySelection(selectionIdentifier); selectionRepository.deleteById(selectionIdentifier); } else { @@ -246,23 +247,23 @@ List get( HttpSession httpSession ) throws Exception { - Selection selection = selectionRepository.findById(selectionIdentifier).get(); - if (selection == null) { + Optional selection = selectionRepository.findById(selectionIdentifier); + if (!selection.isPresent()) { throw new ResourceNotFoundException(String.format( "Selection with id '%d' does not exist.", selectionIdentifier )); } - User user = userRepository.findById(userIdentifier).get(); - if (user == null) { + Optional user = userRepository.findById(userIdentifier); + if (!user.isPresent()) { throw new ResourceNotFoundException(String.format( "User with id '%d' does not exist.", selectionIdentifier )); } - if (selection != null) { + if (selection.isPresent()) { return umsRepository.findMetadata(selectionIdentifier, userIdentifier); } return null; @@ -301,16 +302,16 @@ ResponseEntity addToUserSelection( HttpSession httpSession ) throws Exception { - Selection selection = selectionRepository.findById(selectionIdentifier).get(); - if (selection == null) { + Optional selection = selectionRepository.findById(selectionIdentifier); + if (!selection.isPresent()) { throw new ResourceNotFoundException(String.format( "Selection with id '%d' does not exist.", selectionIdentifier )); } - User user = userRepository.findById(userIdentifier).get(); - if (user == null) { + Optional user = userRepository.findById(userIdentifier); + if (!user.isPresent()) { throw new ResourceNotFoundException(String.format( "User with id '%d' does not exist.", selectionIdentifier @@ -320,7 +321,7 @@ ResponseEntity addToUserSelection( for (String u : uuid) { // Check record exist if (metadataRepository.existsMetadataUuid(u)) { - UserSavedSelection e = new UserSavedSelection(selection, user, u); + UserSavedSelection e = new UserSavedSelection(selection.get(), user.get(), u); try { umsRepository.save(e); } catch (Exception e1) { @@ -366,16 +367,16 @@ ResponseEntity deleteFromUserSelection( HttpSession httpSession ) throws Exception { - Selection selection = selectionRepository.findById(selectionIdentifier).get(); - if (selection == null) { + Optional selection = selectionRepository.findById(selectionIdentifier); + if (!selection.isPresent()) { throw new ResourceNotFoundException(String.format( "Selection with id '%d' does not exist.", selectionIdentifier )); } - User user = userRepository.findById(userIdentifier).get(); - if (user == null) { + Optional user = userRepository.findById(userIdentifier); + if (!user.isPresent()) { throw new ResourceNotFoundException(String.format( "User with id '%d' does not exist.", selectionIdentifier diff --git a/services/src/main/java/org/fao/geonet/api/uisetting/UiSettingApi.java b/services/src/main/java/org/fao/geonet/api/uisetting/UiSettingApi.java index 6ec9e4270b3..82d7f53b033 100644 --- a/services/src/main/java/org/fao/geonet/api/uisetting/UiSettingApi.java +++ b/services/src/main/java/org/fao/geonet/api/uisetting/UiSettingApi.java @@ -196,8 +196,8 @@ public ResponseEntity updateUiConfiguration( UiSetting uiConfiguration, @Parameter(hidden = true) HttpSession httpSession ) throws Exception { - UiSetting one = uiSettingsRepository.findById(uiIdentifier).get(); - if (one != null) { + Optional one = uiSettingsRepository.findById(uiIdentifier); + if (one.isPresent()) { // For user admin, check that the UI is used by a portal managed by the user. UserSession session = ApiUtils.getUserSession(httpSession); boolean isUserAdmin = session.getProfile().equals(Profile.UserAdmin); @@ -254,8 +254,8 @@ public ResponseEntity deleteUiConfiguration( @PathVariable String uiIdentifier ) throws Exception { - UiSetting one = uiSettingsRepository.findById(uiIdentifier).get(); - if (one != null) { + Optional one = uiSettingsRepository.findById(uiIdentifier); + if (one.isPresent()) { uiSettingsRepository.deleteById(uiIdentifier); } else { throw new ResourceNotFoundException(String.format( diff --git a/services/src/main/java/org/fao/geonet/api/users/UsersApi.java b/services/src/main/java/org/fao/geonet/api/users/UsersApi.java index df38224cd16..f48820aac48 100644 --- a/services/src/main/java/org/fao/geonet/api/users/UsersApi.java +++ b/services/src/main/java/org/fao/geonet/api/users/UsersApi.java @@ -153,9 +153,9 @@ public User getUser( if (myProfile.equals(Profile.Administrator) || myProfile.equals(Profile.UserAdmin) || myUserId.equals(Integer.toString(userIdentifier))) { - User user = userRepository.findById(userIdentifier).get(); + Optional user = userRepository.findById(userIdentifier); - if (user == null) { + if (!user.isPresent()) { throw new UserNotFoundEx(Integer.toString(userIdentifier)); } @@ -170,7 +170,7 @@ public User getUser( } } - return user; + return user.get(); } else { throw new IllegalArgumentException("You don't have rights to do this"); } @@ -505,16 +505,16 @@ public ResponseEntity resetUserPassword( throw new IllegalArgumentException("You don't have rights to do this"); } - User user = userRepository.findById(userIdentifier).get(); - if (user == null) { + Optional user = userRepository.findById(userIdentifier); + if (!user.isPresent()) { throw new UserNotFoundEx(Integer.toString(userIdentifier)); } String passwordHash = PasswordUtil.encoder(ApplicationContextHolder.get()).encode( password); - user.getSecurity().setPassword(passwordHash); - user.getSecurity().getSecurityNotifications().remove(UserSecurityNotification.UPDATE_HASH_REQUIRED); - userRepository.save(user); + user.get().getSecurity().setPassword(passwordHash); + user.get().getSecurity().getSecurityNotifications().remove(UserSecurityNotification.UPDATE_HASH_REQUIRED); + userRepository.save(user.get()); return new ResponseEntity(HttpStatus.NO_CONTENT); } diff --git a/services/src/main/java/org/fao/geonet/api/usersearches/UserSearchesApi.java b/services/src/main/java/org/fao/geonet/api/usersearches/UserSearchesApi.java index a10a3a02072..8a59adf9cb7 100644 --- a/services/src/main/java/org/fao/geonet/api/usersearches/UserSearchesApi.java +++ b/services/src/main/java/org/fao/geonet/api/usersearches/UserSearchesApi.java @@ -58,10 +58,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; import javax.servlet.http.HttpSession; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; @EnableWebMvc @Service @@ -374,8 +371,8 @@ public void updateCustomUserSearch( UserSession session = ApiUtils.getUserSession(httpSession); Profile myProfile = session.getProfile(); - final UserSearch existing = userSearchRepository.findById(searchIdentifier).get(); - if (existing == null) { + final Optional existing = userSearchRepository.findById(searchIdentifier); + if (!existing.isPresent()) { throw new ResourceNotFoundException(String.format( MSG_USERSEARCH_WITH_IDENTIFIER_NOT_FOUND, searchIdentifier )); @@ -446,12 +443,12 @@ public ResponseEntity deleteUserCustomSerach( */ private UserSearch retrieveUserSearch(UserSearchRepository userSearchRepository, Integer searchIdentifier) throws ResourceNotFoundEx { - UserSearch userSearch = userSearchRepository.findById(searchIdentifier).get(); + Optional userSearch = userSearchRepository.findById(searchIdentifier); - if (userSearch == null) { + if (!userSearch.isPresent()) { throw new ResourceNotFoundEx("User search not found"); } - return userSearch; + return userSearch.get(); } } diff --git a/services/src/main/java/org/fao/geonet/api/usersearches/model/UserSearchDto.java b/services/src/main/java/org/fao/geonet/api/usersearches/model/UserSearchDto.java index 2356951a58d..80edc2b5202 100644 --- a/services/src/main/java/org/fao/geonet/api/usersearches/model/UserSearchDto.java +++ b/services/src/main/java/org/fao/geonet/api/usersearches/model/UserSearchDto.java @@ -176,9 +176,9 @@ public UserSearch asUserSearch() { } UserRepository userRepository = ApplicationContextHolder.get().getBean(UserRepository.class); - User user = userRepository.findById(this.getCreatorId()).get(); - if (user != null) { - userSearch.setCreator(user); + Optional user = userRepository.findById(this.getCreatorId()); + if (user.isPresent()) { + userSearch.setCreator(user.get()); } this.getNames().forEach((key, value) -> userSearch.getLabelTranslations().put(key, value)); @@ -187,10 +187,10 @@ public UserSearch asUserSearch() { Set groups = new HashSet<>(); getGroups().forEach(groupId -> { if (groupId != null) { - Group g = groupRepository.findById(groupId).get(); + Optional g = groupRepository.findById(groupId); - if (g != null) { - groups.add(g); + if (g.isPresent()) { + groups.add(g.get()); } } }); diff --git a/services/src/test/java/org/fao/geonet/api/categories/TagsApiTest.java b/services/src/test/java/org/fao/geonet/api/categories/TagsApiTest.java index 9cf394564f4..9b8c90b0360 100644 --- a/services/src/test/java/org/fao/geonet/api/categories/TagsApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/categories/TagsApiTest.java @@ -40,6 +40,7 @@ import org.springframework.web.context.WebApplicationContext; import java.util.List; +import java.util.Optional; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; @@ -91,8 +92,8 @@ public void getTag() throws Exception { @Test public void getNonExistingTag() throws Exception { - MetadataCategory category = _categoriesRepo.findById(222).get(); - Assert.assertNull(category); + Optional category = _categoriesRepo.findById(222); + Assert.assertFalse(category.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); @@ -116,8 +117,8 @@ public void deleteTag() throws Exception { @Test public void deleteNonExistingTag() throws Exception { - MetadataCategory category = _categoriesRepo.findById(222).get(); - Assert.assertNull(category); + Optional category = _categoriesRepo.findById(222); + Assert.assertFalse(category.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); @@ -183,8 +184,8 @@ public void updateTag() throws Exception { @Test public void updateNonExistingTag() throws Exception { - MetadataCategory category = _categoriesRepo.findById(222).get(); - Assert.assertNull(category); + Optional category = _categoriesRepo.findById(222); + Assert.assertFalse(category.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); diff --git a/services/src/test/java/org/fao/geonet/api/groups/GroupsApiTest.java b/services/src/test/java/org/fao/geonet/api/groups/GroupsApiTest.java index 1184e4ef281..02f093d3654 100644 --- a/services/src/test/java/org/fao/geonet/api/groups/GroupsApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/groups/GroupsApiTest.java @@ -42,6 +42,8 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import java.util.Optional; + import static org.hamcrest.Matchers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -130,8 +132,8 @@ public void getGroupUsers() throws Exception { @Test public void getGroupUsersNonExistingGroup() throws Exception { - Group nonExistingGroup = _groupRepo.findById(222).get(); - Assert.assertNull(nonExistingGroup); + Optional nonExistingGroup = _groupRepo.findById(222); + Assert.assertFalse(nonExistingGroup.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); @@ -176,8 +178,8 @@ public void deleteGroup() throws Exception { public void deleteNonExistingGroup() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); - Group groupToDelete = _groupRepo.findById(222).get(); - Assert.assertNull(groupToDelete); + Optional groupToDelete = _groupRepo.findById(222); + Assert.assertFalse(groupToDelete.isPresent()); this.mockHttpSession = loginAsAdmin(); @@ -218,10 +220,10 @@ public void updateGroup() throws Exception { @Test public void updateNonExistingGroup() throws Exception { - Group groupToUpdate = _groupRepo.findById(222).get(); - Assert.assertNull(groupToUpdate); + Optional groupToUpdateOptional = _groupRepo.findById(222); + Assert.assertFalse(groupToUpdateOptional.isPresent()); - groupToUpdate = new Group(); + Group groupToUpdate = new Group(); groupToUpdate.setId(222); groupToUpdate.setEmail("group@mail.com"); groupToUpdate.setDescription("A test group"); diff --git a/services/src/test/java/org/fao/geonet/api/identifiers/IdentifiersApiTest.java b/services/src/test/java/org/fao/geonet/api/identifiers/IdentifiersApiTest.java index 8d25e39c00a..cb178eb5d6a 100644 --- a/services/src/test/java/org/fao/geonet/api/identifiers/IdentifiersApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/identifiers/IdentifiersApiTest.java @@ -39,6 +39,8 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import java.util.Optional; + import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; @@ -189,11 +191,11 @@ public void updateIdentifier() throws Exception { @Test public void updateNonExistingIdentifier() throws Exception { - MetadataIdentifierTemplate metadataIdentifierTemplateToUpdate = - metadataIdentifierTemplateRepo.findById(222).get(); - Assert.assertNull(metadataIdentifierTemplateToUpdate); + Optional metadataIdentifierTemplateToUpdateOptional = + metadataIdentifierTemplateRepo.findById(222); + Assert.assertFalse(metadataIdentifierTemplateToUpdateOptional.isPresent()); - metadataIdentifierTemplateToUpdate = new MetadataIdentifierTemplate(); + MetadataIdentifierTemplate metadataIdentifierTemplateToUpdate = new MetadataIdentifierTemplate(); metadataIdentifierTemplateToUpdate.setId(222); metadataIdentifierTemplateToUpdate.setName("non-exisiting-to-upfste"); metadataIdentifierTemplateToUpdate.setTemplate("{AAAA}"); @@ -239,9 +241,9 @@ public void deleteIdentifier() throws Exception { public void deleteNonExistingIdentifier() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); - MetadataIdentifierTemplate identifierTemplateToDelete = - metadataIdentifierTemplateRepo.findById(222).get(); - Assert.assertNull(identifierTemplateToDelete); + Optional identifierTemplateToDelete = + metadataIdentifierTemplateRepo.findById(222); + Assert.assertFalse(identifierTemplateToDelete.isPresent()); this.mockHttpSession = loginAsAdmin(); diff --git a/services/src/test/java/org/fao/geonet/api/mapservers/MapServersApiTest.java b/services/src/test/java/org/fao/geonet/api/mapservers/MapServersApiTest.java index bbfc80feeaf..696481c189b 100644 --- a/services/src/test/java/org/fao/geonet/api/mapservers/MapServersApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/mapservers/MapServersApiTest.java @@ -39,6 +39,8 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import java.util.Optional; + import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -160,10 +162,10 @@ public void updateMapserver() throws Exception { @Test public void updateNonExistingMapServer() throws Exception { - MapServer mapServerToUpdate = mapServerRepo.findById(222).get(); - Assert.assertNull(mapServerToUpdate); + Optional mapServerToUpdateOptional = mapServerRepo.findById(222); + Assert.assertFalse(mapServerToUpdateOptional.isPresent()); - mapServerToUpdate = new MapServer(); + MapServer mapServerToUpdate = new MapServer(); mapServerToUpdate.setId(222); mapServerToUpdate.setName(mapServerToUpdate.getName() + "-update"); @@ -201,14 +203,14 @@ public void deleteExistingMapserver() throws Exception { .accept(MediaType.parseMediaType("application/json"))) .andExpect(status().is(204)); - mapServerToDelete = mapServerRepo.findById(mapServerId).get(); - Assert.assertNull(mapServerToDelete); + Optional mapServerToDeleteOptional = mapServerRepo.findById(mapServerId); + Assert.assertFalse(mapServerToDeleteOptional.isPresent()); } @Test public void deleteNonExistingMapserver() throws Exception { - MapServer mapServerToDelete = mapServerRepo.findById(222).get(); - Assert.assertNull(mapServerToDelete); + Optional mapServerToUpdateOptional = mapServerRepo.findById(222); + Assert.assertFalse(mapServerToUpdateOptional.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); diff --git a/services/src/test/java/org/fao/geonet/api/uisetting/UiApiTest.java b/services/src/test/java/org/fao/geonet/api/uisetting/UiApiTest.java index ec04f29378a..cff24123bcc 100644 --- a/services/src/test/java/org/fao/geonet/api/uisetting/UiApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/uisetting/UiApiTest.java @@ -41,6 +41,7 @@ import org.springframework.web.context.WebApplicationContext; import java.util.List; +import java.util.Optional; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; @@ -66,8 +67,8 @@ public class UiApiTest extends AbstractServiceIntegrationTest { @Test public void putUiConfiguration() throws Exception { - UiSetting uiConfig = uiSettingsRepository.findById("default").get(); - Assert.assertNull(uiConfig); + Optional uiConfig = uiSettingsRepository.findById("default"); + Assert.assertFalse(uiConfig.isPresent()); UiSetting newUiConfig = new UiSetting(); newUiConfig.setId("default"); @@ -123,14 +124,15 @@ public void putUiConfiguration() throws Exception { .andExpect(status().is(204)); // Delete - uiConfiguration = uiSettingsRepository.findById("default").get(); - Assert.assertNotNull(uiConfiguration); + Assert.assertTrue( uiSettingsRepository.existsById("default")); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockMvc.perform(delete("/srv/api/ui/" + uiConfiguration.getId()) .accept(MediaType.parseMediaType("application/json"))) .andExpect(status().is(204)); + + Assert.assertFalse( uiSettingsRepository.existsById("default")); } @Test @@ -148,8 +150,8 @@ public void getUiConfigurations() throws Exception { @Test public void getNonExistingUiConfiguration() throws Exception { - UiSetting one = uiSettingsRepository.findById("222").get(); - Assert.assertNull(one); + Optional one = uiSettingsRepository.findById("222"); + Assert.assertFalse(one.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); @@ -162,8 +164,8 @@ public void getNonExistingUiConfiguration() throws Exception { @Test public void updateNonExistingUiConfiguration() throws Exception { - UiSetting uiConfiguration = uiSettingsRepository.findById("222").get(); - Assert.assertNull(uiConfiguration); + Optional one = uiSettingsRepository.findById("222"); + Assert.assertFalse(one.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); @@ -175,8 +177,8 @@ public void updateNonExistingUiConfiguration() throws Exception { @Test public void deleteNonExistingUiConfiguration() throws Exception { - UiSetting uiConfiguration = uiSettingsRepository.findById("222").get(); - Assert.assertNull(uiConfiguration); + Optional one = uiSettingsRepository.findById("222"); + Assert.assertFalse(one.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); diff --git a/services/src/test/java/org/fao/geonet/api/users/UsersApiTest.java b/services/src/test/java/org/fao/geonet/api/users/UsersApiTest.java index 4559ecfd502..87417e666d7 100644 --- a/services/src/test/java/org/fao/geonet/api/users/UsersApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/users/UsersApiTest.java @@ -45,6 +45,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import static org.fao.geonet.repository.specification.UserGroupSpecs.hasUserId; import static org.hamcrest.Matchers.*; @@ -153,8 +154,8 @@ public void deleteExistingUser() throws Exception { @Test public void deleteNonExistingUser() throws Exception { - User userToDelete = _userRepo.findById(222).get(); - Assert.assertNull(userToDelete); + Optional userToDelete = _userRepo.findById(222); + Assert.assertFalse(userToDelete.isPresent()); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); diff --git a/services/src/test/java/org/fao/geonet/api/usersearches/UserSearchesApiTest.java b/services/src/test/java/org/fao/geonet/api/usersearches/UserSearchesApiTest.java index 4afd7c10eef..a37633de42d 100644 --- a/services/src/test/java/org/fao/geonet/api/usersearches/UserSearchesApiTest.java +++ b/services/src/test/java/org/fao/geonet/api/usersearches/UserSearchesApiTest.java @@ -41,6 +41,7 @@ import org.springframework.web.context.WebApplicationContext; import java.util.Date; +import java.util.Optional; import static org.hamcrest.Matchers.hasSize; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -108,8 +109,8 @@ public void deleteUserSearch() throws Exception { .accept(MediaType.parseMediaType("application/json"))) .andExpect(status().is(204)); - userSearchToDelete = userSearchRepository.findById(userSearchToDelete.getId()).get(); - Assert.assertNull(userSearchToDelete); + Optional userSearchDeleted = userSearchRepository.findById(userSearchToDelete.getId()); + Assert.assertFalse(userSearchDeleted.isPresent()); }