Skip to content

Commit

Permalink
Adding tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zarna1parekh committed Oct 31, 2024
1 parent 0fc328f commit f55cac3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.slack.astra.zipkinApi;

import brave.Span;
import brave.Tracing;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Resources;
import com.linecorp.armeria.common.HttpStatus;
import com.slack.astra.proto.service.AstraSearch;
import com.slack.astra.server.AstraQueryServiceBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpResponse;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;


public class ZipkinServiceTest {
@Mock
private AstraQueryServiceBase searcher;
private ZipkinService zipkinService;
private AstraSearch.SearchResult mockSearchResult;

@BeforeEach
public void setup() throws IOException {
MockitoAnnotations.openMocks(this);
zipkinService = spy(new ZipkinService(searcher));
// Build mockSearchResult
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(Resources.getResource("zipkinApi/search_result.json"));
String jsonString = objectMapper.writeValueAsString(jsonNode);
AstraSearch.SearchResult.Builder builder = AstraSearch.SearchResult.newBuilder();
JsonFormat.parser().merge(jsonString, builder);
mockSearchResult = builder.build();
}

@Test
public void testGetTraceByTraceId_onlyTraceIdProvided() throws Exception {

try (MockedStatic<Tracing> mockedTracing = mockStatic(Tracing.class)) {
brave.Tracer mockTracer = mock(brave.Tracer.class);
Span mockSpan = mock(Span.class);

mockedTracing.when(Tracing::currentTracer).thenReturn(mockTracer);
when(mockTracer.currentSpan()).thenReturn(mockSpan);
String traceId = "test_trace_1";

when(searcher.doSearch(any())).thenReturn(mockSearchResult);

// Act
HttpResponse response = zipkinService.getTraceByTraceId(traceId, Optional.empty(), Optional.empty(), Optional.empty());
AggregatedHttpResponse aggregatedResponse = response.aggregate().join();

// Assert
assertEquals(HttpStatus.OK, aggregatedResponse.status());
}
}


}
10 changes: 10 additions & 0 deletions astra/src/test/resources/zipkinApi/search_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"hits": [
"{\"id\":\"101\",\"timestamp\":1730407713.560000000,\"source\":{\"service-name\":\"traces-staging\",\"trace_id\":\"1234556789\",\"_timesinceepoch\":\"2024-10-31T20:48:33.560Z\",\"level\":\"INFO\",\"service_name\":\"all_tenants\",\"message\":\"This is a test log message\",\"timestamp\":\"2024-03-20T15:30:00.000Z\"},\"index\":\"all_tenants\"}",
"{\"id\":\"100\",\"timestamp\":1730407435.071000000,\"source\":{\"service-name\":\"traces-staging\",\"trace_id\":\"1234556789\",\"_timesinceepoch\":\"2024-10-31T20:43:55.071Z\",\"level\":\"INFO\",\"service_name\":\"all_tenants\",\"message\":\"This is a test log message 1\",\"timestamp\":\"2024-03-20T15:30:00.000Z\"},\"index\":\"all_tenants\"}"
],
"tookMicros": "2732",
"totalNodes": 1,
"totalSnapshots": 1,
"snapshotsWithReplicas": 1
}

0 comments on commit f55cac3

Please sign in to comment.