This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamically set capabilities endpoint based on configuration (#281)
* set supported capabilities dynamically * review: small improvements * adds tests * review: improvements to tests, better mocking, improve maintainability
- Loading branch information
Showing
5 changed files
with
151 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...orer/src/test/java/ai/h2o/mojos/deploy/local/rest/controller/ModelsApiControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package ai.h2o.mojos.deploy.local.rest.controller; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import ai.h2o.mojos.deploy.common.rest.model.CapabilityType; | ||
import ai.h2o.mojos.deploy.common.transform.MojoScorer; | ||
import ai.h2o.mojos.deploy.common.transform.SampleRequestBuilder; | ||
import ai.h2o.mojos.deploy.common.transform.ShapleyLoadOption; | ||
import ai.h2o.mojos.runtime.MojoPipeline; | ||
import ai.h2o.mojos.runtime.api.MojoPipelineService; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ModelsApiControllerTest { | ||
@Mock private SampleRequestBuilder sampleRequestBuilder; | ||
|
||
@BeforeAll | ||
static void setup() throws IOException { | ||
File tmpModel = File.createTempFile("pipeline", ".mojo"); | ||
System.setProperty("mojo.path", tmpModel.getAbsolutePath()); | ||
mockMojoPipeline(tmpModel); | ||
} | ||
|
||
private static void mockMojoPipeline(File tmpModel) { | ||
MojoPipeline mojoPipeline = Mockito.mock(MojoPipeline.class); | ||
MockedStatic<MojoPipelineService> theMock = Mockito.mockStatic(MojoPipelineService.class); | ||
theMock.when(() -> MojoPipelineService | ||
.loadPipeline(new File(tmpModel.getAbsolutePath()))).thenReturn(mojoPipeline); | ||
} | ||
|
||
@Test | ||
void verifyCapabilities_DefaultShapley_ReturnsExpected() { | ||
// Given | ||
List<CapabilityType> expectedCapabilities = Arrays.asList(CapabilityType.SCORE); | ||
|
||
MojoScorer scorer = mock(MojoScorer.class); | ||
when(scorer.getEnabledShapleyTypes()).thenReturn(ShapleyLoadOption.NONE); | ||
|
||
ModelsApiController controller = new ModelsApiController(scorer, sampleRequestBuilder); | ||
|
||
// When | ||
ResponseEntity<List<CapabilityType>> response = controller.getCapabilities(); | ||
|
||
// Then | ||
assertEquals(expectedCapabilities, response.getBody()); | ||
} | ||
|
||
@Test | ||
void verifyCapabilities_AllShapleyEnabled_ReturnsExpected() { | ||
// Given | ||
List<CapabilityType> expectedCapabilities = Arrays.asList( | ||
CapabilityType.SCORE, | ||
CapabilityType.CONTRIBUTION_ORIGINAL, | ||
CapabilityType.CONTRIBUTION_TRANSFORMED); | ||
MojoScorer scorer = mock(MojoScorer.class); | ||
when(scorer.getEnabledShapleyTypes()).thenReturn(ShapleyLoadOption.ALL); | ||
|
||
ModelsApiController controller = new ModelsApiController(scorer, sampleRequestBuilder); | ||
|
||
// When | ||
ResponseEntity<List<CapabilityType>> response = controller.getCapabilities(); | ||
|
||
// Then | ||
assertEquals(expectedCapabilities, response.getBody()); | ||
} | ||
|
||
@Test | ||
void verifyCapabilities_OriginalShapleyEnabled_ReturnsExpected() { | ||
// Given | ||
List<CapabilityType> expectedCapabilities = Arrays.asList( | ||
CapabilityType.SCORE, | ||
CapabilityType.CONTRIBUTION_ORIGINAL); | ||
MojoScorer scorer = mock(MojoScorer.class); | ||
when(scorer.getEnabledShapleyTypes()).thenReturn(ShapleyLoadOption.ORIGINAL); | ||
|
||
ModelsApiController controller = new ModelsApiController(scorer, sampleRequestBuilder); | ||
|
||
// When | ||
ResponseEntity<List<CapabilityType>> response = controller.getCapabilities(); | ||
|
||
// Then | ||
assertEquals(expectedCapabilities, response.getBody()); | ||
} | ||
|
||
@Test | ||
void verifyCapabilities_TransformedShapleyEnabled_ReturnsExpected() { | ||
// Given | ||
List<CapabilityType> expectedCapabilities = Arrays.asList( | ||
CapabilityType.SCORE, | ||
CapabilityType.CONTRIBUTION_TRANSFORMED); | ||
MojoScorer scorer = mock(MojoScorer.class); | ||
when(scorer.getEnabledShapleyTypes()).thenReturn(ShapleyLoadOption.TRANSFORMED); | ||
|
||
ModelsApiController controller = new ModelsApiController(scorer, sampleRequestBuilder); | ||
|
||
// When | ||
ResponseEntity<List<CapabilityType>> response = controller.getCapabilities(); | ||
|
||
// Then | ||
assertEquals(expectedCapabilities, response.getBody()); | ||
} | ||
} |