Skip to content

Commit

Permalink
Merge pull request #136 from Breeding-Insight/feature/BI-1086.1
Browse files Browse the repository at this point in the history
[BI-1086] - Germplasm lists table
  • Loading branch information
HMS17 authored Jan 24, 2022
2 parents 8ecef32 + 953da77 commit 7d76eb3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import io.micronaut.security.rules.SecurityRule;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.brapi.client.v2.JSON;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.v2.model.core.BrAPIServerInfo;
import org.brapi.v2.model.core.response.BrAPIListsListResponse;
import org.brapi.v2.model.core.response.BrAPIServerInfoResponse;
import org.breedinginsight.api.auth.AuthenticatedUser;
import org.breedinginsight.api.auth.ProgramSecured;
import org.breedinginsight.api.auth.ProgramSecuredRoleGroup;
import org.breedinginsight.api.auth.SecurityService;
import org.breedinginsight.brapi.v1.controller.BrapiVersion;
import org.breedinginsight.brapi.v2.services.BrAPIGermplasmService;
import org.breedinginsight.model.ProgramBrAPIEndpoints;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.exceptions.DoesNotExistException;
Expand All @@ -51,11 +55,13 @@ public class BrAPIV2Controller {

private final SecurityService securityService;
private final ProgramService programService;
private final BrAPIGermplasmService germplasmService;

@Inject
public BrAPIV2Controller(SecurityService securityService, ProgramService programService) {
public BrAPIV2Controller(SecurityService securityService, ProgramService programService, BrAPIGermplasmService germplasmService) {
this.securityService = securityService;
this.programService = programService;
this.germplasmService = germplasmService;
}


Expand All @@ -72,6 +78,17 @@ public BrAPIServerInfoResponse serverinfo() {
return new BrAPIServerInfoResponse().result(serverInfo);
}

//@Get(BrapiVersion.BRAPI_V2 + "/lists")
@Get("/${micronaut.bi.api.version}/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/lists")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL})
public HttpResponse getLists(@PathVariable("programId") UUID programId, HttpRequest<String> request) throws DoesNotExistException, ApiException {
BrAPIListsListResponse response = germplasmService.getGermplasmListsByProgramId(programId, request);

String respBody = new JSON().getGson().toJson(response);
return HttpResponse.ok(respBody);
}

@Get("/${micronaut.bi.api.version}/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/{+path}")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import com.google.gson.JsonObject;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.server.exceptions.InternalServerException;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.client.v2.modules.germplasm.GermplasmApi;
import org.brapi.v2.model.core.BrAPIListTypes;
import org.brapi.v2.model.core.response.BrAPIListsListResponse;
import org.brapi.v2.model.germ.BrAPIGermplasm;
import org.brapi.v2.model.germ.request.BrAPIGermplasmSearchRequest;
import org.breedinginsight.brapps.importer.daos.BrAPIListDAO;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.brapi.BrAPIClientType;
import org.breedinginsight.services.brapi.BrAPIProvider;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.utilities.BrAPIDAOUtil;

import javax.inject.Inject;
Expand All @@ -23,10 +30,14 @@ public class BrAPIGermplasmService {
private String referenceSource;
private final String BREEDING_METHOD_ID_KEY = "breedingMethodId";
private final String GERMPLASM_NAME_REGEX = "^(.*\\b) \\[([A-Z]{2,6})-(\\d+)\\]$";
private ProgramService programService;
private BrAPIListDAO brAPIListDAO;

@Inject
public BrAPIGermplasmService(BrAPIProvider brAPIProvider) {
public BrAPIGermplasmService(BrAPIProvider brAPIProvider, BrAPIListDAO brAPIListDAO, ProgramService programService) {
this.brAPIProvider = brAPIProvider;
this.brAPIListDAO = brAPIListDAO;
this.programService = programService;
}

public List<BrAPIGermplasm> getGermplasm(UUID programId) {
Expand Down Expand Up @@ -79,4 +90,35 @@ public List<BrAPIGermplasm> getGermplasm(UUID programId) {

return germplasmList;
}

public BrAPIListsListResponse getGermplasmListsByProgramId(UUID programId, HttpRequest<String> request) throws DoesNotExistException, ApiException {

if (!programService.exists(programId)) {
throw new DoesNotExistException("Program does not exist");
}

Optional<Program> optionalProgram = programService.getById(programId);
if(optionalProgram.isPresent()) {
Program program = optionalProgram.get();
String appendedKey = String.format(" [%s-germplasm]", program.getKey());

BrAPIListsListResponse germplasmLists = brAPIListDAO.getListByTypeAndExternalRef(BrAPIListTypes.GERMPLASM, programId, referenceSource + "/programs", programId);

//Remove key appended to listName for brapi
String listName;
String newListName;
int listLength = germplasmLists.getResult().getData().size();
for (int i=0; i<listLength; i++) {
listName = germplasmLists.getResult().getData().get(i).getListName();
newListName = listName.replace(appendedKey, "");
germplasmLists.getResult().getData().get(i).setListName(newListName);
}

return germplasmLists;
}
else {
throw new DoesNotExistException("Program does not exist");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import org.brapi.client.v2.ApiResponse;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.client.v2.model.queryParams.core.ListQueryParams;
import org.brapi.client.v2.modules.core.ListsApi;
import org.brapi.v2.model.BrAPIResponse;
import org.brapi.v2.model.BrAPIResponseResult;
import org.brapi.v2.model.core.BrAPIListSummary;
import org.brapi.v2.model.core.BrAPIListTypes;
import org.brapi.v2.model.core.request.BrAPIListNewRequest;
import org.brapi.v2.model.core.request.BrAPIListSearchRequest;
import org.brapi.v2.model.core.response.BrAPIListsListResponse;
import org.brapi.v2.model.pheno.BrAPIObservation;
import org.breedinginsight.brapps.importer.model.ImportUpload;
import org.breedinginsight.daos.ProgramDAO;
Expand Down Expand Up @@ -39,6 +42,17 @@ public List<BrAPIListSummary> getListByName(List<String> listNames, UUID program
);
}

public BrAPIListsListResponse getListByTypeAndExternalRef(BrAPIListTypes listType, UUID programId, String externalReferenceSource, UUID externalReferenceId) throws ApiException {
ListQueryParams getParams = new ListQueryParams()
.externalReferenceID(externalReferenceId.toString())
.externalReferenceSource(externalReferenceSource)
.listType(listType);

ListsApi api = new ListsApi(programDAO.getCoreClient(programId));
ApiResponse<BrAPIListsListResponse> apiGetResponse = api.listsGet(getParams);
return apiGetResponse.getBody();
}

public List<BrAPIObservation> createBrAPILists(List<BrAPIListNewRequest> brapiLists, UUID programId, ImportUpload upload) throws ApiException {
ListsApi api = new ListsApi(programDAO.getCoreClient(programId));
// Do manually, it doesn't like List<Object> to List<BrAPIListNewRequest> for some reason
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class GermplasmControllerIntegrationTest extends BrAPITest {
(json, type, context) -> OffsetDateTime.parse(json.getAsString()))
.create();

private final String germplasmListName = "Program List";
private final String germplasmListDesc = "Program List";

@AfterAll
public void finish() { super.stopContainers(); }

Expand Down Expand Up @@ -114,16 +117,16 @@ public void setup() {
// Insert other program germplasm
File otherFile = new File("src/test/resources/files/germplasm_import/minimal_germplasm_import.csv");
Map<String, String> otherListInfo = Map.ofEntries(
Map.entry("germplasmListName", "Program List"),
Map.entry("germplasmListDescription", "Program List")
Map.entry("germplasmListName", germplasmListName),
Map.entry("germplasmListDescription", germplasmListDesc)
);
TestUtils.uploadDataFile(client, otherValidProgram.getId(), germplasmImportId, otherListInfo, otherFile);

// Insert program germplasm
File file = new File("src/test/resources/files/germplasm_import/full_import.csv");
Map<String, String> germplasmListInfo = Map.ofEntries(
Map.entry("germplasmListName", "Program List"),
Map.entry("germplasmListDescription", "Program List")
Map.entry("germplasmListName", germplasmListName),
Map.entry("germplasmListDescription", germplasmListDesc)
);
TestUtils.uploadDataFile(client, validProgram.getId(), germplasmImportId, germplasmListInfo, file);
}
Expand Down Expand Up @@ -173,4 +176,27 @@ public void getPaginatedSuccess() {

assertEquals(1, data.size(), "Wrong number of germplasm were returned");
}

@Test
@SneakyThrows
public void getAllGermplasmListsSuccess() {
Flowable<HttpResponse<String>> call = client.exchange(
GET(String.format("/programs/%s/brapi/v2/lists",validProgram.getId().toString()))
.cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class
);

HttpResponse<String> response = call.blockingFirst();
assertEquals(HttpStatus.OK, response.getStatus());

JsonObject result = JsonParser.parseString(response.body()).getAsJsonObject().getAsJsonObject("result");

JsonArray data = result.getAsJsonArray("data");

assertEquals(1, data.size(), "Wrong number of germplasm lists were returned");
JsonObject exampleGermplasmList = data.get(0).getAsJsonObject();
assertEquals(exampleGermplasmList.get("listName").getAsString(), germplasmListName, "Germplasm list name incorrect");
assertEquals(exampleGermplasmList.get("listDescription").getAsString(), germplasmListDesc, "Germplasm list description incorrect");
assertEquals(exampleGermplasmList.get("listSize").getAsInt(), 3, "Germplasm list displays incorrect total entries ");
}

}

0 comments on commit 7d76eb3

Please sign in to comment.