FINERACT-1760: Get office by external id

- [x] Add new resource to update
- [x] Integration test
This commit is contained in:
Janos Haber
2023-08-30 14:27:39 +02:00
committed by Arnold Gálovics
parent 59c5ad4500
commit f829eaa9d6
5 changed files with 52 additions and 8 deletions
@@ -22,6 +22,7 @@ import java.util.Map;
import java.util.Optional;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.organisation.office.data.OfficeData;
import org.mapstruct.Mapper;
@Mapper(config = MapstructMapperConfig.class)
@@ -35,4 +36,6 @@ public interface OfficeSwaggerMapper {
Optional.ofNullable(changes).map(c -> c.get("name")).ifPresent(c -> response.name = String.valueOf(c));
return response;
}
OfficesApiResourceSwagger.GetOfficesResponse toGetOfficesResponse(OfficeData officeData);
}
@@ -164,6 +164,25 @@ public class OfficesApiResource {
return toApiJsonSerializer.serialize(settings, office, RESPONSE_DATA_PARAMETERS);
}
@GET
@Path("/external-id/{externalId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve an Office using external id", description = "Example Requests:\n" + "\n" + "offices/external-id/asd123\n"
+ "\n" + "\n" + "offices/external-id/asd123?template=true\n" + "\n" + "\n"
+ "offices/external-id/asd123?fields=id,name,parentName")
public OfficesApiResourceSwagger.GetOfficesResponse retrieveOfficeByExternalId(
@PathParam("externalId") @Parameter(description = "externalId") final String externalId, @Context final UriInfo uriInfo) {
context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
OfficeData office = readPlatformService.retrieveOfficeWithExternalId(ExternalIdFactory.produce(externalId));
if (settings.isTemplate()) {
final Collection<OfficeData> allowedParents = readPlatformService.retrieveAllowedParents(office.getId());
office = OfficeData.appendedTemplate(office, allowedParents);
}
return officeSwaggerMapper.toGetOfficesResponse(office);
}
@PUT
@Path("{officeId}")
@Consumes({ MediaType.APPLICATION_JSON })
@@ -34,10 +34,6 @@ final class OfficesApiResourceSwagger {
@Schema(description = "GetOfficesResponse")
public static final class GetOfficesResponse {
private GetOfficesResponse() {
}
@Schema(example = "1")
public Long id;
@Schema(example = "Head Office")
@@ -50,10 +46,12 @@ final class OfficesApiResourceSwagger {
public LocalDate openingDate;
@Schema(example = ".")
public String hierarchy;
// @Schema(example = "")
// public Long parentId;
// @Schema(example = "")
// public String parentName;
@Schema(example = "dd MMMM yyyy")
public String dateFormat;
@Schema(example = "en")
public String locale;
public Collection<GetOfficesResponse> allowedParents;
}
@Schema(description = "GetOfficesTemplateResponse")
@@ -24,7 +24,9 @@ import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import java.io.IOException;
import java.time.LocalDate;
import java.util.UUID;
import org.apache.fineract.client.models.GetOfficesResponse;
import org.apache.fineract.client.models.PutOfficesOfficeIdResponse;
import org.apache.fineract.integrationtests.common.OfficeDomain;
import org.apache.fineract.integrationtests.common.OfficeHelper;
@@ -79,4 +81,22 @@ public class OfficeIntegrationTest {
Assertions.assertTrue(name.equals(newOffice.getName()));
Assertions.assertArrayEquals(dateArr, newOffice.getOpeningDate());
}
@Test
public void testOfficeModificationAndFetchWithExternalId() throws IOException {
OfficeHelper oh = new OfficeHelper(requestSpec, responseSpec);
String externalId = UUID.randomUUID().toString();
int officeId = oh.createOfficeWithExternalId(externalId, "01 July 2007");
String name = Utils.uniqueRandomStringGenerator("New_Office_", 4);
String date = "02 July 2007";
String[] dateArr = { "2007", "7", "2" };
oh.updateOfficeUsingExternalId(externalId, name, date);
Response<GetOfficesResponse> officeResult = oh.retrieveOfficeByExternalId(externalId);
GetOfficesResponse newOffice = officeResult.body();
Assertions.assertTrue(name.equals(newOffice.getName()));
Assertions.assertTrue(newOffice.getOpeningDate().isEqual(LocalDate.of(2007, 7, 2)));
}
}
@@ -92,6 +92,10 @@ public class OfficeHelper extends IntegrationTest {
new Gson().toJson(map), "resourceId");
}
public Response<GetOfficesResponse> retrieveOfficeByExternalId(String externalId) throws IOException {
return fineract().offices.retrieveOfficeByExternalId(externalId).execute();
}
public Response<PutOfficesOfficeIdResponse> updateOfficeUsingExternalId(String externalId, String name, String openingDate)
throws IOException {
return fineract().offices