Skip to content

Commit

Permalink
#2609 - Updated certification tests to reflect the database change to…
Browse files Browse the repository at this point in the history
… move the description into the certification and the name change for certification image URL.
  • Loading branch information
ocielliottc committed Sep 30, 2024
1 parent c944681 commit 351d451
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ void testStartsEmpty() {
@Test
void canCreateCertification() {
createDefaultCertification();
CertificationDTO newCertification = new CertificationDTO("New Certification", "https://badge.url");
CertificationDTO newCertification = new CertificationDTO("New Certification", "Description", "https://badge.url");
Certification createdCertification = create(newCertification);

assertNotNull(createdCertification.getId());
assertEquals(newCertification.getName(), createdCertification.getName());
assertEquals(newCertification.getDescription(), createdCertification.getDescription());
assertEquals(newCertification.getBadgeUrl(), createdCertification.getBadgeUrl());

List<Certification> retrieve = certificationClient.toBlocking().retrieve(HttpRequest.GET("/").basicAuth(ADMIN_ROLE, ADMIN_ROLE), Argument.listOf(Certification.class));
Expand All @@ -61,8 +62,8 @@ void canCreateCertification() {

@Test
void inactiveCerificatesAreHidden() {
createCertification("Certificate");
createCertification("Another Certificate", "https://badge.url", false);
createCertification("Certificate", "Description");
createCertification("Another Certificate", "Description", "https://badge.url", false);

List<Certification> list = certificationClient.toBlocking().retrieve(HttpRequest.GET("/").basicAuth(ADMIN_ROLE, ADMIN_ROLE), Argument.listOf(Certification.class));
assertEquals(List.of("Certificate"), list.stream().map(Certification::getName).toList());
Expand All @@ -75,11 +76,12 @@ void inactiveCerificatesAreHidden() {
void canCreateCertificationWithoutThePermission() {
MemberProfile tim = createASecondDefaultMemberProfile();
createDefaultCertification();
CertificationDTO newCertification = new CertificationDTO("New Certification", "https://badge.url");
CertificationDTO newCertification = new CertificationDTO("New Certification", "Description", "https://badge.url");
Certification createdCertification = create(newCertification, tim.getWorkEmail(), MEMBER_ROLE);

assertNotNull(createdCertification.getId());
assertEquals(newCertification.getName(), createdCertification.getName());
assertEquals(newCertification.getDescription(), createdCertification.getDescription());
assertEquals(newCertification.getBadgeUrl(), createdCertification.getBadgeUrl());

List<Certification> retrieve = certificationClient.toBlocking().retrieve(HttpRequest.GET("/").basicAuth(ADMIN_ROLE, ADMIN_ROLE), Argument.listOf(Certification.class));
Expand All @@ -88,26 +90,28 @@ void canCreateCertificationWithoutThePermission() {

@Test
void listIsOrdered() {
createCertification("Donkey husbandry");
createCertification("Aardvark upkeep");
createCertification("Zebu and you", "https://badge.url");
createCertification("Donkey husbandry", "Description 1");
createCertification("Aardvark upkeep", "Description 2");
createCertification("Zebu and you", "Description 3", "https://badge.url");

List<Certification> list = list();

assertEquals(3, list.size());
assertEquals(List.of("Aardvark upkeep", "Donkey husbandry", "Zebu and you"), list.stream().map(Certification::getName).toList());
assertEquals(List.of("Description 2", "Description 1", "Description 3"), list.stream().map(Certification::getDescription).toList());
}

@Test
void canUpdate() {
Certification certification = createCertification("To update");
Certification certification = createCertification("To update", "To describe");

CertificationDTO update = new CertificationDTO("Updated", "https://badge.url");
CertificationDTO update = new CertificationDTO("Updated", "Description", "https://badge.url");

Certification updated = update(certification.getId(), update);

assertEquals(certification.getId(), updated.getId());
assertEquals(update.getName(), updated.getName());
assertEquals(update.getDescription(), updated.getDescription());
assertEquals(update.getBadgeUrl(), updated.getBadgeUrl());

List<Certification> list = list();
Expand All @@ -117,9 +121,9 @@ void canUpdate() {
@Test
void cannotUpdateWithoutThePermission() {
MemberProfile tim = createASecondDefaultMemberProfile();
Certification certification = createCertification("To update");
Certification certification = createCertification("To update", "Description");

CertificationDTO update = new CertificationDTO("Updated", "https://badge.url");
CertificationDTO update = new CertificationDTO("Updated", "Description", "https://badge.url");

HttpClientResponseException e = assertThrows(HttpClientResponseException.class, () -> update(certification.getId(), update, tim.getWorkEmail(), MEMBER_ROLE));
assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
Expand All @@ -128,26 +132,26 @@ void cannotUpdateWithoutThePermission() {
@Test
void certificationNameRequired() {
HttpClientResponseException exception = assertThrows(HttpClientResponseException.class, () -> create("""
{"badgeUrl":"badge"}"""));
{"description":"description","badgeUrl":"badge"}"""));

assertEquals(HttpStatus.BAD_REQUEST, exception.getResponse().status());
assertTrue(exception.getResponse().getBody(String.class).get().contains("certification.name: must not be null"), "Validation message is as expected");
}

@Test
void certificationNameUniquenessTest() {
Certification original = createCertification("Default");
Certification original = createCertification("Default", "Description");

// Cannot create a certification with the same name
String postBody = """
{"name":"%s"}""".formatted(original.getName());
{"name":"%s","description":"description"}""".formatted(original.getName());
HttpClientResponseException exception = assertThrows(HttpClientResponseException.class, () -> create(postBody));

assertEquals(HttpStatus.BAD_REQUEST, exception.getResponse().status());
assertEquals("Certification with name Default already exists", exception.getMessage());

// Cannot update a certification to have the same name as another
Certification other = createCertification("Other");
Certification other = createCertification("Other", "Description");
UUID otherId = other.getId();
HttpClientResponseException exception2 = assertThrows(HttpClientResponseException.class, () -> update(otherId, postBody));

Expand All @@ -156,53 +160,59 @@ void certificationNameUniquenessTest() {

// But we can successfully rename the certification
update(other.getId(), """
{"name":"Updated"}""");
{"name":"Updated","description":"d"}""");

// and we can update to keep the same name
update(other.getId(), """
{"name":"Updated","badgeUrl":"woo"}""");
{"name":"Updated","description":"desc","badgeUrl":"woo"}""");

// Check the list of exceptions are as we expect
List<Certification> list = list();
assertEquals(2, list.size());
assertEquals(List.of("Default", "Updated"), list.stream().map(Certification::getName).toList());
assertEquals(List.of("Description", "desc"), list.stream().map(Certification::getDescription).toList());
assertEquals(Arrays.asList(null, "woo"), list.stream().map(Certification::getBadgeUrl).toList());
}

@Test
void certificationsDefaultToEnabled() {
Certification nameAndUrlActive = create("""
{"name":"a","badgeUrl":"badge"}""");
{"name":"a","description":"da","badgeUrl":"badge"}""");

assertEquals("a", nameAndUrlActive.getName());
assertEquals("da", nameAndUrlActive.getDescription());
assertEquals("badge", nameAndUrlActive.getBadgeUrl());
assertTrue(nameAndUrlActive.isActive());

Certification nameNoUrlActive = create("""
{"name":"b"}""");
{"name":"b","description":"db"}""");

assertEquals("b", nameNoUrlActive.getName());
assertEquals("db", nameNoUrlActive.getDescription());
assertNull(nameNoUrlActive.getBadgeUrl());
assertTrue(nameNoUrlActive.isActive());

Certification nameNoUrlInactive = create("""
{"name":"c","active":"false"}""");
{"name":"c","description":"dc","active":"false"}""");

assertEquals("c", nameNoUrlInactive.getName());
assertEquals("dc", nameNoUrlInactive.getDescription());
assertNull(nameNoUrlInactive.getBadgeUrl());
assertFalse(nameNoUrlInactive.isActive());

Certification nameNoUrlInactiveBool = create("""
{"name":"d","active":false}""");
{"name":"d","description":"dd","active":false}""");

assertEquals("d", nameNoUrlInactiveBool.getName());
assertEquals("dd", nameNoUrlInactiveBool.getDescription());
assertNull(nameNoUrlInactiveBool.getBadgeUrl());
assertFalse(nameNoUrlInactiveBool.isActive());

Certification nameUrlSetActive = create("""
{"name":"e","badgeUrl":"badge2","active":true}""");
{"name":"e","description":"de","badgeUrl":"badge2","active":true}""");

assertEquals("e", nameUrlSetActive.getName());
assertEquals("de", nameUrlSetActive.getDescription());
assertEquals("badge2", nameUrlSetActive.getBadgeUrl());
assertTrue(nameUrlSetActive.isActive());
}
Expand Down
Loading

0 comments on commit 351d451

Please sign in to comment.