Skip to content
Snippets Groups Projects
Commit b44aa419 authored by Kishore Battula's avatar Kishore Battula
Browse files

Merge branch 'haaggarw/AddressingMrComments' into 'master'

Addressing review comments

See merge request !9
parents a0c8b289 b54251f7
No related branches found
No related tags found
1 merge request!9Addressing review comments
Pipeline #4798 failed
......@@ -18,6 +18,7 @@ import com.azure.cosmos.FeedOptions;
import com.azure.cosmos.SqlParameter;
import com.azure.cosmos.SqlParameterList;
import com.azure.cosmos.SqlQuerySpec;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.action.model.Action;
import org.opengroup.osdu.register.provider.interfaces.action.IActionRepo;
......@@ -47,17 +48,22 @@ public class ActionRepository implements IActionRepo {
@Autowired
private String cosmosDBName;
@Autowired
private JaxRsDpsLog log;
@Override
public Action createAction(Action action) {
if(action.getId() == null){
log.error("Action id cannot be null");
throw new AppException(400, "Bad Request", "Action id cannot be null");
}
try {
mutex.lock();
if (exists(action.getId())) {
throwConflict();
log.error(String.format("An action already exists with the id: %s", action.getId()));
throw new AppException(409, "Conflict", String.format("An action already exists with the id: %s", action.getId()));
}
ActionDoc doc = new ActionDoc(action, headers.getPartitionId());
......@@ -118,8 +124,4 @@ public class ActionRepository implements IActionRepo {
return action;
}
private void throwConflict() {
throw new AppException(409, "Conflict", "An action already exists with the same message and endpoint combination");
}
}
......@@ -20,6 +20,7 @@ import com.azure.cosmos.SqlParameterList;
import com.azure.cosmos.SqlQuerySpec;
import com.google.cloud.Timestamp;
import org.opengroup.osdu.azure.CosmosStore;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.ddms.model.Ddms;
import org.opengroup.osdu.register.ddms.model.RegisteredInterface;
......@@ -49,6 +50,9 @@ public class DdmsRepository implements IDdmsRepository {
@Autowired
private String cosmosDBName;
@Autowired
private JaxRsDpsLog log;
@Override
public Ddms create(Ddms ddms) {
......@@ -56,9 +60,9 @@ public class DdmsRepository implements IDdmsRepository {
mutex.lock();
if(exists(ddms.getId())){
throw new AppException(409, "Conflict", "A DDMS already exists with the same id");
log.error(String.format("A DDMS already exists with the same id: %s", ddms.getId()));
throw new AppException(409, "Conflict", String.format("A DDMS already exists with the same id: %s", ddms.getId()));
}
// TODO Size check for Ddms schema [Maximum Size for a CosmosDb item is 2MB]
DdmsDoc doc = new DdmsDoc(ddms, headers.getPartitionId());
cosmosStore.upsertItem(headers.getPartitionId(), cosmosDBName, ddmsContainer, doc);
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.register.provider.azure.Util;
package org.opengroup.osdu.register.provider.azure.util;
import lombok.SneakyThrows;
import org.opengroup.osdu.register.utils.IGoogleServiceAccount;
......
......@@ -21,6 +21,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.CosmosStore;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.action.model.Action;
......@@ -36,7 +37,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.lenient;
......@@ -55,6 +56,9 @@ public class ActionRepositoryTest {
@Mock
private CosmosStore cosmosStore;
@Mock
private JaxRsDpsLog log;
@InjectMocks
private ActionRepository repo;
......@@ -81,7 +85,7 @@ public class ActionRepositoryTest {
Action action = createAction();
Optional<ActionDoc> cosmosItem = Optional.of(new ActionDoc());
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(actionId), eq(dataPartitionId), any());
AppException exception = assertThrows(AppException.class, () -> {
repo.createAction(action);
......@@ -89,7 +93,7 @@ public class ActionRepositoryTest {
assertEquals(409, exception.getError().getCode());
assertEquals("Conflict", exception.getError().getReason());
assertEquals("An action already exists with the same message and endpoint combination", exception.getError().getMessage());
assertEquals(String.format("An action already exists with the id: %s", action.getId()), exception.getError().getMessage());
verify(cosmosStore, times(1)).findItem(dataPartitionId, null, null, actionId, dataPartitionId, ActionDoc.class);
}
......@@ -98,8 +102,8 @@ public class ActionRepositoryTest {
Action action = createAction();
Optional<ActionDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doNothing().when(cosmosStore).upsertItem(anyString(), any(), any(), any());
doReturn(cosmosItem).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(actionId), eq(dataPartitionId), any());
doNothing().when(cosmosStore).upsertItem(eq(dataPartitionId), any(), any(), any());
Action output = repo.createAction(action);
......@@ -110,7 +114,7 @@ public class ActionRepositoryTest {
@Test
public void getAction_throws404_whenIdNotFound() {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.empty()).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(actionId), eq(dataPartitionId), any());
AppException exception = assertThrows(AppException.class, () -> {
repo.get(actionId);
......@@ -128,7 +132,7 @@ public class ActionRepositoryTest {
Action action = createAction();
ActionDoc doc = new ActionDoc(action, dataPartitionId);
doReturn(Optional.of(doc)).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.of(doc)).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(actionId), eq(dataPartitionId), any());
Action output = repo.get(actionId);
......@@ -139,7 +143,7 @@ public class ActionRepositoryTest {
@Test
public void deleteAction_returnsFalse_whenIdNotFound() {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.empty()).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(actionId), eq(dataPartitionId), any());
boolean output = repo.delete(actionId);
......@@ -154,7 +158,7 @@ public class ActionRepositoryTest {
ActionDoc doc = new ActionDoc(action, dataPartitionId);
Optional<ActionDoc> cosmosItem = Optional.of(doc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(actionId), eq(dataPartitionId), any());
boolean output = repo.delete(actionId);
......@@ -176,7 +180,7 @@ public class ActionRepositoryTest {
List<Action> actions = Stream.of(action1, action2).collect(Collectors.toList());
List<ActionDoc> actionDocs = Stream.of(doc1, doc2).collect(Collectors.toList());
doReturn(actionDocs).when(cosmosStore).queryItems(anyString(), any(), any(), any(), any(), any());
doReturn(actionDocs).when(cosmosStore).queryItems(eq(dataPartitionId), any(), any(), any(), any(), any());
List<Action> output = repo.getAllActions();
......
......@@ -21,6 +21,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.CosmosStore;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.ddms.model.Ddms;
......@@ -37,7 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.lenient;
......@@ -56,6 +57,9 @@ public class DdmsRepositoryTest {
@Mock
private CosmosStore cosmosStore;
@Mock
private JaxRsDpsLog log;
@InjectMocks
private DdmsRepository repo;
......@@ -69,7 +73,7 @@ public class DdmsRepositoryTest {
Ddms ddms = createDdms();
Optional<DdmsDoc> cosmosItem = Optional.of(new DdmsDoc());
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(ddmsId), eq(dataPartitionId), any());
AppException exception = assertThrows(AppException.class, () -> {
repo.create(ddms);
......@@ -77,7 +81,7 @@ public class DdmsRepositoryTest {
assertEquals(409, exception.getError().getCode());
assertEquals("Conflict", exception.getError().getReason());
assertEquals("A DDMS already exists with the same id", exception.getError().getMessage());
assertEquals(String.format("A DDMS already exists with the same id: %s", ddms.getId()), exception.getError().getMessage());
verify(cosmosStore, times(1)).findItem(dataPartitionId, null, null, ddmsId, dataPartitionId, DdmsDoc.class);
}
......@@ -86,8 +90,8 @@ public class DdmsRepositoryTest {
Ddms ddms = createDdms();
Optional<DdmsDoc> cosmosItem = Optional.empty();
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doNothing().when(cosmosStore).upsertItem(anyString(), any(), any(), any());
doReturn(cosmosItem).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(ddmsId), eq(dataPartitionId), any());
doNothing().when(cosmosStore).upsertItem(eq(dataPartitionId), any(), any(), any());
Ddms output = repo.create(ddms);
......@@ -98,7 +102,7 @@ public class DdmsRepositoryTest {
@Test
public void getDdms_throws404_whenIdNotFound() {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.empty()).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(ddmsId), eq(dataPartitionId), any());
AppException exception = assertThrows(AppException.class, () -> {
repo.get(ddmsId);
......@@ -116,7 +120,7 @@ public class DdmsRepositoryTest {
Ddms ddms = createDdms();
DdmsDoc doc = new DdmsDoc(ddms, dataPartitionId);
doReturn(Optional.of(doc)).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.of(doc)).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(ddmsId), eq(dataPartitionId), any());
Ddms output = repo.get(ddmsId);
......@@ -127,7 +131,7 @@ public class DdmsRepositoryTest {
@Test
public void deleteDdms_returnsFalse_whenIdNotFound() {
doReturn(Optional.empty()).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(Optional.empty()).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(ddmsId), eq(dataPartitionId), any());
boolean output = repo.delete(ddmsId);
......@@ -142,7 +146,7 @@ public class DdmsRepositoryTest {
DdmsDoc doc = new DdmsDoc(ddms, dataPartitionId);
Optional<DdmsDoc> cosmosItem = Optional.of(doc);
doReturn(cosmosItem).when(cosmosStore).findItem(anyString(), any(), any(), anyString(), anyString(), any());
doReturn(cosmosItem).when(cosmosStore).findItem(eq(dataPartitionId), any(), any(), eq(ddmsId), eq(dataPartitionId), any());
boolean output = repo.delete(ddmsId);
......@@ -164,7 +168,7 @@ public class DdmsRepositoryTest {
List<Ddms> ddmsList = Stream.of(ddms1, ddms2).collect(Collectors.toList());
List<DdmsDoc> ddmsDocList = Stream.of(doc1, doc2).collect(Collectors.toList());
doReturn(ddmsDocList).when(cosmosStore).queryItems(anyString(), any(), any(), any(), any(), any());
doReturn(ddmsDocList).when(cosmosStore).queryItems(eq(dataPartitionId), any(), any(), any(), any(), any());
List<Ddms> output = repo.query("type");
......
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