Skip to content

Commit

Permalink
Merge pull request #174 from mbarto/transactional_insert_resource
Browse files Browse the repository at this point in the history
Fixes #173: transactional resource insert
  • Loading branch information
mbarto authored Mar 19, 2018
2 parents 7519ad3 + 4285d0b commit efef5b8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,41 +192,47 @@ public long insert(Resource resource) throws BadRequestServiceEx, NotFoundServic
} catch (DataIntegrityViolationException exc) {
throw new BadRequestServiceEx(exc.getLocalizedMessage());
}

//
// Persisting Attributes
//
List<Attribute> attributes = resource.getAttribute();
if (attributes != null) {
for (Attribute a : attributes) {
a.setResource(r);
attributeDAO.persist(a);
try {
//
// Persisting Attributes
//
List<Attribute> attributes = resource.getAttribute();
if (attributes != null) {
for (Attribute a : attributes) {
a.setResource(r);
attributeDAO.persist(a);
}
}
}

//
// Persisting StoredData
//
StoredData data = resource.getData();
if (data != null) {
data.setId(r.getId());
data.setResource(r);
storedDataDAO.persist(data);
}
//
// Persisting StoredData
//
StoredData data = resource.getData();
if (data != null) {
data.setId(r.getId());
data.setResource(r);
storedDataDAO.persist(data);
}

//
// Persisting SecurityRule
//
List<SecurityRule> rules = resource.getSecurity();
//
// Persisting SecurityRule
//
List<SecurityRule> rules = resource.getSecurity();

if (rules != null) {
for (SecurityRule rule : rules) {
rule.setResource(r);
securityDAO.persist(rule);
if (rules != null) {
for (SecurityRule rule : rules) {
rule.setResource(r);
securityDAO.persist(rule);
}
}
}

return r.getId();
return r.getId();
} catch(Exception e) {
// remove resource if we cannot persist other objects bound to the resource
// (attributes, data, etc.)
delete(r.getId());
throw e;
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,31 @@ public void testUpdateSecurityRules() throws Exception {
assertEquals(3, writtenRules.size());
}

@Test
public void testInsertTooBigResource() throws Exception {
final String ORIG_RES_NAME = "testRes";
final String DESCRIPTION = "description";
final String CATEGORY_NAME = "MAP";
String bigData = createDataSize(10000000);
boolean error = false;
assertEquals(0, resourceService.getCount(null));
try {
createResource(ORIG_RES_NAME, DESCRIPTION, CATEGORY_NAME, bigData);
} catch(Exception e) {
error = true;
}
assertEquals(0, resourceService.getCount(null));
assertTrue(error);
}

private static String createDataSize(int msgSize) {
StringBuilder sb = new StringBuilder(msgSize);
for (int i = 0; i < msgSize; i++) {
sb.append('a');
}
return sb.toString();
}

@Test
public void testInsertUpdateDuplicatedResource() throws Exception {
final String ORIG_RES_NAME = "testRes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ protected long createResource(String name, String description, String catName) t
return resourceService.insert(resource);
}

/**
* @param name
* @param creation
* @param description
* @param data
*
* @return long
* @throws Exception
*/
protected long createResource(String name, String description, String catName, String data) throws Exception {

Category category = new Category();
category.setName(catName);

categoryService.insert(category);

Resource resource = new Resource();
resource.setName(name);
resource.setDescription(description);
resource.setCategory(category);
StoredData storedData = new StoredData();
storedData.setData(data);
resource.setData(storedData);

return resourceService.insert(resource);
}

/**
* @param name
* @param creation
Expand Down

0 comments on commit efef5b8

Please sign in to comment.