Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTSRD-3921.Create endpoint to save name and SraId #1725

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmcts.reform.professionalapi;


import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import net.thucydides.core.annotations.WithTag;
import net.thucydides.core.annotations.WithTags;
Expand All @@ -14,12 +15,17 @@
import uk.gov.hmcts.reform.professionalapi.util.FeatureToggleConditionExtension;
import uk.gov.hmcts.reform.professionalapi.util.ToggleEnable;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import static uk.gov.hmcts.reform.professionalapi.client.ProfessionalApiClient.createOrganisationRequestForV2;
Expand Down Expand Up @@ -204,4 +210,235 @@ public void findOrganisationPbaWithoutEmailByExternalUserShouldBeBadRequestV2()
log.info("findOrganisationPbaWithoutEmailByExternalUserShouldBeBadRequest :: END");
}

@Test
void updateOrganisationNameSraShouldReturnSuccess() {
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
log.info("updateOrganisationNameShouldReturnSuccess :: STARTED");

String updateName = randomAlphabetic(10);
String updateSraId = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("name",updateName);
organisationNameSraUpdate.put("sraId",updateSraId);

//call endpoint to update name as 'updatedname'
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(204);

//retrieve saved organisation by id
var orgResponse = professionalApiClient.retrieveOrganisationDetails(extActiveOrgId, hmctsAdmin, OK);
assertThat(orgResponse).isNotNull();

final Object orgName = orgResponse.get("name");
assertThat(orgName).isNotNull().isEqualTo(updateName);
final Object sraId = orgResponse.get("sraId");
assertThat(sraId).isNotNull().isEqualTo(updateSraId);

LocalDateTime updatedDate = LocalDateTime.parse(orgResponse.get("lastUpdated").toString());
assertThat(updatedDate.toLocalDate()).isEqualTo(LocalDate.now());

log.info("updateOrganisationNameShouldReturnSuccess :: END");

}

@Test
void updateOrganisationSraShouldReturnSuccess() {
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
log.info("updateOrganisationNameShouldReturnSuccess :: STARTED");

String updateSraId = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("sraId",updateSraId);

//call endpoint to update name as 'updatedname'
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(204);

//retrieve saved organisation by id
var orgResponse = professionalApiClient.retrieveOrganisationDetails(extActiveOrgId, hmctsAdmin, OK);
assertThat(orgResponse).isNotNull();

final Object sraId = orgResponse.get("sraId");
assertThat(sraId).isNotNull().isEqualTo(updateSraId);

LocalDateTime updatedDate = LocalDateTime.parse(orgResponse.get("lastUpdated").toString());
assertThat(updatedDate.toLocalDate()).isEqualTo(LocalDate.now());

log.info("updateOrganisationNameShouldReturnSuccess :: END");

}

@Test
void updateOrganisationNameOnlyShouldReturnSuccess() {
log.info("updateOrganisationNameOnlyShouldReturnSuccess :: STARTED");
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));

String updateName = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("name",updateName);

//call endpoint to update name
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(204);

//retrieve saved organisation by id
var orgResponse = professionalApiClient.retrieveOrganisationDetails(extActiveOrgId, hmctsAdmin, OK);
assertThat(orgResponse).isNotNull();

final Object orgName = orgResponse.get("name");
assertThat(orgName).isNotNull().isEqualTo(updateName);

LocalDateTime updatedDate = LocalDateTime.parse(orgResponse.get("lastUpdated").toString());
assertThat(updatedDate.toLocalDate()).isEqualTo(LocalDate.now());

log.info("updateOrganisationNameOnlyShouldReturnSuccess :: END");

}

@Test
void updateOrganisationShouldReturnFailureIfNoNameValueInMapButKeyExists() {
log.info("updateOrganisationShouldReturnFailureIfNoNameValueInMapButKeyExists :: STARTED");
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
String updateSraId = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("sraId",updateSraId);
organisationNameSraUpdate.put("name","");
//call endpoint to update
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(400);
assertThat(orgUpdatedResponse.getBody().prettyPrint()).contains("Organisation name cannot be empty");

log.info("updateOrganisationShouldReturnFailureIfNoNameValueInMapButKeyExists :: END");
}

@Test
void updateOrganisationShouldReturnSuccessIfNoNameAddedInMap() {
log.info("updateOrganisationShouldReturnSuccessIfNoNameAddedInMap :: STARTED");
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
String updateSraId = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("sraId",updateSraId);

//call endpoint to update
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(204);

//retrieve saved organisation by id
var orgResponse = professionalApiClient.retrieveOrganisationDetails(extActiveOrgId, hmctsAdmin, OK);
assertThat(orgResponse).isNotNull();

final Object orgName = orgResponse.get("sraId");
assertThat(orgName).isNotNull().isEqualTo(updateSraId);

LocalDateTime updatedDate = LocalDateTime.parse(orgResponse.get("lastUpdated").toString());
assertThat(updatedDate.toLocalDate()).isEqualTo(LocalDate.now());

log.info("updateOrganisationShouldReturnSuccessIfNoNameAddedInMap :: END");
}


@Test
void updateOrganisationSraIdShouldReturnFailureIfTooLong() {
log.info("updateOrganisationNameSraIdShouldReturnFailureIfTooLong :: STARTED");
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
String updateSraId = randomAlphabetic(258);
Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("sraId",updateSraId);
//call endpoint to update
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(400);
assertThat(orgUpdatedResponse.getBody().prettyPrint())
.contains("Organisation sraId cannot be more than 255 characters");

log.info("updateOrganisationNameSraIdShouldReturnFailureIfTooLong :: END");
}

@Test
void updateOrganisationNameShouldReturnFailureIfTooLong() {
log.info("updateOrganisationNameSraIdShouldReturnFailureIfTooLong :: STARTED");
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
String updateName = randomAlphabetic(258);
Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("name",updateName);
//call endpoint to update
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(pomBearerToken));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(400);
assertThat(orgUpdatedResponse.getBody().prettyPrint())
.contains("Organisation name cannot be more than 255 characters");

log.info("updateOrganisationNameSraIdShouldReturnFailureIfTooLong :: END");
}

@Test
void updateOrganisationNameSraIdShouldReturnFailureIfNoOrgId() {
log.info("updateOrganisationNameSraIdShouldReturnFailureIfNoOrgId :: STARTED");

setUpOrgTestData();
setUpUserBearerTokens(List.of(puiOrgManager));
log.info("updateOrganisationNameShouldReturnSuccess :: STARTED");

String updateName = randomAlphabetic(10);
String updateSraId = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("name",updateName);
organisationNameSraUpdate.put("sraId",updateSraId);

//call endpoint to update with no org id does not create token invalid authentication 401
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(null));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(401);
log.info("updateOrganisationNameSraIdShouldReturnFailureIfNoOrgId :: END");
}


@Test
void updateOrganisationSraIdShouldReturnFailureIfUnAuthorised() {
log.info("updateOrganisationSraIdShouldReturnFailureIfUnAuthorised :: STARTED");
setUpOrgTestData();
setUpUserBearerTokens(List.of(puiUserManager));

String updateName = randomAlphabetic(10);
String updateSraId = randomAlphabetic(10);

Map<String,String> organisationNameSraUpdate = new HashMap<>();
organisationNameSraUpdate.put("name",updateName);
organisationNameSraUpdate.put("sraId",updateSraId);

//call endpoint to update with user manager role not allowed
Response orgUpdatedResponse = professionalApiClient.updatesOrganisationDetails(organisationNameSraUpdate,
professionalApiClient.getMultipleAuthHeaders(puiUserManager));
assertNotNull(orgUpdatedResponse);
assertThat(orgUpdatedResponse.statusCode()).isEqualTo(401);

log.info("updateOrganisationSraIdShouldReturnFailureIfUnAuthorised :: END");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1752,4 +1752,16 @@ public void deleteMultipleAddressesOfOrganisation(List<DeleteMultipleAddressRequ
log.info("{}:: Delete Multiple Addresses of an organisation status response: {}",
loggingComponentName, response.getStatusCode());
}

public Response updatesOrganisationDetails(Map<String,String> organisationNameSraUpdate,
RequestSpecification requestSpecification) {

Response response = requestSpecification
.body(organisationNameSraUpdate)
.put("/refdata/external/v2/organisations/orgDetails")
.andReturn();

return response;
}

}
Loading