-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a statistical anonymous endpoint (#790)
Create a statistical anonymous endpoint returning the total number of users
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
iam-login-service/src/main/java/it/infn/mw/iam/core/IamStatisticalEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
iam-login-service/src/main/java/it/infn/mw/iam/core/StatsEndpointResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
iam-login-service/src/test/java/it/infn/mw/iam/test/core/IamStatisticalEndpointTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |