Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test for services. #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions services/src/main/java/org/fao/geonet/api/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand Down
23 changes: 12 additions & 11 deletions services/src/main/java/org/fao/geonet/api/categories/TagsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RequestMapping(value = {
"/{portal}/api/tags"
Expand Down Expand Up @@ -106,8 +107,8 @@ public ResponseEntity<Integer> putTag(
@RequestBody
MetadataCategory category
) throws Exception {
MetadataCategory existingCategory = categoryRepository.findById(category.getId()).get();
if (existingCategory != null) {
Optional<MetadataCategory> existingCategory = categoryRepository.findById(category.getId());
if (existingCategory.isPresent()) {
throw new IllegalArgumentException(String.format(
"A tag with id '%d' already exist", category.getId()
));
Expand Down Expand Up @@ -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<MetadataCategory> category = categoryRepository.findById(tagIdentifier);
if (!category.isPresent()) {
throw new ResourceNotFoundException("Category not found");
}
return category;
return category.get();
}

@io.swagger.v3.oas.annotations.Operation(
Expand Down Expand Up @@ -181,8 +182,8 @@ public ResponseEntity updateTag(
@RequestBody
MetadataCategory category
) throws Exception {
MetadataCategory existingCategory = categoryRepository.findById(tagIdentifier).get();
if (existingCategory != null) {
Optional<MetadataCategory> existingCategory = categoryRepository.findById(tagIdentifier);
if (existingCategory.isPresent()) {
updateCategory(tagIdentifier, category);
} else {
throw new ResourceNotFoundException(String.format(
Expand Down Expand Up @@ -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<Metadata>) MetadataSpecs.hasCategory(category));
Optional<MetadataCategory> category = categoryRepository.findById(tagIdentifier);
if (category.isPresent()) {
long recordsCount = metadataRepository.count((Specification<Metadata>) 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 {
Expand Down
32 changes: 16 additions & 16 deletions services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ public void getGroupLogo(
throw new RuntimeException("ServiceContext not available");
}

Group group = groupRepository.findById(groupId).get();
if (group == null) {
Optional<Group> 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);
Expand Down Expand Up @@ -357,14 +357,14 @@ public Group getGroup(
@PathVariable
Integer groupIdentifier
) throws Exception {
final Group group = groupRepository.findById(groupIdentifier).get();
final Optional<Group> 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(
Expand Down Expand Up @@ -393,9 +393,9 @@ public List<User> getGroupUsers(
@PathVariable
Integer groupIdentifier
) throws Exception {
final Group group = groupRepository.findById(groupIdentifier).get();
final Optional<Group> group = groupRepository.findById(groupIdentifier);

if (group == null) {
if (!group.isPresent()) {
throw new ResourceNotFoundException(String.format(
MSG_GROUP_WITH_IDENTIFIER_NOT_FOUND, groupIdentifier
));
Expand Down Expand Up @@ -436,8 +436,8 @@ public void updateGroup(
@RequestBody
Group group
) throws Exception {
final Group existing = groupRepository.findById(groupIdentifier).get();
if (existing == null) {
final Optional<Group> existing = groupRepository.findById(groupIdentifier);
if (!existing.isPresent()) {
throw new ResourceNotFoundException(String.format(
MSG_GROUP_WITH_IDENTIFIER_NOT_FOUND, groupIdentifier
));
Expand Down Expand Up @@ -484,9 +484,9 @@ public void deleteGroup(
@Parameter(hidden = true)
ServletRequest request
) throws Exception {
Group group = groupRepository.findById(groupIdentifier).get();
Optional<Group> group = groupRepository.findById(groupIdentifier);

if (group != null) {
if (group.isPresent()) {
List<Integer> reindex = operationAllowedRepo.findAllIds(OperationAllowedSpecs.hasGroupId(groupIdentifier),
OperationAllowedId_.metadataId);

Expand All @@ -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<Integer> users = userGroupRepository.findUserIds(where(UserGroupSpecs.hasGroupId(group.getId())));
final List<Integer> 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()
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RequestMapping(value = {
"/{portal}/api/identifiers"
Expand Down Expand Up @@ -122,10 +123,10 @@ public ResponseEntity<Integer> addIdentifier(
@RequestBody
MetadataIdentifierTemplate metadataIdentifierTemplate
) throws Exception {
final MetadataIdentifierTemplate existingId = metadataIdentifierTemplateRepository
.findById(metadataIdentifierTemplate.getId()).get();
final Optional<MetadataIdentifierTemplate> 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()
Expand Down Expand Up @@ -170,9 +171,9 @@ public void updateIdentifier(
@RequestBody
MetadataIdentifierTemplate metadataIdentifierTemplate
) throws Exception {
MetadataIdentifierTemplate existing =
metadataIdentifierTemplateRepository.findById(identifier).get();
if (existing == null) {
Optional<MetadataIdentifierTemplate> existing =
metadataIdentifierTemplateRepository.findById(identifier);
if (!existing.isPresent()) {
throw new ResourceNotFoundException(String.format(
MSG_NO_METADATA_IDENTIFIER_FOUND_WITH_ID,
identifier
Expand Down Expand Up @@ -210,9 +211,9 @@ public void deleteIdentifier(
@PathVariable
int identifier
) throws Exception {
MetadataIdentifierTemplate existing =
metadataIdentifierTemplateRepository.findById(identifier).get();
if (existing == null) {
Optional<MetadataIdentifierTemplate> existing =
metadataIdentifierTemplateRepository.findById(identifier);
if (!existing.isPresent()) {
throw new ResourceNotFoundException(String.format(
MSG_NO_METADATA_IDENTIFIER_FOUND_WITH_ID,
identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ public StoreInfoAndDataLoadResult call() throws Exception {
long changeDate = fparams.metadataInfo.getDataInfo().getChangeDate().toDate().getTime();
final Specification<OperationAllowed> isPublished = OperationAllowedSpecs.isPublic(ReservedOperation.view);
final Specification<OperationAllowed> 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<OperationAllowed> one = serviceContext.getBean(OperationAllowedRepository.class).findOne(where(hasMdId).and(isPublished));
final boolean isPublishedMd = one.isPresent();

Key withheldKey = null;
FormatMetadata loadWithheld = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -111,15 +112,15 @@ public ResponseEntity createPersistentSelectionType(
Selection selection
)
throws Exception {
Selection existingSelection = selectionRepository.findById(selection.getId()).get();
if (existingSelection != null) {
Optional<Selection> 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()
Expand Down Expand Up @@ -164,8 +165,8 @@ public ResponseEntity updateUserSelection(
@RequestBody
Selection selection
) throws Exception {
Selection existingSelection = selectionRepository.findById(selectionIdentifier).get();
if (existingSelection != null) {
Optional<Selection> existingSelection = selectionRepository.findById(selectionIdentifier);
if (existingSelection.isPresent()) {
selection.setId(selectionIdentifier);
selectionRepository.save(selection);
// selectionRepository.update(selectionIdentifier, entity -> {
Expand Down Expand Up @@ -208,8 +209,8 @@ public ResponseEntity deleteUserSelection(
@PathVariable
Integer selectionIdentifier
) throws Exception {
Selection selection = selectionRepository.findById(selectionIdentifier).get();
if (selection != null) {
Optional<Selection> selection = selectionRepository.findById(selectionIdentifier);
if (selection.isPresent()) {
umsRepository.deleteAllBySelection(selectionIdentifier);
selectionRepository.deleteById(selectionIdentifier);
} else {
Expand Down Expand Up @@ -246,23 +247,23 @@ List<String> get(
HttpSession httpSession
)
throws Exception {
Selection selection = selectionRepository.findById(selectionIdentifier).get();
if (selection == null) {
Optional<Selection> 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> 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;
Expand Down Expand Up @@ -301,16 +302,16 @@ ResponseEntity<String> addToUserSelection(
HttpSession httpSession
)
throws Exception {
Selection selection = selectionRepository.findById(selectionIdentifier).get();
if (selection == null) {
Optional<Selection> 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> user = userRepository.findById(userIdentifier);
if (!user.isPresent()) {
throw new ResourceNotFoundException(String.format(
"User with id '%d' does not exist.",
selectionIdentifier
Expand All @@ -320,7 +321,7 @@ ResponseEntity<String> 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) {
Expand Down Expand Up @@ -366,16 +367,16 @@ ResponseEntity deleteFromUserSelection(
HttpSession httpSession
)
throws Exception {
Selection selection = selectionRepository.findById(selectionIdentifier).get();
if (selection == null) {
Optional<Selection> 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> user = userRepository.findById(userIdentifier);
if (!user.isPresent()) {
throw new ResourceNotFoundException(String.format(
"User with id '%d' does not exist.",
selectionIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UiSetting> 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);
Expand Down Expand Up @@ -254,8 +254,8 @@ public ResponseEntity deleteUiConfiguration(
@PathVariable
String uiIdentifier
) throws Exception {
UiSetting one = uiSettingsRepository.findById(uiIdentifier).get();
if (one != null) {
Optional<UiSetting> one = uiSettingsRepository.findById(uiIdentifier);
if (one.isPresent()) {
uiSettingsRepository.deleteById(uiIdentifier);
} else {
throw new ResourceNotFoundException(String.format(
Expand Down
Loading