Skip to content

Commit 0202dfb

Browse files
Joostrothweilerwoop
authored andcommitted
Create project if not exists on applyFeatureSet (#596)
* Create project if not exists on apply * Update comments to reflect new situation * Fixed typo in comment Co-authored-by: Joost Rothweiler <=> Co-authored-by: Willem Pienaar <6728866+woop@users.noreply.github.com>
1 parent 08cdfb4 commit 0202dfb

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

core/src/main/java/feast/core/service/SpecService.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,14 @@ public ApplyFeatureSetResponse applyFeatureSet(FeatureSetProto.FeatureSet newFea
285285
// Validate incoming feature set
286286
FeatureSetValidator.validateSpec(newFeatureSet);
287287

288-
// Ensure that the project already exists
288+
// Find project or create new one if it does not exist
289289
String project_name = newFeatureSet.getSpec().getProject();
290290
Project project =
291291
projectRepository
292292
.findById(newFeatureSet.getSpec().getProject())
293-
.orElseThrow(
294-
() ->
295-
new IllegalArgumentException(
296-
String.format(
297-
"Project name does not exist. Please create a project first: %s",
298-
project_name)));
299-
300-
// Ensure that the project is not archived
293+
.orElse(new Project(project_name));
294+
295+
// Ensure that the project retrieved from repository is not archived
301296
if (project.isArchived()) {
302297
throw new IllegalArgumentException(String.format("Project is archived: %s", project_name));
303298
}

core/src/test/java/feast/core/service/SpecServiceTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public void setUp() {
150150
when(projectRepository.findAllByArchivedIsFalse())
151151
.thenReturn(Collections.singletonList(new Project("project1")));
152152
when(projectRepository.findById("project1")).thenReturn(Optional.of(new Project("project1")));
153+
Project archivedProject = new Project("archivedproject");
154+
archivedProject.setArchived(true);
155+
when(projectRepository.findById(archivedProject.getName()))
156+
.thenReturn(Optional.of(archivedProject));
153157

154158
Store store1 = newDummyStore("SERVING");
155159
Store store2 = newDummyStore("WAREHOUSE");
@@ -481,6 +485,55 @@ public void applyFeatureSetShouldNotCreateFeatureSetIfFieldsUnordered() {
481485
equalTo(incomingFeatureSet.getSpec().getName()));
482486
}
483487

488+
@Test
489+
public void applyFeatureSetShouldCreateProjectWhenNotAlreadyExists()
490+
throws InvalidProtocolBufferException {
491+
Field f3f1 = new Field("f3f1", Enum.INT64);
492+
Field f3f2 = new Field("f3f2", Enum.INT64);
493+
Field f3e1 = new Field("f3e1", Enum.STRING);
494+
FeatureSetProto.FeatureSet incomingFeatureSet =
495+
(new FeatureSet(
496+
"f3",
497+
"newproject",
498+
5,
499+
100L,
500+
Arrays.asList(f3e1),
501+
Arrays.asList(f3f2, f3f1),
502+
defaultSource,
503+
FeatureSetStatus.STATUS_READY))
504+
.toProto();
505+
506+
ApplyFeatureSetResponse applyFeatureSetResponse =
507+
specService.applyFeatureSet(incomingFeatureSet);
508+
assertThat(applyFeatureSetResponse.getStatus(), equalTo(Status.CREATED));
509+
assertThat(
510+
applyFeatureSetResponse.getFeatureSet().getSpec().getProject(),
511+
equalTo(incomingFeatureSet.getSpec().getProject()));
512+
}
513+
514+
@Test
515+
public void applyFeatureSetShouldFailWhenProjectIsArchived()
516+
throws InvalidProtocolBufferException {
517+
Field f3f1 = new Field("f3f1", Enum.INT64);
518+
Field f3f2 = new Field("f3f2", Enum.INT64);
519+
Field f3e1 = new Field("f3e1", Enum.STRING);
520+
FeatureSetProto.FeatureSet incomingFeatureSet =
521+
(new FeatureSet(
522+
"f3",
523+
"archivedproject",
524+
5,
525+
100L,
526+
Arrays.asList(f3e1),
527+
Arrays.asList(f3f2, f3f1),
528+
defaultSource,
529+
FeatureSetStatus.STATUS_READY))
530+
.toProto();
531+
532+
expectedException.expect(IllegalArgumentException.class);
533+
expectedException.expectMessage("Project is archived");
534+
specService.applyFeatureSet(incomingFeatureSet);
535+
}
536+
484537
@Test
485538
public void shouldUpdateStoreIfConfigChanges() throws InvalidProtocolBufferException {
486539
when(storeRepository.findById("SERVING")).thenReturn(Optional.of(stores.get(0)));

0 commit comments

Comments
 (0)