From 6e84490afba91ce66d6662737f784b2dbd85ca69 Mon Sep 17 00:00:00 2001
From: Sonia Zorba <sonia.zorba@inaf.it>
Date: Fri, 19 Mar 2021 16:48:09 +0100
Subject: [PATCH] Added membership endpoint

---
 .../ia2/gms/controller/MembersController.java   | 17 +++++++++++++++++
 .../gms/controller/MembersControllerTest.java   | 13 +++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/gms/src/main/java/it/inaf/ia2/gms/controller/MembersController.java b/gms/src/main/java/it/inaf/ia2/gms/controller/MembersController.java
index b29e6d9..840f67c 100644
--- a/gms/src/main/java/it/inaf/ia2/gms/controller/MembersController.java
+++ b/gms/src/main/java/it/inaf/ia2/gms/controller/MembersController.java
@@ -11,9 +11,12 @@ import it.inaf.ia2.gms.persistence.model.GroupEntity;
 import it.inaf.ia2.gms.service.GroupNameService;
 import it.inaf.ia2.gms.service.GroupsService;
 import it.inaf.ia2.rap.data.RapUser;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -84,6 +87,20 @@ public class MembersController {
         return new PaginatedData<>(members, request.getPaginatorPage(), request.getPaginatorPageSize());
     }
 
+    @GetMapping(value = "/membership", produces = MediaType.TEXT_PLAIN_VALUE)
+    public void getMembership(@RequestParam("group") Optional<String> groupNames, @RequestParam("user_id") String userId, HttpServletResponse response) throws IOException {
+
+        GroupEntity parent = groupNameService.getGroupFromNames(groupNames);
+
+        List<GroupEntity> groups = membershipManager.getUserGroups(parent, userId);
+
+        try ( PrintWriter pw = new PrintWriter(response.getOutputStream())) {
+            for (String groupName : groupNameService.getGroupsNames(groups)) {
+                pw.println(groupNameService.getShortGroupName(groupName, groupNames));
+            }
+        }
+    }
+
     @PostMapping(value = "/membership", produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
     public void addMember(@RequestParam("group") Optional<String> groupNames,
             @RequestParam("user_id") String targetUserId) {
diff --git a/gms/src/test/java/it/inaf/ia2/gms/controller/MembersControllerTest.java b/gms/src/test/java/it/inaf/ia2/gms/controller/MembersControllerTest.java
index ae69b82..56b1746 100644
--- a/gms/src/test/java/it/inaf/ia2/gms/controller/MembersControllerTest.java
+++ b/gms/src/test/java/it/inaf/ia2/gms/controller/MembersControllerTest.java
@@ -12,6 +12,7 @@ import it.inaf.ia2.gms.persistence.model.GroupEntity;
 import it.inaf.ia2.gms.persistence.model.MembershipEntity;
 import it.inaf.ia2.gms.service.GroupNameService;
 import it.inaf.ia2.gms.service.GroupsService;
+import java.util.ArrayList;
 import java.util.Optional;
 import static org.hamcrest.CoreMatchers.is;
 import org.junit.Before;
@@ -28,6 +29,7 @@ import org.mockito.junit.MockitoJUnitRunner;
 import org.springframework.http.MediaType;
 import org.springframework.test.web.servlet.MockMvc;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -128,4 +130,15 @@ public class MembersControllerTest {
 
         verify(membershipManager, times(1)).removeMember(eq(inaf), eq("userId"));
     }
+
+    @Test
+    public void testGetMembers() throws Exception {
+
+        when(membershipManager.getUserGroups(any(), eq("userId"))).thenReturn(new ArrayList<>());
+
+        mockMvc.perform(get("/membership?group=LBT&user_id=userId"))
+                .andExpect(status().isOk());
+
+        verify(membershipManager, times(1)).getUserGroups(any(), eq("userId"));
+    }
 }
-- 
GitLab