diff --git a/iam-login-service/src/main/java/it/infn/mw/iam/core/IamStatisticalEndpoint.java b/iam-login-service/src/main/java/it/infn/mw/iam/core/IamStatisticalEndpoint.java new file mode 100644 index 000000000..52802ce0e --- /dev/null +++ b/iam-login-service/src/main/java/it/infn/mw/iam/core/IamStatisticalEndpoint.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package it.infn.mw.iam.core; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import it.infn.mw.iam.persistence.repository.IamAccountRepository; + +@RestController +public class IamStatisticalEndpoint { + + @Autowired + IamAccountRepository accountRepo; + + @GetMapping("/stats") + public StatsEndpointResponse getStats() { + long count = accountRepo.count(); + return new StatsEndpointResponse(count); + } +} diff --git a/iam-login-service/src/main/java/it/infn/mw/iam/core/StatsEndpointResponse.java b/iam-login-service/src/main/java/it/infn/mw/iam/core/StatsEndpointResponse.java new file mode 100644 index 000000000..73c1317a8 --- /dev/null +++ b/iam-login-service/src/main/java/it/infn/mw/iam/core/StatsEndpointResponse.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package it.infn.mw.iam.core; + +public class StatsEndpointResponse { + + private long numberOfUsers; + + public StatsEndpointResponse(long numberOfUsers) { + this.numberOfUsers = numberOfUsers; + } + + public long getNumberOfUsers() { + return numberOfUsers; + } + + public void setNumberOfUsers(long numberOfUsers) { + this.numberOfUsers = numberOfUsers; + } +} diff --git a/iam-login-service/src/test/java/it/infn/mw/iam/test/core/IamStatisticalEndpointTests.java b/iam-login-service/src/test/java/it/infn/mw/iam/test/core/IamStatisticalEndpointTests.java new file mode 100644 index 000000000..53fa80cb6 --- /dev/null +++ b/iam-login-service/src/test/java/it/infn/mw/iam/test/core/IamStatisticalEndpointTests.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package it.infn.mw.iam.test.core; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import it.infn.mw.iam.core.StatsEndpointResponse; +import it.infn.mw.iam.test.util.annotation.IamMockMvcIntegrationTest; + +@RunWith(SpringRunner.class) +@IamMockMvcIntegrationTest +public class IamStatisticalEndpointTests { + + @Autowired + protected MockMvc mvc; + + @Test + public void anonymousisAcceptedAtStatEndpoint() throws Exception { + mvc.perform(get("/stats")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.numberOfUsers", equalTo(255))); + } + + @Test + public void testSetNumberOfUsers() { + StatsEndpointResponse userCount = new StatsEndpointResponse(0); + userCount.setNumberOfUsers(255); + assertEquals(255, userCount.getNumberOfUsers()); + } + +}