Commit 6cf8ee9a authored by Sviatoslav Nekhaienko's avatar Sviatoslav Nekhaienko
Browse files

fix unexpected conversion from int to double

parent 8c724ba2
Pipeline #12491 failed with stage
in 2 minutes and 5 seconds
......@@ -14,6 +14,8 @@
package org.opengroup.osdu.indexer.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.HttpMethods;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
......@@ -59,6 +61,8 @@ public class StorageServiceImpl implements StorageService {
private final Gson gson = new Gson();
@Inject
private ObjectMapper objectMapper;
@Inject
private IUrlFetchService urlFetchService;
@Inject
......@@ -98,7 +102,7 @@ public class StorageServiceImpl implements StorageService {
return Records.builder().records(valid).notFound(notFound).conversionStatuses(conversionStatuses).missingRetryRecords(missingRetryRecordIds).build();
}
private Records getRecords(List<String> ids) throws URISyntaxException {
protected Records getRecords(List<String> ids) throws URISyntaxException {
// e.g. {"records":["test:10"]}
String body = this.gson.toJson(RecordIds.builder().records(ids).build());
......@@ -123,9 +127,12 @@ public class StorageServiceImpl implements StorageService {
throw new AppException(HttpStatus.SC_NOT_FOUND, "Invalid request", "Storage service returned empty response");
}
Type recordsListType = new TypeToken<Records>() {
}.getType();
Records records = this.gson.fromJson(bulkStorageData, recordsListType);
Records records = null;
try {
records = this.objectMapper.readValue(bulkStorageData, Records.class);
} catch (JsonProcessingException e) {
throw new AppException(RequestStatus.INVALID_RECORD, "Invalid request", "Successful Storage service response with wrong json");
}
// no retry possible, update record status as failed -- storage service cannot locate records
if (!records.getNotFound().isEmpty()) {
......
package org.opengroup.osdu.indexer.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.opengroup.osdu.core.common.http.IUrlFetchService;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.HttpResponse;
import org.opengroup.osdu.core.common.model.indexer.Records;
import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URISyntaxException;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(SpringRunner.class)
public class StorageServiceImplTest {
@Spy
private ObjectMapper objectMapper = new ObjectMapper();
@Mock
private IUrlFetchService urlFetchService;
@InjectMocks
private StorageServiceImpl sut;
@Mock
private IRequestInfo requestInfo;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(this.requestInfo.getHeaders()).thenReturn(new DpsHeaders());
}
@Test
public void getRecords() throws URISyntaxException {
String body = "{\"records\":[{\"id\":\"id1\",\"version\":0,\"data\":{\"long_int\":1000000000000000000000000,\"int\":123}}],\"notFound\":[],\"conversionStatuses\":[],\"missingRetryRecords\":[]}";
HttpResponse httpResponse = mock(HttpResponse.class);
when(httpResponse.getBody()).thenReturn(body);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any())).thenReturn(httpResponse);
Records rec = sut.getRecords(Collections.singletonList("id1"));
assertEquals(1, rec.getRecords().size());
assertEquals("1000000000000000000000000", rec.getRecords().get(0).getData().get("long_int").toString());
assertEquals("123", rec.getRecords().get(0).getData().get("int").toString());
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment