diff --git a/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java b/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java new file mode 100755 index 0000000..5ebe6f9 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java @@ -0,0 +1,43 @@ +package com.plannaplan.controllers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; + +import com.plannaplan.App; +import com.plannaplan.responses.models.StatisticCreatedGroupsResponse; +import com.plannaplan.services.GroupService; + +/** + * Rest controller to enpoint that help deveopler test the app + */ +@RestController +@CrossOrigin +@RequestMapping("/api/" + App.API_VERSION + "/statistics") +@Api(tags = { + "StatisticsController" }, value = "StatisticsController", description = "Statistics are meant to be used by deanery only so in every endpoint you need to provide DEANERY token.") + +public class StatisticsController { + + @Autowired + private GroupService groupService; + + /** + * @return if tour was set + */ + @PreAuthorize("hasRole('ROLE_DEANERY')") + @GetMapping(path = "/groups/created") + public ResponseEntity getGroupsAmmounts() { + final StatisticCreatedGroupsResponse response = new StatisticCreatedGroupsResponse( + this.groupService.getGroupsAmmount()); + return new ResponseEntity<>(response, HttpStatus.OK); + } + +} diff --git a/restservice/src/main/java/com/plannaplan/responses/models/StatisticCreatedGroupsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/StatisticCreatedGroupsResponse.java new file mode 100755 index 0000000..81cc68a --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/StatisticCreatedGroupsResponse.java @@ -0,0 +1,23 @@ +package com.plannaplan.responses.models; + +/** + * Api response for created groups statistics + */ +public class StatisticCreatedGroupsResponse { + private Integer ammount; + + /** + * @param ammount created groups + */ + public StatisticCreatedGroupsResponse(Integer ammount) { + this.ammount = ammount; + } + + /** + * @return ammount of created groups + */ + public Integer getAmmount() { + return ammount; + } + +} diff --git a/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java new file mode 100755 index 0000000..c54e852 --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java @@ -0,0 +1,64 @@ +package com.plannaplan.controllers; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; + +import com.plannaplan.entities.User; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration +public class StatisticsControllerTest extends AbstractControllerTest { + + private static final String GROUP_AMMOUNTS_ENDPOINT = "/api/v1/statistics/groups/created"; + + @Autowired + private UserService userService; + + /* GROUP AMMOUNTS TESTS */ + + @Test + public void shouldFailWithWrongAccesGroupsAmmounts() throws Exception { + final String mail = "shouldFailWithWrongAccesGroupsAmmounts@StatisticsController.test"; + final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER)); + + final String token = this.userService.login(usr).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GROUP_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().is4xxClientError()); + + } + + @Test + public void shouldOkGettingGroupsAmmounts() throws Exception { + final String mail = "shouldOkGettingGroupsAmmounts@StatisticsController.test"; + final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEANERY)); + + final String token = this.userService.login(usr).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GROUP_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); + + } + + @Test + public void shouldFailWithNoTokenGroupsAmmounts() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GROUP_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError()); + + } +}