Skip to content

Commit

Permalink
Add a statistical anonymous endpoint (#790)
Browse files Browse the repository at this point in the history
Create a statistical anonymous endpoint returning the total number of users
  • Loading branch information
rmiccoli authored Jul 22, 2024
1 parent 90217e2 commit 178b87e
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
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);
}
}
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;
}
}
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());
}

}

0 comments on commit 178b87e

Please sign in to comment.