/*
* Copyright 2023 Ant Group Co., Ltd.
*
* Licensed 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.secretflow.secretpad.web.controller;
import org.secretflow.secretpad.common.constant.resource.ApiResourceCodeConstants;
import org.secretflow.secretpad.common.errorcode.*;
import org.secretflow.secretpad.common.util.DateTimes;
import org.secretflow.secretpad.common.util.JsonUtils;
import org.secretflow.secretpad.common.util.UserContext;
import org.secretflow.secretpad.persistence.entity.*;
import org.secretflow.secretpad.persistence.model.ResultKind;
import org.secretflow.secretpad.persistence.repository.*;
import org.secretflow.secretpad.service.model.project.*;
import org.secretflow.secretpad.web.utils.FakerUtils;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.secretflow.proto.pipeline.Pipeline;
import org.secretflow.v1alpha1.common.Common;
import org.secretflow.v1alpha1.kusciaapi.DomainDataServiceGrpc;
import org.secretflow.v1alpha1.kusciaapi.Domaindata;
import org.secretflow.v1alpha1.kusciaapi.JobServiceGrpc;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.*;
/**
* Project controller test
*
* @author yansi
* @date 2023/7/24
*/
class ProjectControllerTest extends ControllerTest {
private static final String GRAPH_ID = "graphagdasvacaghyhbvscvyjnba";
private static final String TASK_ID = "task-dabgvasfasdasdas";
private static final String JOB_ID = "op-psiv3-dabgvasfasdasdas";
@MockBean
private ProjectRepository projectRepository;
@MockBean
private NodeRepository nodeRepository;
@MockBean
private InstRepository instRepository;
@MockBean
private ProjectDatatableRepository projectDatatableRepository;
@MockBean
private ProjectNodeRepository projectNodeRepository;
@MockBean
private ProjectGraphRepository graphRepository;
@MockBean
private ProjectJobRepository projectJobRepository;
@MockBean
private ProjectResultRepository projectResultRepository;
@MockBean
private DomainDataServiceGrpc.DomainDataServiceBlockingStub dataStub;
@MockBean
private JobServiceGrpc.JobServiceBlockingStub jobStub;
private ProjectDO buildProjectDO() {
return ProjectDO.builder().projectId(PROJECT_ID).build();
}
private InstDO buildInstDO() {
return InstDO.builder().build();
}
private NodeDO buildNodeDO() {
return NodeDO.builder().nodeId("alice").name("alice").description("alice").auth("alice").type("mpc").build();
}
private ProjectNodeDO buildProjectNodeDO() {
return ProjectNodeDO.builder().upk(new ProjectNodeDO.UPK(PROJECT_ID, "alice")).build();
}
private ProjectDatatableDO buildProjectDatatableDO() {
ProjectDatatableDO.UPK upk = new ProjectDatatableDO.UPK();
upk.setDatatableId("alice-ref1");
upk.setNodeId("alice");
upk.setProjectId(PROJECT_ID);
List<ProjectDatatableDO.TableColumnConfig> tableConfig = new ArrayList<>();
ProjectDatatableDO.TableColumnConfig config = new ProjectDatatableDO.TableColumnConfig();
config.setColType("id1");
config.setColType("string");
config.setAssociateKey(true);
config.setLabelKey(false);
config.setGroupKey(true);
tableConfig.add(config);
return ProjectDatatableDO.builder().upk(upk).tableConfig(tableConfig).build();
}
private ProjectJobDO buildProjectJobDO(boolean isTaskEmpty) {
ProjectJobDO.UPK upk = new ProjectJobDO.UPK();
upk.setProjectId(PROJECT_ID);
upk.setJobId(JOB_ID);
ProjectJobDO projectJobDO = ProjectJobDO.builder().upk(upk).graphId(GRAPH_ID).edges(Collections.emptyList()).build();
Map<String, ProjectTaskDO> projectTaskDOMap = new HashMap<>();
ProjectTaskDO.UPK taskUpk = new ProjectTaskDO.UPK();
taskUpk.setJobId(JOB_ID);
taskUpk.setProjectId(PROJECT_ID);
if (!isTaskEmpty) {
taskUpk.setTaskId(TASK_ID);
projectTaskDOMap.put(TASK_ID, ProjectTaskDO.builder().upk(taskUpk).graphNode(buildProjectGraphNodeDO()).build());
} else {
taskUpk.setTaskId("task-dabgvasfasdasdasssss");
projectTaskDOMap.put("task-dabgvasfasdasdasssss", ProjectTaskDO.builder().upk(taskUpk).graphNode(buildProjectGraphNodeDO()).build());
}
projectJobDO.setTasks(projectTaskDOMap);
projectJobDO.setGmtCreate(DateTimes.utcFromRfc3339("2023-08-02T08:30:15.235+08:00"));
projectJobDO.setGmtModified(DateTimes.utcFromRfc3339("2023-08-02T16:30:15.235+08:00"));
return projectJobDO;
}
private List<ProjectDatatableDO.UPK> buildProjectDatatableDOUPK() {
List<ProjectDatatableDO.UPK> upkList = new ArrayList<>();
ProjectDatatableDO.UPK upk = new ProjectDatatableDO.UPK();
upk.setDatatableId("alice-ref1");
upk.setNodeId("alice");
upk.setProjectId(PROJECT_ID);
upkList.add(upk);
return upkList;
}
private ProjectGraphNodeDO buildProjectGraphNodeDO() {
ProjectGraphNodeDO.UPK upk = new ProjectGraphNodeDO.UPK();
upk.setGraphNodeId("alice");
return ProjectGraphNodeDO.builder().upk(upk).nodeDef(Pipeline.NodeDef.getDefaultInstance()).build();
}
private List<ProjectResultDO> buildProjectResultDOList() {
List<ProjectResultDO> projectResultDOList = new ArrayList<>();
ProjectResultDO.UPK upk = new ProjectResultDO.UPK();
upk.setKind(ResultKind.FedTable);
upk.setNodeId("alice");
upk.setRefId("alice-ref1");
upk.setProjectId(PROJECT_ID);
ProjectResultDO projectResultDO = ProjectResultDO.builder().upk(upk).taskId(TASK_ID).jobId("op-psiv3-dabgvasfasdasdas").build();
projectResultDO.setGmtCreate(DateTimes.utcFromRfc3339("2023-08-02T08:30:15.235+08:00"));
projectResultDO.setGmtModified(DateTimes.utcFromRfc3339("2023-08-02T16:30:15.235+08:00"));
projectResultDOList.add(projectResultDO);
return projectResultDOList;
}
private Domaindata.QueryDomainDataResponse buildQueryDomainDataResponse(Integer code) {
return Domaindata.QueryDomainDataResponse.newBuilder().setStatus(Common.Status.newBuilder().setCode(code).build()).setData(
Domaindata.DomainData.newBuilder().setDomainId("alice").setDomaindataId("alice-ref1").setType("2").setRelativeUri("dmds://psi_125676513").
setDatasourceId("alice-datasource-ref1").addColumns(Common.DataColumn.newBuilder().setName("id1").setType("string")).build()
).build();
}
private Domaindata.QueryDomainDataResponse buildEmptyQueryDomainDataResponse(Integer code) {
return Domaindata.QueryDomainDataResponse.newBuilder().setStatus(Common.Status.newBuilder().setCode(code).build()).build();
}
private Domaindata.BatchQueryDomainDataResponse buildBatchQueryDomainDataResponse(Integer code) {
return Domaindata.BatchQueryDomainDataResponse.newBuilder().setStatus(Common.Status.newBuilder().setCode(code).build()).setData(
Domaindata.DomainDataList.newBuilder().addDomaindataList(Domaindata.DomainData.newBuilder().setDomainId("alice").
setDomaindataId("alice-ref1").setType("2").setRelativeUri("dmds://psi_125676513").build()).build()
).build();
}
@Test
void createProject(