View Javadoc
1   package civitas.bboard.server.controllers.tests;
2   
3   import static org.mockito.ArgumentMatchers.any;
4   import static org.mockito.ArgumentMatchers.eq;
5   import static org.mockito.Mockito.mock;
6   import static org.mockito.Mockito.when;
7   
8   import java.io.IOException;
9   import java.lang.reflect.Field;
10  import java.util.Map;
11  
12  import org.springframework.web.client.RestTemplate;
13  
14  import civitas.bboard.server.controllers.GetRestTemplate;
15  import civitas.common.election.tests.ElectionDetailsTestData;
16  import civitas.common.tests.EnvironmentState;
17  
18  public class GetRestTemplateStub implements ElectionDetailsTestData {
19  	public static RestTemplate restTemplate;
20  
21  	public static GetRestTemplate stub() throws NoSuchFieldException, IllegalAccessException {
22  		GetRestTemplate mock = mock(GetRestTemplate.class);
23  		restTemplate = mock(RestTemplate.class);
24  		Field restTemplateField = GetRestTemplate.class.getDeclaredField("restTemplate");
25  		restTemplateField.setAccessible(true);
26  		restTemplateField.set(mock, restTemplate);
27  		when(mock.apply()).thenReturn(restTemplate);
28  		when(restTemplate.postForObject(eq(ELECTION_ID.uriBase() + "/post"), any(), any()))
29  				.then(new EnvDependentAnswer<>(Map.of(
30  						EnvironmentState.NORMAL,
31  						new AnswerOrThrowable<>(null, true),
32  						EnvironmentState.ELECTION_SERVER_IS_UNREACHEABLE,
33  						new AnswerOrThrowable<>(new IOException(), null))));
34  		return mock;
35  	}
36  }