Skip to content

Commit

Permalink
Query result metadata field decoding fix
Browse files Browse the repository at this point in the history
- in the metadata of QueryStatus, the query result metadata field
was always gibberish
- This fixes that
  • Loading branch information
Luke-Sikina committed Jun 27, 2023
1 parent 8b5b4b3 commit f741d42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.transaction.Transactional;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.util.*;

Expand Down Expand Up @@ -258,8 +259,8 @@ public QueryStatus queryMetadata(UUID queryId, HttpHeaders headers){
Map<String, Object> metadata = new HashMap<String, Object>();
try {
metadata.put(QUERY_JSON_FIELD, new ObjectMapper().readValue(query.getQuery(), Object.class));
metadata.put(QUERY_RESULT_METADATA_FIELD, String.valueOf(query.getMetadata()));
} catch (JsonProcessingException e) {
metadata.put(QUERY_RESULT_METADATA_FIELD, new String(query.getMetadata(), StandardCharsets.UTF_8));
} catch (JsonProcessingException | NullPointerException e) {
logger.warn("Unable to use object mapper", e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.nio.charset.StandardCharsets;
import java.util.*;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
Expand Down Expand Up @@ -414,6 +417,31 @@ public Void answer(InvocationOnMock invocation) {
queryId.toString(), queryEntity.getResourceResultId());

}

@Test
public void testShouldQueryMetadata() {
Resource resource = new Resource();
resource.setUuid(resourceId);
String resultId = UUID.randomUUID().toString();
Query query = new Query();
query.setQuery("{}");
query.setResource(resource);
query.setStatus(PicSureStatus.AVAILABLE);
query.setStartTime(new java.sql.Date(System.currentTimeMillis()));
query.setResourceResultId(resultId);

String metaData = "{\\\"picsureQueryId\\\":\\\"b9e6cea7-142e-5859-8e98-1245c959fc0b\\\"," +
"\\\"commonAreaId\\\":\\\"290a9d2e-1a20-4407-b351-499185f5554e\\\"}";

query.setMetadata(metaData.getBytes(StandardCharsets.UTF_8));

Mockito.when(queryRepo.getById(query.getUuid()))
.thenReturn(query);

QueryStatus status = queryService.queryMetadata(query.getUuid(), null);
String actual = (String) status.getResultMetadata().get("queryResultMetadata");
Assert.assertEquals(metaData, actual);
}

@Test
public void testQuerySyncValidWithResponseId() {
Expand Down

0 comments on commit f741d42

Please sign in to comment.