Skip to content

Commit

Permalink
[FSTORE-1139] Get feature store requests can throw NullPointerExcepti…
Browse files Browse the repository at this point in the history
…on is the project is not properly initialized (#1646)
  • Loading branch information
SirOibaf committed Dec 29, 2023
1 parent 5112907 commit 4c0ebf7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import io.hops.hopsworks.exceptions.UserException;
import io.hops.hopsworks.persistence.entity.dataset.Dataset;
import io.hops.hopsworks.persistence.entity.dataset.DatasetSharedWith;
import io.hops.hopsworks.persistence.entity.dataset.DatasetType;
import io.hops.hopsworks.persistence.entity.featurestore.Featurestore;
import io.hops.hopsworks.persistence.entity.featurestore.storageconnector.FeaturestoreConnectorType;
import io.hops.hopsworks.persistence.entity.project.Project;
Expand All @@ -64,6 +63,7 @@
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -136,7 +136,7 @@ public List<FeaturestoreDTO> getFeaturestoresForProject(Project project) throws
*/
public Dataset getProjectFeaturestoreDataset(Project project) throws FeaturestoreException {
return project.getDatasetCollection().stream()
.filter(ds -> ds.getDsType() == DatasetType.FEATURESTORE)
.filter(ds -> ds.getFeatureStore() != null)
.findFirst()
.orElseThrow(() -> new FeaturestoreException(RESTCodes.FeaturestoreErrorCode.FEATURESTORE_NOT_FOUND,
Level.INFO, "Could not find feature store for project: " + project.getName()));
Expand All @@ -150,8 +150,8 @@ public Dataset getProjectFeaturestoreDataset(Project project) throws Featurestor
public Featurestore getProjectFeaturestore(Project project) throws FeaturestoreException {
Collection<Dataset> dsInProject = project.getDatasetCollection();
return dsInProject.stream()
.filter(ds -> ds.getDsType() == DatasetType.FEATURESTORE)
.map(Dataset::getFeatureStore)
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new FeaturestoreException(RESTCodes.FeaturestoreErrorCode.FEATURESTORE_NOT_FOUND,
Level.INFO, "Could not find feature store for project: " + project.getName()));
Expand All @@ -163,16 +163,17 @@ public Featurestore getProjectFeaturestore(Project project) throws FeaturestoreE
* @param project the project to list featurestores for
* @return a list of featurestore entities
*/
private List<Featurestore> getProjectFeaturestores(Project project) {
public List<Featurestore> getProjectFeaturestores(Project project) {
Collection<Dataset> dsInProject = project.getDatasetCollection();
// Add all datasets shared with the project
dsInProject.addAll(project.getDatasetSharedWithCollection().stream()
// Filter out datasets which have not been accepted
.filter(DatasetSharedWith::getAccepted)
.map(DatasetSharedWith::getDataset).collect(Collectors.toList()));
return dsInProject.stream()
.filter(ds -> ds.getDsType() == DatasetType.FEATURESTORE)
.map(Dataset::getFeatureStore).collect(Collectors.toList());
.map(Dataset::getFeatureStore)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of Hopsworks
* Copyright (C) 2023, Hopsworks AB. All rights reserved
*
* Hopsworks is free software: you can redistribute it and/or modify it under the terms of
* the GNU Affero General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* Hopsworks is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

package io.hops.hopsworks.common.featurestore;

import io.hops.hopsworks.persistence.entity.dataset.Dataset;
import io.hops.hopsworks.persistence.entity.dataset.DatasetType;
import io.hops.hopsworks.persistence.entity.featurestore.Featurestore;
import io.hops.hopsworks.persistence.entity.project.Project;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestFeaturestoreController {

@Test
public void testGetProjectFeaturestores() {
FeaturestoreController featurestoreController = new FeaturestoreController();
Featurestore featurestore = new Featurestore();
featurestore.setId(1);

Dataset fsDataset = new Dataset();
fsDataset.setDsType(DatasetType.FEATURESTORE);
fsDataset.setFeatureStore(featurestore);

Project project = new Project();
project.setDatasetCollection(Arrays.asList(fsDataset));
project.setDatasetSharedWithCollection(new ArrayList<>());

List<Featurestore> projectFeaturestores = featurestoreController.getProjectFeaturestores(project);
Assert.assertEquals(featurestore.getId(), projectFeaturestores.get(0).getId());
}

@Test
public void testGetProjectFeaturestores_notInitialized() {
FeaturestoreController featurestoreController = new FeaturestoreController();

Dataset fsDataset = new Dataset();
fsDataset.setDsType(DatasetType.FEATURESTORE);
fsDataset.setFeatureStore(null);

Project project = new Project();
project.setDatasetCollection(Arrays.asList(fsDataset));
project.setDatasetSharedWithCollection(new ArrayList<>());

List<Featurestore> projectFeaturestores = featurestoreController.getProjectFeaturestores(project);
Assert.assertEquals(0, projectFeaturestores.size());
}

}

0 comments on commit 4c0ebf7

Please sign in to comment.