use of org.phoebus.olog.entity.Logbook in project phoebus-olog by Olog.
the class LogbookRepository method deleteById.
@Override
public void deleteById(String logbookName) {
try {
UpdateResponse response = client.update(new UpdateRequest(ES_LOGBOOK_INDEX, ES_LOGBOOK_TYPE, logbookName).doc(jsonBuilder().startObject().field("state", State.Inactive).endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
if (response.getResult().equals(Result.UPDATED)) {
BytesReference ref = client.get(new GetRequest(ES_LOGBOOK_INDEX, ES_LOGBOOK_TYPE, response.getId()), RequestOptions.DEFAULT).getSourceAsBytesRef();
Logbook deletedLogbook = mapper.readValue(ref.streamInput(), Logbook.class);
logger.log(Level.INFO, "Deleted logbook " + deletedLogbook.toLogger());
}
} catch (DocumentMissingException e) {
logger.log(Level.SEVERE, "Failed to delete logbook: " + logbookName + " because it does not exist", e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Failed to delete logbook: " + logbookName + " because it does not exist");
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to delete logbook: " + logbookName, e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete logbook: " + logbookName);
}
}
use of org.phoebus.olog.entity.Logbook in project phoebus-olog by Olog.
the class LogbookRepository method save.
@Override
public <S extends Logbook> S save(S logbook) {
try {
IndexRequest indexRequest = new IndexRequest(ES_LOGBOOK_INDEX, ES_LOGBOOK_TYPE, logbook.getName()).source(mapper.writeValueAsBytes(logbook), XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
if (response.getResult().equals(Result.CREATED) || response.getResult().equals(Result.UPDATED)) {
BytesReference ref = client.get(new GetRequest(ES_LOGBOOK_INDEX, ES_LOGBOOK_TYPE, response.getId()), RequestOptions.DEFAULT).getSourceAsBytesRef();
Logbook createdLogbook = mapper.readValue(ref.streamInput(), Logbook.class);
return (S) createdLogbook;
}
return null;
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to create logbook: " + logbook, e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create logbook: " + logbook);
}
}
use of org.phoebus.olog.entity.Logbook in project phoebus-olog by Olog.
the class LogResourceTest method init.
@Before
public void init() {
logbook1 = new Logbook("name1", "user");
logbook2 = new Logbook("name2", "user");
tag1 = new Tag("tag1");
tag2 = new Tag("tag2");
log1 = LogBuilder.createLog().id(1L).owner("owner").title("title").withLogbooks(Set.of(logbook1, logbook2)).description("description1").withTags(Set.of(tag1, tag2)).createDate(now).level("Urgent").build();
log2 = LogBuilder.createLog().id(2L).owner("user").withLogbooks(Set.of(logbook1, logbook2)).description("description2").createDate(now).level("Urgent").build();
}
use of org.phoebus.olog.entity.Logbook in project phoebus-olog by Olog.
the class LogbookResourceTest method testUpdateLogbooks.
@Test
public void testUpdateLogbooks() throws Exception {
List<Logbook> logbooks = Arrays.asList(logbook1, logbook2);
when(logbookRepository.saveAll(logbooks)).thenReturn(logbooks);
MockHttpServletRequestBuilder request = put("/" + OlogResourceDescriptors.LOGBOOK_RESOURCE_URI).header(HttpHeaders.AUTHORIZATION, AUTHORIZATION).content(objectMapper.writeValueAsString(logbooks)).contentType(JSON);
MvcResult result = mockMvc.perform(request).andExpect(status().isOk()).andReturn();
logbooks = objectMapper.readValue(result.getResponse().getContentAsString(), new TypeReference<List<Logbook>>() {
});
assertEquals("name1", logbooks.iterator().next().getName());
verify(logbookRepository, times(1)).saveAll(logbooks);
reset(logbookRepository);
}
use of org.phoebus.olog.entity.Logbook in project phoebus-olog by Olog.
the class OlogLogbooksIT method handleLogbookCreateCheck.
/**
* Test {@link gov.bnl.log.OlogResourceDescriptors#LOGBOOK_RESOURCE_URI}.
*/
@Test
public void handleLogbookCreateCheck() {
// what
// check(s) for create logbook
// e.g.
// user without required role
// content
// json - incomplete
// name - null, empty
// owner - null, empty
// state - null (empty, incorrect value (ok: Active, Inactive))
// --------------------------------------------------------------------------------
// Retrieve a Logbook
// x List Logbooks
// x Create a Logbook
// Create Logbooks
// Remove Logbook
Logbook logbook_check = new Logbook();
ObjectMapper mapper = new ObjectMapper();
try {
String[] response = ITUtil.doGetJson(HTTP_IP_PORT_OLOG_LOGBOOKS);
ITUtil.assertResponseLength2CodeOKContent(response, ITUtil.EMPTY_JSON);
// response = ITUtil.runShellCommand(createCurlLogbookForUser("l1", mapper.writeValueAsString(logbook_l1_owner_a_state_a)));
// ITUtil.assertResponseLength2Code(HttpURLConnection.HTTP_UNAUTHORIZED, response);
response = ITUtil.runShellCommand(createCurlLogbookForUser("asdf", mapper.writeValueAsString(logbook_check)));
ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
response = ITUtil.runShellCommand(createCurlLogbookForAdmin("asdf", mapper.writeValueAsString(logbook_check)));
ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
logbook_check.setName(null);
response = ITUtil.runShellCommand(createCurlLogbookForAdmin("asdf", mapper.writeValueAsString(logbook_check)));
ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
logbook_check.setName("");
response = ITUtil.runShellCommand(createCurlLogbookForAdmin("asdf", mapper.writeValueAsString(logbook_check)));
ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
response = ITUtil.doGetJson(HTTP_IP_PORT_OLOG_LOGBOOKS);
ITUtil.assertResponseLength2CodeOKContent(response, ITUtil.EMPTY_JSON);
} catch (IOException e) {
fail();
} catch (InterruptedException e) {
fail();
} catch (Exception e) {
fail();
}
}
Aggregations