diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/service/OrganizationServiceTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/service/OrganizationServiceTest.java index 95667462e4..e00246d647 100644 --- a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/service/OrganizationServiceTest.java +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/service/OrganizationServiceTest.java @@ -1,81 +1,91 @@ -// package org.sagebionetworks.openchallenges.organization.service.service; - -// import java.util.Arrays; -// import java.util.List; -// import -// org.sagebionetworks.openchallenges.organization.service.exception.OrganizationNotFoundException; -// import org.sagebionetworks.openchallenges.organization.service.model.dto.OrganizationDto; -// import -// org.sagebionetworks.openchallenges.organization.service.model.dto.OrganizationSearchQueryDto; -// import org.sagebionetworks.openchallenges.organization.service.model.dto.OrganizationsPageDto; -// import org.sagebionetworks.openchallenges.organization.service.model.entity.OrganizationEntity; -// import org.sagebionetworks.openchallenges.organization.service.model.mapper.OrganizationMapper; -// import -// org.sagebionetworks.openchallenges.organization.service.model.repository.OrganizationRepository; -// import org.sagebionetworks.openchallenges.organization.service.service.OrganizationService; -// import org.slf4j.Logger; -// import org.slf4j.LoggerFactory; -// import org.springframework.data.domain.Page; -// import org.springframework.data.domain.PageRequest; -// import org.springframework.data.domain.Pageable; -// import org.springframework.stereotype.Service; -// import org.springframework.transaction.annotation.Transactional; -// import java.time.OffsetDateTime; -// import java.util.Collections; -// import org.junit.jupiter.api.Assertions; -// import org.junit.jupiter.api.Test; -// import org.junit.jupiter.api.BeforeEach; - -// public class OrganizationServiceTest { -// private Long id = 1L; -// private String name = "Test Organization"; -// private String avatarKey = "avatarKey"; -// private String websiteUrl = "https://example.com"; -// private Integer challengeCount = 5; -// private String description = "Test description"; -// private OffsetDateTime createdAt = OffsetDateTime.now(); -// private OffsetDateTime updatedAt = OffsetDateTime.now(); -// private String acronym = "TO"; - -// @BeforeEach -// public void setup() { -// private OrganizationEntity entityFromConstructor; -// private OrganizationEntity entityFromConstructor2; -// private final Logger LOG = LoggerFactory.getLogger(OrganizationService.class); -// private OrganizationRepository organizationRepository; - -// entityFromConstructor = new OrganizationEntity(); - -// entityFromConstructor.setId(id); -// entityFromConstructor.setName(name); -// entityFromConstructor.setDescription(description); -// entityFromConstructor.setAvatarKey(avatarKey); -// entityFromConstructor.setWebsiteUrl(websiteUrl); -// entityFromConstructor.setChallengeCount(challengeCount); -// entityFromConstructor.setCreatedAt(createdAt); -// entityFromConstructor.setUpdatedAt(updatedAt); -// entityFromConstructor.setAcronym(acronym); -// entityFromConstructor.setCategories(Collections.emptyList()); -// entityFromConstructor.setChallengeContributions(Collections.emptyList()); - -// entityFromConstructor2 = new OrganizationEntity(); - -// entityFromConstructor2.setId(id); -// entityFromConstructor2.setName(name); -// entityFromConstructor2.setDescription(description); -// entityFromConstructor2.setAvatarKey(avatarKey); -// entityFromConstructor2.setWebsiteUrl(websiteUrl); -// entityFromConstructor2.setChallengeCount(challengeCount); -// entityFromConstructor2.setCreatedAt(createdAt); -// entityFromConstructor2.setUpdatedAt(updatedAt); -// entityFromConstructor2.setAcronym(acronym); -// entityFromConstructor2.setCategories(Collections.emptyList()); -// entityFromConstructor2.setChallengeContributions(Collections.emptyList()); - -// } - -// @Test -// public void ConvertToEntity_ShouldReturnDtoProperties_WhenDtoPropertiesPassed() { -// } - -// } +package org.sagebionetworks.openchallenges.organization.service.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.sagebionetworks.openchallenges.organization.service.exception.OrganizationNotFoundException; +import org.sagebionetworks.openchallenges.organization.service.model.dto.OrganizationDto; +import org.sagebionetworks.openchallenges.organization.service.model.entity.OrganizationEntity; +import org.sagebionetworks.openchallenges.organization.service.model.mapper.OrganizationMapper; +import org.sagebionetworks.openchallenges.organization.service.model.repository.OrganizationRepository; + +@ExtendWith(MockitoExtension.class) +class OrganizationServiceTest { + + @Mock private OrganizationRepository organizationRepository; + + @Mock private OrganizationMapper organizationMapper; + + OrganizationService organizationService; + + @BeforeEach + void setup() { + organizationService = new OrganizationService(organizationRepository); + } + + @Test + void getOrganization_ShouldReturnOrganizationDto_WhenValidIdentifierPassed() { + // Create valid identifier + String identifier = "123"; + Long orgId = 123L; + String orgLogin = String.valueOf(identifier); + + OrganizationEntity orgEntity = new OrganizationEntity(); + orgEntity.setId(orgId); + + OrganizationDto expectedDto = new OrganizationDto(); + expectedDto.setId(orgId); + + // Stubbing configuration to simulate a specific behavior of the organizationRepository mock + // object to control its response and verify the behavior of the tested code that interacts with + // it. + when(organizationRepository.findByIdOrLogin(orgId, orgLogin)) + .thenReturn(Optional.of(orgEntity)); + + // get the organization using the identifier + OrganizationDto response = organizationService.getOrganization(identifier); + + // Test that the organization is in the repo, is not null, that the id can be pulled, + // findByIdOrLogin method is called, and no interactions with the organizationMapper (verifies + // interactions w/Mock object) + + verify(organizationRepository).findByIdOrLogin(orgId, orgLogin); + assertNotNull(response); + assertEquals(expectedDto.getId(), response.getId()); + verifyNoInteractions(organizationMapper); + } + + @Test + void getOrganization_ShouldThrowOrganizationNotFoundException_WhenInvalidIdentifierPassed() { + // Create invalid identifier + String invalidIdentifier = "abc"; + + // Stubbing configuration to simulate a specific behavior of the organizationRepository mock + // object to control its response and verify the behavior of the tested code that interacts with + // it. + when(organizationRepository.findByIdOrLogin(any(), any())).thenReturn(Optional.empty()); + + // Test that calling getOrganization throws an error + assertThrows( + OrganizationNotFoundException.class, + () -> { + organizationService.getOrganization(invalidIdentifier); + }); + /* Test thatfindByIdOrLogin method is called, and no interactions with the organizationMapper + (verifies interactions w/Mock object)*/ + + verify(organizationRepository).findByIdOrLogin(any(), any()); + verifyNoInteractions(organizationMapper); + } +}