Skip to content
Snippets Groups Projects
Commit f5fa7a5a authored by helayoty's avatar helayoty
Browse files

Merge branch 'heba-azure-work' of...

Merge branch 'heba-azure-work' of https://dev.azure.com/slb-des-ext-collaboration/open-data-ecosystem/_git/os-indexer into heba-azure-work
parents a8758273 b7d2af92
No related branches found
No related tags found
1 merge request!6Trusted ibm
......@@ -17,8 +17,10 @@ package org.opengroup.osdu.indexer.api;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.SwaggerDoc;
import org.opengroup.osdu.indexer.util.JobStatus;
import org.opengroup.osdu.indexer.model.RecordReindexRequest;
......@@ -26,6 +28,7 @@ import org.opengroup.osdu.indexer.service.IndexerService;
import org.opengroup.osdu.indexer.service.ReindexService;
import org.opengroup.osdu.indexer.util.RecordInfo;
import org.opengroup.osdu.is.core.model.RecordChangedMessages;
import org.opengroup.osdu.is.core.util.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
......@@ -56,17 +59,34 @@ public class RecordIndexerApi {
@NotNull(message = SwaggerDoc.REQUEST_VALIDATION_NOT_NULL_BODY)
@Valid @RequestBody RecordChangedMessages recordChangedMessages) throws Exception {
if (recordChangedMessages.missingAccountId()) {
throw new AppException(org.apache.http.HttpStatus.SC_BAD_REQUEST, "Invalid tenant",
String.format("Required header: '%s' not found", DpsHeaders.DATA_PARTITION_ID));
}
try {
if (recordChangedMessages == null) {
log.info("record change messages is null");
}
Type listType = new TypeToken<List<RecordInfo>>() {}.getType();
Type listType = new TypeToken<List<RecordInfo>>() {
}.getType();
List<RecordInfo> recordInfos = new Gson().fromJson(recordChangedMessages.getData(), listType);
if (recordInfos == null) {
log.info("RECORD INFO IS NULL");
}
if (recordInfos.size() == 0) {
log.info("none of record-change message can be deserialized");
return new ResponseEntity(HttpStatus.OK);
}
this.indexerService.processRecordChangedMessages(recordChangedMessages, recordInfos);
return new ResponseEntity(HttpStatus.OK);
} catch (AppException e) {
throw e;
} catch (JsonParseException e) {
throw new AppException(org.apache.http.HttpStatus.SC_BAD_REQUEST, "Request payload parsing error", "Unable to parse request payload.", e);
} catch (Exception e) {
throw new AppException(org.apache.http.HttpStatus.SC_BAD_REQUEST, "Unknown error", "An unknown error has occurred.", e);
}
}
// THIS IS AN INTERNAL USE API ONLY
......
......@@ -42,17 +42,17 @@ import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
@Ignore
@RunWith(PowerMockRunner.class)
@PrepareForTest({HeadersUtil.class, IndexerQueueTaskBuilder.class, DpsHeaders.class, Config.class})
public class RecordIndexerApiTest {
private final String messageValid = "{\"data\":\"[{\\\"id\\\":\\\"tenant1:welldb:wellbore-d9033ae1-fb15-496c-9ba0-880fd1d2b2cf\\\",\\\"kind\\\":\\\"tenant1:welldb:wellbore:1.0.0\\\",\\\"op\\\":\\\"create\\\"}]\",\"attributes\":{\"slb-account-id\":\"tenant1\",\"slb-correlation-id\":\"b5a281bd-f59d-4db2-9939-b2d85036fc7e\"},\"messageId\":\"75328163778221\",\"publishTime\":\"2018-05-08T21:48:56.131Z\"}";
private final String messageValid = "{\"data\":\"[{\\\"id\\\":\\\"opendes:welldb:wellbore-d9033ae1-fb15-496c-9ba0-880fd1d2b2cf\\\",\\\"kind\\\":\\\"tenant1:welldb:wellbore:1.0.0\\\",\\\"op\\\":\\\"create\\\"}]\",\"attributes\":{\"account-id\":\"opendes\",\"correlation-id\":\"b5a281bd-f59d-4db2-9939-b2d85036fc7e\"},\"messageId\":\"75328163778221\",\"publishTime\":\"2018-05-08T21:48:56.131Z\"}";
private final String messageEmpty = "{}";
private final String messageWithEmptyData = "{\"data\":\"[]\",\"attributes\":{\"slb-account-id\":\"tenant1\",\"slb-correlation-id\":\"b5a281bd-f59d-4db2-9939-b2d85036fc7e\"},\"messageId\":\"75328163778221\",\"publishTime\":\"2018-05-08T21:48:56.131Z\"}";
private final String messageWithIncorrectJsonFormat = "{\"data\":\"[{}}]\",\"attributes\":{\"slb-account-id\":\"tenant1\",\"slb-correlation-id\":\"b5a281bd-f59d-4db2-9939-b2d85036fc7e\"},\"messageId\":\"75328163778221\",\"publishTime\":\"2018-05-08T21:48:56.131Z\"}";
private final String messageWithEmptyData = "{\"data\":\"[]\",\"attributes\":{\"account-id\":\"opendes\",\"correlation-id\":\"b5a281bd-f59d-4db2-9939-b2d85036fc7e\"},\"messageId\":\"75328163778221\",\"publishTime\":\"2018-05-08T21:48:56.131Z\"}";
private final String messageWithIncorrectJsonFormat = "{\"data\":\"[{}}]\",\"attributes\":{\"account-id\":\"opendes\",\"correlation-id\":\"b5a281bd-f59d-4db2-9939-b2d85036fc7e\"},\"messageId\":\"75328163778221\",\"publishTime\":\"2018-05-08T21:48:56.131Z\"}";
private final String ACCOUNT_ID = "any-account";
private final String DATA_PARTITION_ID = "opendes";
@InjectMocks
private RecordIndexerApi sut;
......@@ -61,20 +61,15 @@ public class RecordIndexerApiTest {
@Mock
private IndexerService indexService;
@Mock
private DpsHeaders dpsHeaders;
@Before
public void setup() {
initMocks(this);
// mockStatic(HeadersUtil.class);
// mockStatic(IndexerQueueTaskBuilder.class);
// mockStatic(DpsHeaders.class);
// mockStatic(Config.class);
DpsHeaders dpsHeaders = new DpsHeaders();
dpsHeaders.put(DpsHeaders.ACCOUNT_ID, this.ACCOUNT_ID);
when(DpsHeaders.createFromEntrySet(any())).thenReturn(dpsHeaders);
when(Config.getDeploymentEnvironment()).thenReturn(DeploymentEnvironment.LOCAL);
dpsHeaders.put(DpsHeaders.DATA_PARTITION_ID, this.DATA_PARTITION_ID);
}
@Test
......@@ -89,7 +84,7 @@ public class RecordIndexerApiTest {
@Test
public void should_return400_given_emptyMessage_indexWorkerTest() {
should_return400_indexerWorkerTest(messageEmpty, "Required header: 'slb-data-partition-id' not found");
should_return400_indexerWorkerTest(messageEmpty, String.format("Required header: '%s' not found", DpsHeaders.DATA_PARTITION_ID));
}
@Test
......@@ -99,7 +94,7 @@ public class RecordIndexerApiTest {
private void should_return200_indexerWorkerTest(String message) throws Exception {
ResponseEntity response = this.sut.indexWorker(createRecordChangedMessage(message));
Assert.assertEquals(HttpStatus.OK, response.getStatusCodeValue());
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
}
private void should_return400_indexerWorkerTest(String message, String errorMessage) {
......@@ -107,7 +102,7 @@ public class RecordIndexerApiTest {
this.sut.indexWorker(createRecordChangedMessage(message));
fail("Should throw exception");
} catch (AppException e) {
Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getError().getCode());
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), e.getError().getCode());
Assert.assertEquals(errorMessage, e.getError().getMessage());
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
......
......@@ -15,6 +15,7 @@
package org.opengroup.osdu.indexer.model;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
......
......@@ -15,6 +15,7 @@
package org.opengroup.osdu.indexer.model;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
......
......@@ -57,8 +57,8 @@ public class RecordInfoTest {
Assert.assertEquals(OperationType.update, kindRecordOpMap.get("tenant1:testindexer3:well:1.0.0").get("tenant1:doc:test3"));
}
@Test
public void should_return400_getUpsertRecordTest(List<RecordInfo> msgs, String errorMessage) {
private void should_return400_getUpsertRecordTest(List<RecordInfo> msgs, String errorMessage) {
try {
RecordInfo.getUpsertRecordIds(msgs);
} catch (AppException e) {
......@@ -82,8 +82,8 @@ public class RecordInfoTest {
Assert.assertEquals(2, deleteRecordMap.get("tenant1:testindexer4:well:1.0.0").size());
}
@Test
public void should_return400_getDeleteRecordTest(List<RecordInfo> msgs, String errorMessage) {
private void should_return400_getDeleteRecordTest(List<RecordInfo> msgs, String errorMessage) {
try {
RecordInfo.getDeleteRecordIds(msgs);
fail("Should throw exception");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment