Skip to content

Commit 6c8330e

Browse files
author
Burak Serdar
committed
Fix #727: response returns the entity name:version
1 parent 8e5a063 commit 6c8330e

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

core-api/src/main/java/com/redhat/lightblue/Response.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class Response extends JsonObject {
3838

3939
private static final long serialVersionUID = 1L;
4040

41+
private static final String PROPERTY_ENTITY = "entity";
42+
private static final String PROPERTY_VERSION = "entityVersion";
4143
private static final String PROPERTY_STATUS = "status";
4244
private static final String PROPERTY_MOD_COUNT = "modifiedCount";
4345
private static final String PROPERTY_MATCH_COUNT = "matchCount";
@@ -48,6 +50,7 @@ public class Response extends JsonObject {
4850
private static final String PROPERTY_ERRORS = "errors";
4951
private static final String PROPERTY_HOSTNAME = "hostname";
5052

53+
private EntityVersion entity;
5154
private OperationStatus status;
5255
private long modifiedCount;
5356
private long matchCount;
@@ -88,6 +91,18 @@ public Response(JsonNodeFactory jsonNodeFactory) {
8891
this.hostname = HOSTNAME;
8992
}
9093

94+
public EntityVersion getEntity() {
95+
return entity;
96+
}
97+
98+
public void setEntity(EntityVersion e) {
99+
entity=e;
100+
}
101+
102+
public void setEntity(String entityName,String version) {
103+
this.entity=new EntityVersion(entityName,version);
104+
}
105+
91106
/**
92107
* Status of the completed operation
93108
*/
@@ -213,6 +228,10 @@ public List<Error> getErrors() {
213228
@Override
214229
public JsonNode toJson() {
215230
JsonNodeBuilder builder = new JsonNodeBuilder();
231+
if(entity!=null) {
232+
builder.add(PROPERTY_ENTITY,entity.getEntity());
233+
builder.add(PROPERTY_VERSION,entity.getVersion());
234+
}
216235
builder.add(PROPERTY_STATUS, status);
217236
builder.add(PROPERTY_MOD_COUNT, modifiedCount);
218237
builder.add(PROPERTY_MATCH_COUNT, matchCount);

crud/src/main/java/com/redhat/lightblue/mediator/Mediator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public Response insert(InsertionRequest req) {
115115
Response response = new Response(factory.getNodeFactory());
116116
try {
117117
OperationContext ctx = newCtx(req, CRUDOperation.INSERT);
118+
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
118119
EntityMetadata md = ctx.getTopLevelEntityMetadata();
119120
if (!md.getAccess().getInsert().hasAccess(ctx.getCallerRoles())) {
120121
ctx.setStatus(OperationStatus.ERROR);
@@ -182,6 +183,7 @@ public Response save(SaveRequest req) {
182183
Response response = new Response(factory.getNodeFactory());
183184
try {
184185
OperationContext ctx = newCtx(req, CRUDOperation.SAVE);
186+
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
185187
EntityMetadata md = ctx.getTopLevelEntityMetadata();
186188
if (!md.getAccess().getUpdate().hasAccess(ctx.getCallerRoles())
187189
|| (req.isUpsert() && !md.getAccess().getInsert().hasAccess(ctx.getCallerRoles()))) {
@@ -249,6 +251,7 @@ public Response update(UpdateRequest req) {
249251
Response response = new Response(factory.getNodeFactory());
250252
try {
251253
OperationContext ctx = newCtx(req, CRUDOperation.UPDATE);
254+
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
252255
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
253256
if (!md.getAccess().getUpdate().hasAccess(ctx.getCallerRoles())) {
254257
ctx.setStatus(OperationStatus.ERROR);
@@ -317,6 +320,7 @@ public Response delete(DeleteRequest req) {
317320
Response response = new Response(factory.getNodeFactory());
318321
try {
319322
OperationContext ctx = newCtx(req, CRUDOperation.DELETE);
323+
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
320324
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
321325
if (!md.getAccess().getDelete().hasAccess(ctx.getCallerRoles())) {
322326
ctx.setStatus(OperationStatus.ERROR);
@@ -439,6 +443,7 @@ public Response find(FindRequest req) {
439443
response.setStatus(OperationStatus.ERROR);
440444
try {
441445
OperationContext ctx = newCtx(req, CRUDOperation.FIND);
446+
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
442447
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
443448
if (!md.getAccess().getFind().hasAccess(ctx.getCallerRoles())) {
444449
ctx.setStatus(OperationStatus.ERROR);
@@ -513,6 +518,7 @@ public Response explain(FindRequest req) {
513518
response.setStatus(OperationStatus.ERROR);
514519
try {
515520
OperationContext ctx = newCtx(req, CRUDOperation.FIND);
521+
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
516522
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
517523
Finder finder;
518524
if (ctx.isSimple()) {

crud/src/test/java/com/redhat/lightblue/mediator/MediatorTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void insertDocWithRequiredFieldNull() throws Exception {
109109
mdManager.md = getMd("./termsmd.json");
110110

111111
InsertionRequest req = new InsertionRequest();
112-
req.setEntityVersion(new EntityVersion("terms", "0.14.0-SNAPSHOT"));
112+
req.setEntityVersion(new EntityVersion("terms", "0.14.1-SNAPSHOT"));
113113
req.setEntityData(loadJsonNode("./termsdata.json"));
114114
req.setReturnFields(null);
115115
req.setClientId(new RestClientIdentification(Arrays.asList("test.field1-insert", "test-insert")));
@@ -118,6 +118,9 @@ public void insertDocWithRequiredFieldNull() throws Exception {
118118

119119
Response response = mediator.insert(req);
120120
// there should be no errors
121+
// Response should return the entity name:version
122+
Assert.assertEquals("terms",response.getEntity().getEntity());
123+
Assert.assertEquals("0.14.1-SNAPSHOT",response.getEntity().getVersion());
121124
}
122125

123126
@Test
@@ -247,6 +250,9 @@ public void deleteRoleTest() throws Exception {
247250
Assert.assertEquals(0, response.getMatchCount());
248251
Assert.assertEquals(0, response.getDataErrors().size());
249252
Assert.assertEquals(0, response.getErrors().size());
253+
// Response should return the entity name:version
254+
Assert.assertEquals("test",response.getEntity().getEntity());
255+
Assert.assertEquals("1.0",response.getEntity().getVersion());
250256
}
251257

252258
@Test
@@ -289,6 +295,9 @@ public void findRoleTest() throws Exception {
289295
Assert.assertEquals(0, response.getMatchCount());
290296
Assert.assertEquals(0, response.getDataErrors().size());
291297
Assert.assertEquals(0, response.getErrors().size());
298+
// Response should return the entity name:version
299+
Assert.assertEquals("test",response.getEntity().getEntity());
300+
Assert.assertEquals("1.0",response.getEntity().getVersion());
292301
}
293302

294303
@Test

0 commit comments

Comments
 (0)