TSK-1109 Make WorkbasketResource attributes private and convert tests to AssertJ

This commit is contained in:
Sofie Hofmann 2020-02-12 14:45:00 +01:00
parent d7362d40cd
commit fc812cb9ea
6 changed files with 83 additions and 85 deletions

View File

@ -183,7 +183,7 @@ public class WorkbasketController extends AbstractPagingController {
ConcurrencyException {
LOGGER.debug("Entry to updateWorkbasket(workbasketId= {})", workbasketId);
ResponseEntity<WorkbasketResource> result;
if (workbasketId.equals(workbasketResource.workbasketId)) {
if (workbasketId.equals(workbasketResource.getWorkbasketId())) {
Workbasket workbasket = workbasketResourceAssembler.toModel(workbasketResource);
workbasket = workbasketService.updateWorkbasket(workbasket);
result = ResponseEntity.ok(workbasketResourceAssembler.toResource(workbasket));

View File

@ -55,15 +55,16 @@ public class WorkbasketDefinitionResourceAssembler {
public Workbasket toModel(WorkbasketResource wbResource) {
WorkbasketImpl workbasket =
(WorkbasketImpl) workbasketService.newWorkbasket(wbResource.key, wbResource.domain);
(WorkbasketImpl)
workbasketService.newWorkbasket(wbResource.getKey(), wbResource.getDomain());
BeanUtils.copyProperties(wbResource, workbasket);
workbasket.setId(wbResource.workbasketId);
workbasket.setId(wbResource.getWorkbasketId());
if (wbResource.getModified() != null) {
workbasket.setModified(Instant.parse(wbResource.modified));
workbasket.setModified(Instant.parse(wbResource.getModified()));
}
if (wbResource.getCreated() != null) {
workbasket.setCreated(Instant.parse(wbResource.created));
workbasket.setCreated(Instant.parse(wbResource.getCreated()));
}
return workbasket;
}

View File

@ -9,23 +9,23 @@ import pro.taskana.workbasket.api.WorkbasketType;
/** Resource class for {@link Workbasket}. */
public class WorkbasketResource extends ResourceSupport {
public String workbasketId;
@NotNull public String key;
@NotNull public String name;
@NotNull public String domain;
@NotNull public WorkbasketType type;
public String created; // ISO-8601
public String modified; // ISO-8601
public String description;
public String owner;
public String custom1;
public String custom2;
public String custom3;
public String custom4;
public String orgLevel1;
public String orgLevel2;
public String orgLevel3;
public String orgLevel4;
private String workbasketId;
@NotNull private String key;
@NotNull private String name;
@NotNull private String domain;
@NotNull private WorkbasketType type;
private String created; // ISO-8601
private String modified; // ISO-8601
private String description;
private String owner;
private String custom1;
private String custom2;
private String custom3;
private String custom4;
private String orgLevel1;
private String orgLevel2;
private String orgLevel3;
private String orgLevel4;
public WorkbasketResource() {}

View File

@ -45,16 +45,17 @@ public class WorkbasketResourceAssembler
}
public Workbasket toModel(WorkbasketResource wbResource) {
WorkbasketImpl workbasket =
(WorkbasketImpl) workbasketService.newWorkbasket(wbResource.key, wbResource.domain);
String wbKey = wbResource.getKey();
String wbDomain = wbResource.getDomain();
WorkbasketImpl workbasket = (WorkbasketImpl) workbasketService.newWorkbasket(wbKey, wbDomain);
BeanUtils.copyProperties(wbResource, workbasket);
workbasket.setId(wbResource.workbasketId);
if (wbResource.modified != null) {
workbasket.setModified(Instant.parse(wbResource.modified));
workbasket.setId(wbResource.getWorkbasketId());
if (wbResource.getModified() != null) {
workbasket.setModified(Instant.parse(wbResource.getModified()));
}
if (wbResource.created != null) {
workbasket.setCreated(Instant.parse(wbResource.created));
if (wbResource.getCreated() != null) {
workbasket.setCreated(Instant.parse(wbResource.getCreated()));
}
return workbasket;
}

View File

@ -1,12 +1,9 @@
package pro.taskana.rest;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -20,7 +17,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -75,9 +71,9 @@ class WorkbasketDefinitionControllerIntTest {
ResponseEntity<List<WorkbasketDefinitionResource>> response =
executeExportRequestForDomain("DOMAIN_A");
assertNotNull(response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
assertThat(response.getBody().get(0), instanceOf(WorkbasketDefinitionResource.class));
assertThat(response.getBody()).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody().get(0)).isInstanceOf(WorkbasketDefinitionResource.class);
boolean allAuthorizationsAreEmpty = true;
boolean allDistributionTargetsAreEmpty = true;
@ -93,15 +89,15 @@ class WorkbasketDefinitionControllerIntTest {
break;
}
}
assertFalse(allDistributionTargetsAreEmpty);
assertFalse(allAuthorizationsAreEmpty);
assertThat(allDistributionTargetsAreEmpty).isFalse();
assertThat(allAuthorizationsAreEmpty).isFalse();
}
@Test
void testExportWorkbasketsFromWrongDomain() {
ResponseEntity<List<WorkbasketDefinitionResource>> response =
executeExportRequestForDomain("wrongDomain");
assertEquals(0, response.getBody().size());
assertThat(response.getBody()).isEmpty();
}
@Test
@ -130,7 +126,7 @@ class WorkbasketDefinitionControllerIntTest {
WorkbasketDefinitionResource w = wbList.get(0);
w.setDistributionTargets(new HashSet<>());
String letMeBeYourDistributionTarget = w.getWorkbasket().workbasketId;
String letMeBeYourDistributionTarget = w.getWorkbasket().getWorkbasketId();
WorkbasketDefinitionResource w2 = wbList.get(1);
w2.setDistributionTargets(Collections.singleton(letMeBeYourDistributionTarget));
expectStatusWhenExecutingImportRequestOfWorkbaskets(HttpStatus.NO_CONTENT, w, w2);
@ -168,7 +164,7 @@ class WorkbasketDefinitionControllerIntTest {
expectStatusWhenExecutingImportRequestOfWorkbaskets(HttpStatus.BAD_REQUEST, w);
fail("Expected http-Status 400");
} catch (HttpClientErrorException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
}
w.getWorkbasket().setKey("anotherNewKey");
@ -176,7 +172,7 @@ class WorkbasketDefinitionControllerIntTest {
expectStatusWhenExecutingImportRequestOfWorkbaskets(HttpStatus.BAD_REQUEST, w);
fail("Expected http-Status 400");
} catch (HttpClientErrorException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
}
}
@ -187,7 +183,7 @@ class WorkbasketDefinitionControllerIntTest {
expectStatusWhenExecutingImportRequestOfWorkbaskets(HttpStatus.CONFLICT, w, w);
fail("Expected http-Status 409");
} catch (HttpClientErrorException e) {
assertEquals(HttpStatus.CONFLICT, e.getStatusCode());
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
}
}
@ -202,7 +198,7 @@ class WorkbasketDefinitionControllerIntTest {
// breaks the logic but not the script- should we really allow this case?
WorkbasketDefinitionResource theDestroyer = wbList.get(2);
theDestroyer.setDistributionTargets(
Collections.singleton(differentLogicalId.getWorkbasket().workbasketId));
Collections.singleton(differentLogicalId.getWorkbasket().getWorkbasketId()));
expectStatusWhenExecutingImportRequestOfWorkbaskets(
HttpStatus.NO_CONTENT, w, differentLogicalId, theDestroyer);
@ -215,11 +211,11 @@ class WorkbasketDefinitionControllerIntTest {
String w1String = workbasketToString(w);
w.getWorkbasket().setKey("new Key for this WB");
String w2String = workbasketToString(w);
Assertions.assertThrows(
HttpClientErrorException.class,
assertThatThrownBy(
() ->
expectStatusWhenExecutingImportRequestOfWorkbaskets(
HttpStatus.CONFLICT, Arrays.asList(w1String, w2String)));
HttpStatus.CONFLICT, Arrays.asList(w1String, w2String)))
.isInstanceOf(HttpClientErrorException.class);
}
private void changeWorkbasketIdOrKey(
@ -246,7 +242,7 @@ class WorkbasketDefinitionControllerIntTest {
private void expectStatusWhenExecutingImportRequestOfWorkbaskets(
HttpStatus expectedStatus, WorkbasketDefinitionResource... workbaskets) throws IOException {
List<String> workbasketStrings =
Arrays.stream(workbaskets).map(wb -> workbasketToString(wb)).collect(Collectors.toList());
Arrays.stream(workbaskets).map(this::workbasketToString).collect(Collectors.toList());
expectStatusWhenExecutingImportRequestOfWorkbaskets(expectedStatus, workbasketStrings);
}
@ -267,7 +263,7 @@ class WorkbasketDefinitionControllerIntTest {
ResponseEntity<Void> responseImport =
template.postForEntity(serverUrl, requestEntity, Void.class);
assertEquals(expectedStatus, responseImport.getStatusCode());
assertThat(responseImport.getStatusCode()).isEqualTo(expectedStatus);
}
private String workbasketToString(WorkbasketDefinitionResource workbasketDefinitionResource) {

View File

@ -1,7 +1,8 @@
package pro.taskana.rest.resource;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Instant;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@ -99,40 +100,39 @@ class WorkbasketResourceAssemblerTest {
}
private void verifyLinks(WorkbasketResource workbasket) {
Assert.assertEquals(5, workbasket.getLinks().size());
Assert.assertEquals(
Mapping.URL_WORKBASKET_ID.replaceAll("\\{.*}", workbasket.getWorkbasketId()),
workbasket.getLink("self").getHref());
Assert.assertEquals(
Mapping.URL_WORKBASKET_ID_DISTRIBUTION.replaceAll("\\{.*}", workbasket.getWorkbasketId()),
workbasket.getLink("distributionTargets").getHref());
Assert.assertEquals(Mapping.URL_WORKBASKET, workbasket.getLink("allWorkbaskets").getHref());
Assert.assertEquals(
Mapping.URL_WORKBASKET_DISTRIBUTION_ID.replaceAll("\\{.*}", workbasket.getWorkbasketId()),
workbasket.getLink("removeDistributionTargets").getHref());
assertThat(workbasket.getLinks()).hasSize(5);
assertThat(workbasket.getLink("self").getHref())
.isEqualTo(Mapping.URL_WORKBASKET_ID.replaceAll("\\{.*}", workbasket.getWorkbasketId()));
assertThat(workbasket.getLink("distributionTargets").getHref())
.isEqualTo(
Mapping.URL_WORKBASKET_ID_DISTRIBUTION.replaceAll(
"\\{.*}", workbasket.getWorkbasketId()));
assertThat(workbasket.getLink("allWorkbaskets").getHref()).isEqualTo(Mapping.URL_WORKBASKET);
assertThat(workbasket.getLink("removeDistributionTargets").getHref())
.isEqualTo(
Mapping.URL_WORKBASKET_DISTRIBUTION_ID.replaceAll(
"\\{.*}", workbasket.getWorkbasketId()));
}
private void testEquality(Workbasket workbasket, WorkbasketResource workbasketResource) {
Assert.assertEquals(workbasket.getId(), workbasketResource.workbasketId);
Assert.assertEquals(workbasket.getKey(), workbasketResource.key);
Assert.assertEquals(
workbasket.getCreated() == null ? null : workbasket.getCreated().toString(),
workbasketResource.created);
Assert.assertEquals(
workbasket.getModified() == null ? null : workbasket.getModified().toString(),
workbasketResource.modified);
Assert.assertEquals(workbasket.getName(), workbasketResource.name);
Assert.assertEquals(workbasket.getDescription(), workbasketResource.description);
Assert.assertEquals(workbasket.getOwner(), workbasketResource.owner);
Assert.assertEquals(workbasket.getDomain(), workbasketResource.domain);
Assert.assertEquals(workbasket.getType(), workbasketResource.type);
Assert.assertEquals(workbasket.getCustom1(), workbasketResource.custom1);
Assert.assertEquals(workbasket.getCustom2(), workbasketResource.custom2);
Assert.assertEquals(workbasket.getCustom3(), workbasketResource.custom3);
Assert.assertEquals(workbasket.getCustom4(), workbasketResource.custom4);
Assert.assertEquals(workbasket.getOrgLevel1(), workbasketResource.orgLevel1);
Assert.assertEquals(workbasket.getOrgLevel2(), workbasketResource.orgLevel2);
Assert.assertEquals(workbasket.getOrgLevel3(), workbasketResource.orgLevel3);
Assert.assertEquals(workbasket.getOrgLevel4(), workbasketResource.orgLevel4);
assertThat(workbasketResource.getWorkbasketId()).isEqualTo(workbasket.getId());
assertThat(workbasketResource.getKey()).isEqualTo(workbasket.getKey());
assertThat(workbasketResource.getCreated())
.isEqualTo(workbasket.getCreated() == null ? null : workbasket.getCreated().toString());
assertThat(workbasketResource.getModified())
.isEqualTo(workbasket.getModified() == null ? null : workbasket.getModified().toString());
assertThat(workbasketResource.getName()).isEqualTo(workbasket.getName());
assertThat(workbasketResource.getDescription()).isEqualTo(workbasket.getDescription());
assertThat(workbasketResource.getOwner()).isEqualTo(workbasket.getOwner());
assertThat(workbasketResource.getDomain()).isEqualTo(workbasket.getDomain());
assertThat(workbasketResource.getType()).isEqualTo(workbasket.getType());
assertThat(workbasketResource.getCustom1()).isEqualTo(workbasket.getCustom1());
assertThat(workbasketResource.getCustom2()).isEqualTo(workbasket.getCustom2());
assertThat(workbasketResource.getCustom3()).isEqualTo(workbasket.getCustom3());
assertThat(workbasketResource.getCustom4()).isEqualTo(workbasket.getCustom4());
assertThat(workbasketResource.getOrgLevel1()).isEqualTo(workbasket.getOrgLevel1());
assertThat(workbasketResource.getOrgLevel2()).isEqualTo(workbasket.getOrgLevel2());
assertThat(workbasketResource.getOrgLevel3()).isEqualTo(workbasket.getOrgLevel3());
assertThat(workbasketResource.getOrgLevel4()).isEqualTo(workbasket.getOrgLevel4());
}
}