Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

SUBMARINE-584. Add unit test for ExperimentRestApi.java #375

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions submarine-server/server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
aeioulisa marked this conversation as resolved.
Show resolved Hide resolved
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,44 @@
@Path(RestConstants.V1 + "/" + RestConstants.EXPERIMENT)
@Produces({MediaType.APPLICATION_JSON + "; " + RestConstants.CHARSET_UTF8})
public class ExperimentRestApi {
private final ExperimentManager experimentManager = ExperimentManager.getInstance();
private ExperimentManager experimentManager = ExperimentManager.getInstance();

public void setExperimentManager(ExperimentManager experimentManager) {
aeioulisa marked this conversation as resolved.
Show resolved Hide resolved
this.experimentManager = experimentManager;
}

/**
* Return the Pong message for test the connectivity
*
* @return Pong message
*/
@GET
@Path(RestConstants.PING)
@Consumes(MediaType.APPLICATION_JSON)
@Operation(summary = "Ping submarine server",
tags = {"experiment"},
description = "Return the Pong message for test the connectivity",
responses = {
@ApiResponse(responseCode = "200", description = "successful operation",
content = @Content(schema = @Schema(implementation = String.class)))})
tags = {"experiment"},
description = "Return the Pong message for test the connectivity",
responses = {
@ApiResponse(responseCode = "200", description = "successful operation",
content = @Content(schema = @Schema(implementation = String.class)))})
public Response ping() {
return new JsonResponse.Builder<String>(Response.Status.OK)
.success(true).result("Pong").build();
}

/**
* Returns the contents of {@link Experiment} that submitted by user.
*
* @param spec spec
* @return the contents of experiment
*/
@POST
@Consumes({RestConstants.MEDIA_TYPE_YAML, MediaType.APPLICATION_JSON})
@Operation(summary = "Create an experiment",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class)))})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class)))})
public Response createExperiment(ExperimentSpec spec) {
try {
Experiment experiment = experimentManager.createExperiment(spec);
Expand All @@ -93,14 +100,15 @@ public Response createExperiment(ExperimentSpec spec) {

/**
* List all experiment for the user
*
* @return experiment list
*/
@GET
@Operation(summary = "List experiments",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class)))})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class)))})
public Response listExperiments(@QueryParam("status") String status) {
try {
List<Experiment> experimentList = experimentManager.listExperimentsByStatus(status);
Expand All @@ -113,17 +121,18 @@ public Response listExperiments(@QueryParam("status") String status) {

/**
* Returns the experiment detailed info by specified experiment id
*
* @param id experiment id
* @return the detailed info of experiment
*/
@GET
@Path("/{id}")
@Operation(summary = "Get the experiment's detailed info by id",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
public Response getExperiment(@PathParam(RestConstants.ID) String id) {
try {
Experiment experiment = experimentManager.getExperiment(id);
Expand All @@ -138,11 +147,11 @@ public Response getExperiment(@PathParam(RestConstants.ID) String id) {
@Path("/{id}")
@Consumes({RestConstants.MEDIA_TYPE_YAML, MediaType.APPLICATION_JSON})
@Operation(summary = "Update the experiment in the submarine server with spec",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
public Response patchExperiment(@PathParam(RestConstants.ID) String id, ExperimentSpec spec) {
try {
Experiment experiment = experimentManager.patchExperiment(id, spec);
Expand All @@ -155,17 +164,18 @@ public Response patchExperiment(@PathParam(RestConstants.ID) String id, Experime

/**
* Returns the experiment that deleted
*
* @param id experiment id
* @return the detailed info about deleted experiment
*/
@DELETE
@Path("/{id}")
@Operation(summary = "Delete the experiment",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
public Response deleteExperiment(@PathParam(RestConstants.ID) String id) {
try {
Experiment experiment = experimentManager.deleteExperiment(id);
Expand All @@ -179,10 +189,10 @@ public Response deleteExperiment(@PathParam(RestConstants.ID) String id) {
@GET
@Path("/logs")
@Operation(summary = "List experiment's log",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class)))})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class)))})
public Response listLog(@QueryParam("status") String status) {
try {
List<ExperimentLog> experimentLogList = experimentManager.listExperimentLogsByStatus(status);
Expand All @@ -197,11 +207,11 @@ public Response listLog(@QueryParam("status") String status) {
@GET
@Path("/logs/{id}")
@Operation(summary = "Log experiment by id",
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
tags = {"experiment"},
responses = {
@ApiResponse(description = "successful operation", content = @Content(
schema = @Schema(implementation = JsonResponse.class))),
@ApiResponse(responseCode = "404", description = "Experiment not found")})
public Response getLog(@PathParam(RestConstants.ID) String id) {
try {
ExperimentLog experimentLog = experimentManager.getExperimentLog(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.submarine.server.rest;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonArray;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.submarine.server.SubmarineServer;
import org.apache.submarine.server.api.experiment.Experiment;
import org.apache.submarine.server.api.experiment.ExperimentId;
import org.apache.submarine.server.api.experiment.ExperimentLog;
import org.apache.submarine.server.api.spec.EnvironmentSpec;
import org.apache.submarine.server.api.spec.ExperimentSpec;
import org.apache.submarine.server.experiment.ExperimentManager;
import org.apache.submarine.server.gson.ExperimentIdDeserializer;
import org.apache.submarine.server.gson.ExperimentIdSerializer;
import org.junit.Test;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.After;


aeioulisa marked this conversation as resolved.
Show resolved Hide resolved
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ExperimentRestApiTest {

private static ExperimentRestApi experimentRestApi;
private static ExperimentManager mockExperimentManager;
private final AtomicInteger experimentCounter = new AtomicInteger(0);
private Experiment experiment;

private static GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeAdapter(ExperimentId.class, new ExperimentIdSerializer())
.registerTypeAdapter(ExperimentId.class, new ExperimentIdDeserializer());
private static Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

@BeforeClass
public static void init() {
mockExperimentManager = mock(ExperimentManager.class);
experimentRestApi = new ExperimentRestApi();
experimentRestApi.setExperimentManager(mockExperimentManager);
}

@Before
public void createAndUpdateExperiment() {
experiment = new Experiment();
experiment.setAcceptedTime("2020-08-06T08:39:22.000+08:00");
experiment.setCreatedTime("2020-08-06T08:39:22.000+08:00");
experiment.setRunningTime("2020-08-06T08:39:23.000+08:00");
experiment.setFinishedTime("2020-08-06T08:41:07.000+08:00");
experiment.setUid("0b617cea-81fa-40b6-bbff-da3e400d2be4");
experiment.setName("tf-example");
experiment.setStatus("Succeeded");
experiment.setExperimentId(ExperimentId.newInstance(SubmarineServer.getServerTimeStamp(),
experimentCounter.incrementAndGet()));
ExperimentSpec experimentSpec = new ExperimentSpec();
EnvironmentSpec environmentSpec = new EnvironmentSpec();
environmentSpec.setName("foo");
aeioulisa marked this conversation as resolved.
Show resolved Hide resolved
experimentSpec.setEnvironment(environmentSpec);
experiment.setSpec(experimentSpec);
when(mockExperimentManager.createExperiment(any(ExperimentSpec.class))).thenReturn(experiment);
Response createExperimentResponse = experimentRestApi.createExperiment(experimentSpec);
assertEquals(Response.Status.OK.getStatusCode(), createExperimentResponse.getStatus());
Experiment result = getResultFromResponse(createExperimentResponse, Experiment.class);
assertEquals(experiment.getAcceptedTime(), result.getAcceptedTime());
aeioulisa marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void getExperiment() {
aeioulisa marked this conversation as resolved.
Show resolved Hide resolved
when(mockExperimentManager.getExperiment(any(String.class))).thenReturn(experiment);
Response getExperimentResponse = experimentRestApi.getExperiment("1");
Experiment experiment = getResultFromResponse(getExperimentResponse, Experiment.class);
assertEquals("0b617cea-81fa-40b6-bbff-da3e400d2be4", experiment.getUid());
assertEquals("2020-08-06T08:39:22.000+08:00", experiment.getAcceptedTime());
assertEquals("tf-example", experiment.getName());
assertEquals("2020-08-06T08:39:22.000+08:00", experiment.getCreatedTime());
assertEquals("2020-08-06T08:39:23.000+08:00", experiment.getRunningTime());
}
aeioulisa marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void patchExperiment() {
when(mockExperimentManager.patchExperiment(any(String.class), any(ExperimentSpec.class))).
thenReturn(experiment);
Response patchExperimentResponse = experimentRestApi.patchExperiment("1", new ExperimentSpec());
Experiment experiment = getResultFromResponse(patchExperimentResponse, Experiment.class);
assertEquals("0b617cea-81fa-40b6-bbff-da3e400d2be4", experiment.getUid());
assertEquals("2020-08-06T08:39:22.000+08:00", experiment.getAcceptedTime());
assertEquals("tf-example", experiment.getName());
assertEquals("2020-08-06T08:39:22.000+08:00", experiment.getCreatedTime());
assertEquals("2020-08-06T08:39:23.000+08:00", experiment.getRunningTime());
}

@Test
public void listLog() {
List<ExperimentLog> experimentLogList = new ArrayList<>();
ExperimentLog log1 = new ExperimentLog();
log1.setExperimentId("test id");
experimentLogList.add(log1);
when(mockExperimentManager.listExperimentLogsByStatus(any(String.class))).thenReturn(experimentLogList);
Response listLogResponse = experimentRestApi.listLog("1");
List<ExperimentLog> logs = getResultListFromResponse(listLogResponse, ExperimentLog.class);
assertEquals("test id", logs.get(0).getExperimentId());
}

@Test
public void getLog() {
ExperimentLog log1 = new ExperimentLog();
log1.setExperimentId("test id");
pingsutw marked this conversation as resolved.
Show resolved Hide resolved
when(mockExperimentManager.getExperimentLog(any(String.class))).thenReturn(log1);
Response logResponse = experimentRestApi.getLog("1");
ExperimentLog log = getResultFromResponse(logResponse, ExperimentLog.class);
assertEquals("test id", log.getExperimentId());
}

@Test
public void listExperiment() {
Experiment experiment2 = new Experiment();
experiment2.setUid("0b617cea-81fa-40b6-bbff-da3e400d2be5");
List<Experiment> experimentList = new ArrayList<>();
experimentList.add(experiment);
experimentList.add(experiment2);
when(mockExperimentManager.listExperimentsByStatus(any(String.class))).thenReturn(experimentList);
Response listExperimentResponse = experimentRestApi.listExperiments(Response.Status.OK.toString());
List<Experiment> experiments = getResultListFromResponse(listExperimentResponse, Experiment.class);
assertEquals("0b617cea-81fa-40b6-bbff-da3e400d2be4", experiments.get(0).getUid());
assertEquals("0b617cea-81fa-40b6-bbff-da3e400d2be5", experiments.get(1).getUid());
}

@After
public void deleteExperiment() {
when(mockExperimentManager.deleteExperiment("1")).thenReturn(experiment);
Response deleteExperimentResponse = experimentRestApi.deleteExperiment("1");
Experiment experiment = getResultFromResponse(deleteExperimentResponse, Experiment.class);
assertEquals(this.experiment.getAcceptedTime(), experiment.getAcceptedTime());
}

private <T> T getResultFromResponse(Response response, Class<T> typeT) {
String entity = (String) response.getEntity();
JsonObject object = new JsonParser().parse(entity).getAsJsonObject();
JsonElement result = object.get("result");
return gson.fromJson(result, typeT);
}

private <T> List<T> getResultListFromResponse(Response response, Class<T> typeT) {
String entity = (String) response.getEntity();
JsonObject object = new JsonParser().parse(entity).getAsJsonObject();
JsonElement result = object.get("result");
List<T> list = new ArrayList<T>();
JsonArray arry = result.getAsJsonArray();
for (JsonElement jsonElement : arry) {
list.add(gson.fromJson(jsonElement, typeT));
}
return list;
}
}