View Javadoc
1   package emissary.directory;
2   
3   import emissary.client.EmissaryClient;
4   import emissary.client.EmissaryResponse;
5   import emissary.test.core.junit5.UnitTest;
6   
7   import jakarta.ws.rs.core.MediaType;
8   import org.apache.commons.io.IOUtils;
9   import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
10  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
11  import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
12  import org.apache.hc.core5.http.Header;
13  import org.apache.hc.core5.http.HttpEntity;
14  import org.apache.hc.core5.http.HttpHeaders;
15  import org.apache.hc.core5.http.NoHttpResponseException;
16  import org.apache.hc.core5.http.message.BasicHeader;
17  import org.apache.hc.core5.http.protocol.HttpContext;
18  import org.junit.jupiter.api.Test;
19  
20  import java.io.IOException;
21  import java.nio.charset.StandardCharsets;
22  
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.mockito.ArgumentMatchers.any;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  class HeartbeatManagerTest extends UnitTest {
30  
31      @Test
32      void testBadGetHearbeatHasBAD_RESPONSE() {
33          String fromPlace = "EMISSARY_DIRECTORY_SERVICES.DIRECTORY.STUDY.http://localhost:8001/DirectoryPlace";
34          String toPlace = "*.*.*.http://localhost:1233/DirectoryPlace";
35          EmissaryResponse response = HeartbeatManager.getHeartbeat(fromPlace, toPlace);
36          assertTrue(response.getContentString().contains(HeartbeatManager.BAD_RESPOSNE));
37      }
38  
39      @Test
40      void testBadHeartbeat() {
41          String directoryKey = "EMISSARY_DIRECTORY_SERVICES.DIRECTORY.STUDY.http://localhost:8001/DirectoryPlace";
42          HeartbeatManager mgr = new HeartbeatManager(directoryKey, 30, 30);
43          boolean isUp = mgr.heartbeat("*.*.*.http://localhost:1222/DirectoryPlace");
44          assertFalse(isUp);
45      }
46  
47      @Test
48      void testSlowHeartbeat() throws IOException {
49          // peer didn't respond before the timeout, throws org.apache.http.NoHttpResponseException which is still and
50          // IOException
51          CloseableHttpClient mockClient = mock(CloseableHttpClient.class);
52          when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class), any())).thenThrow(
53                  new NoHttpResponseException("localhost:1222 failed to respond"));
54  
55          EmissaryClient client = new EmissaryClient(mockClient);
56  
57          String fromPlace = "EMISSARY_DIRECTORY_SERVICES.DIRECTORY.STUDY.http://localhost:8001/DirectoryPlace";
58          String toPlace = "*.*.*.http://localhost:1233/DirectoryPlace";
59          EmissaryResponse response = HeartbeatManager.getHeartbeat(fromPlace, toPlace, client); // use that client
60          assertTrue(response.getContentString().contains(HeartbeatManager.BAD_RESPOSNE));
61      }
62  
63      @Test
64      void testUnauthorizedHeartbeat() throws IOException {
65          CloseableHttpClient mockClient = mock(CloseableHttpClient.class);
66          CloseableHttpResponse mockResponse = mock(CloseableHttpResponse.class);
67          HttpEntity mockHttpEntity = mock(HttpEntity.class);
68  
69          when(mockResponse.getCode()).thenReturn(401);
70          when(mockResponse.getEntity()).thenReturn(mockHttpEntity);
71          String responseString = "Unauthorized heartbeat man";
72          when(mockHttpEntity.getContent()).thenReturn(IOUtils.toInputStream(responseString, StandardCharsets.UTF_8));
73          BasicHeader header1 = new BasicHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
74          Header[] headers = new Header[] {header1};
75          when(mockResponse.getHeaders(any())).thenReturn(headers);
76  
77          EmissaryResponse resp = new EmissaryResponse(mockResponse);
78          when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class), any())).thenReturn(resp);
79  
80          EmissaryClient client = new EmissaryClient(mockClient);
81  
82          String fromPlace = "EMISSARY_DIRECTORY_SERVICES.DIRECTORY.STUDY.http://localhost:8001/DirectoryPlace";
83          String toPlace = "*.*.*.http://localhost:1233/DirectoryPlace";
84          EmissaryResponse response = HeartbeatManager.getHeartbeat(fromPlace, toPlace, client); // use that client
85          assertTrue(response.getContentString().contains("Bad request -> status: 401 message: " + responseString));
86      }
87  
88  }