From a2768fb2eb2077b15624d71c011e78776a215061 Mon Sep 17 00:00:00 2001 From: chasegawa Date: Thu, 20 Oct 2022 16:05:05 -0700 Subject: [PATCH] SHIBUI-2380 fixing repository issues with returning the protocol for EntityDescriptorProjections --- .../EntityDescriptorProjection.java | 65 +++++++++++++++---- .../EntityDescriptorRepository.java | 10 ++- .../EntityDescriptorControllerTests.groovy | 7 +- 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorProjection.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorProjection.java index c0640edc3..22e78ba59 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorProjection.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorProjection.java @@ -1,22 +1,61 @@ package edu.internet2.tier.shibboleth.admin.ui.repository; +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonInclude; import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptorProtocol; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; import java.time.LocalDateTime; -public interface EntityDescriptorProjection { - default String getId() { - return getResourceId(); +public class EntityDescriptorProjection { + @Getter + String id; + String entityID; + String entityId; + @Getter + String resourceId; + @Getter + String serviceProviderName; + @Getter + String createdBy; + @Getter + LocalDateTime createdDate; + @Getter + boolean serviceEnabled; + @Getter + String idOfOwner; + EntityDescriptorProtocol protocol; + + public EntityDescriptorProjection(String entityID, String resourceId, String serviceProviderName, String createdBy, + LocalDateTime createdDate, boolean serviceEnabled, String idOfOwner, String protocol) { + this.entityID = entityID; + this.entityId = entityID; + this.resourceId = resourceId; + this.id = resourceId; + this.serviceProviderName = serviceProviderName; + this.createdBy = createdBy; + this.createdDate = createdDate; + this.serviceEnabled = serviceEnabled; + this.idOfOwner = idOfOwner; + setProtocol(protocol); + } + + public String getEntityID() { + return entityID; + } + + public String getEntityId() { + return entityId; + } + + public EntityDescriptorProtocol getProtocol() { + return protocol == null ? EntityDescriptorProtocol.SAML : protocol; } - String getEntityID(); - default String getEntityId() { - return getEntityID(); + + public void setProtocol(String index) { + int i = Integer.valueOf(index); + protocol = EntityDescriptorProtocol.values()[i]; } - String getResourceId(); - String getServiceProviderName(); - String getCreatedBy(); - LocalDateTime getCreatedDate(); - boolean getServiceEnabled(); - String getIdOfOwner(); - EntityDescriptorProtocol getProtocol(); } \ No newline at end of file diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorRepository.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorRepository.java index bb2b275d6..68cac3803 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorRepository.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/repository/EntityDescriptorRepository.java @@ -3,6 +3,7 @@ import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptor; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; import java.util.stream.Stream; @@ -12,9 +13,16 @@ * Repository to manage {@link EntityDescriptor} instances. */ public interface EntityDescriptorRepository extends JpaRepository { + @Query(value = "select new edu.internet2.tier.shibboleth.admin.ui.repository.EntityDescriptorProjection(e.entityID, e.resourceId, e.serviceProviderName, e.createdBy, " + + "e.createdDate, e.serviceEnabled, e.idOfOwner, case e.protocol when null then 'SAML' else e.protocol end ) " + + "from EntityDescriptor e") List findAllBy(); - List findAllByIdOfOwner(String ownerId); + @Query(value = "select new edu.internet2.tier.shibboleth.admin.ui.repository.EntityDescriptorProjection(e.entityID, e.resourceId, e.serviceProviderName, e.createdBy, " + + "e.createdDate, e.serviceEnabled, e.idOfOwner, case e.protocol when null then 'SAML' else e.protocol end ) " + + "from EntityDescriptor e " + + "where e.idOfOwner = :ownerId") + List findAllByIdOfOwner(@Param("ownerId") String ownerId); EntityDescriptor findByEntityID(String entityId); diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy index 7c9ee1537..33de12c2f 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerTests.groovy @@ -27,6 +27,7 @@ import lombok.SneakyThrows import org.springframework.beans.factory.annotation.Autowired import org.springframework.core.io.ClassPathResource import org.springframework.security.test.context.support.WithMockUser +import org.springframework.test.web.servlet.result.MockMvcResultHandlers import org.springframework.test.web.servlet.setup.MockMvcBuilders import org.springframework.transaction.annotation.Transactional import org.springframework.web.client.RestTemplate @@ -160,11 +161,13 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest { def result = mockMvc.perform(get('/api/EntityDescriptors')) then: - result.andExpect(expectedHttpResponseStatus).andExpect(content().contentType(expectedResponseContentType)) + result.andDo(MockMvcResultHandlers.print()) + .andExpect(expectedHttpResponseStatus).andExpect(content().contentType(expectedResponseContentType)) .andExpect(jsonPath("\$.[0].id").value("uuid-1")) .andExpect(jsonPath("\$.[0].entityId").value("eid1")) .andExpect(jsonPath("\$.[0].serviceEnabled").value(true)) .andExpect(jsonPath("\$.[0].idOfOwner").value("admingroup")) + .andExpect(jsonPath("\$.[0].protocol").value("SAML")) } @WithMockAdmin @@ -189,10 +192,12 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest { .andExpect(jsonPath("\$.[0].entityId").value("eid1")) .andExpect(jsonPath("\$.[0].serviceEnabled").value(true)) .andExpect(jsonPath("\$.[0].idOfOwner").value("admingroup")) + .andExpect(jsonPath("\$.[0].protocol").value("SAML")) .andExpect(jsonPath("\$.[1].id").value("uuid-2")) .andExpect(jsonPath("\$.[1].entityId").value("eid2")) .andExpect(jsonPath("\$.[1].serviceEnabled").value(false)) .andExpect(jsonPath("\$.[1].idOfOwner").value("admingroup")) + .andExpect(jsonPath("\$.[1].protocol").value("SAML")) } @WithMockUser(value = "someUser", roles = ["USER"])