import java.io.IOException;
import javax.persistence.PersistenceException;
import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.EmptyResultDataAccessException;
}
}
+ /**
+ * Delete a JSON Object using the object identifier.
+ *
+ * @param jsonObjectId the JSON object identifier.
+ * @return a HTTP response.
+ */
+ @DELETE
+ @Path("json-object/{id}")
+ public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
+ try {
+ cpService.deleteJsonById(jsonObjectId);
+ return Response.status(Status.OK).entity(Status.OK.toString()).build();
+ } catch (final EmptyResultDataAccessException e) {
+ return Response.status(Status.NOT_FOUND).entity(Status.NOT_FOUND.toString()).build();
+ } catch (final Exception e) {
+ return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+ }
+ }
+
private static final void validateJsonStructure(final String jsonFile) {
final Gson gson = new Gson();
gson.fromJson(jsonFile, Object.class);
* @param jsonStructure the JSON data structure.
* @return the entity identifier.
*/
+ @Override
public final Integer storeJsonStructure(final String jsonStructure) {
final JsonDataEntity jsonDataEntity = new JsonDataEntity(jsonStructure);
dataRepository.save(jsonDataEntity);
return jsonDataEntity.getId();
}
- /**
+ /*
* Return the JSON structure from the database using the object identifier.
*
* @param jsonStructureId the JSON object identifier.
+ *
* @return the JSON structure from the database as a string.
*/
+ @Override
public final String getJsonById(final int jsonStructureId) {
return dataRepository.getOne(jsonStructureId).getJsonStructure();
}
+
+ /**
+ * Delete the JSON structure from the database using the object identifier.
+ *
+ * @param jsonStructureId the JSON object identifier.
+ */
+ @Override
+ public void deleteJsonById(int jsonStructureId) {
+ dataRepository.deleteById(jsonStructureId);
+ }
}
* @return the JSON structure.
*/
String getJsonById(final int jsonObjectId);
+
+ /**
+ * Delete a JSON Object using the object identifier.
+ *
+ * @param jsonObjectId the JSON object identifier.
+ */
+ void deleteJsonById(final int jsonObjectId);
}
@Override
public final SchemaContext parseAndValidateModel(final String yangModelContent) throws IOException,
- YangParserException {
+ YangParserException {
final File tempFile = File.createTempFile("yang", ".yang");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
writer.write(yangModelContent);
return dataPersistencyService.getJsonById(jsonObjectId);
}
+ @Override
+ public void deleteJsonById(int jsonObjectId) {
+ dataPersistencyService.deleteJsonById(jsonObjectId);;
+ }
+
@Override
public final void storeSchemaContext(final SchemaContext schemaContext) {
for (final Module module : schemaContext.getModules()) {
* @return a JSON Structure.
*/
String getJsonById(int jsonStructureId);
-}
+
+ /**
+ * Delete the JSON structure from the database using the entity identifier.
+ *
+ * @param jsonStructureId the json entity identifier.
+ */
+ void deleteJsonById(int jsonStructureId);
+}
\ No newline at end of file
then: 'the same exception is thrown by CPS'
thrown(IllegalStateException)
}
-}
+
+ def 'Delete a JSON object with a valid identifier'(){
+ given: 'that the data persistence service can delete a JSON structure for identifier 1'
+ mockDataPersistencyService.deleteJsonById(1)
+ expect: 'No exception is thrown when we delete a JSON structure with identifier 1'
+ objectUnderTest.deleteJsonById(1)
+ }
+
+ def 'Delete a JSON object with an identifier that does not exist'(){
+ given: 'that the data persistence service throws an exception'
+ mockDataPersistencyService.deleteJsonById(_) >> {throw new IllegalStateException()}
+ when: 'we try to delete a JSON structure'
+ objectUnderTest.deleteJsonById(100);
+ then: 'the same exception is thrown by CPS'
+ thrown(IllegalStateException)
+ }
+}
\ No newline at end of file