Search in sources :

Example 1 with Logbook

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);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Logbook(org.phoebus.olog.entity.Logbook) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException)

Example 2 with Logbook

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);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Logbook(org.phoebus.olog.entity.Logbook) IndexResponse(org.elasticsearch.action.index.IndexResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException)

Example 3 with 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();
}
Also used : Logbook(org.phoebus.olog.entity.Logbook) Tag(org.phoebus.olog.entity.Tag) Before(org.junit.Before)

Example 4 with Logbook

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);
}
Also used : Logbook(org.phoebus.olog.entity.Logbook) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 5 with Logbook

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();
    }
}
Also used : Logbook(org.phoebus.olog.entity.Logbook) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Logbook (org.phoebus.olog.entity.Logbook)23 IOException (java.io.IOException)11 Test (org.junit.Test)11 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)7 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 GetRequest (org.elasticsearch.action.get.GetRequest)4 DocumentMissingException (org.elasticsearch.index.engine.DocumentMissingException)4 Tag (org.phoebus.olog.entity.Tag)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ResponseStatusException (org.springframework.web.server.ResponseStatusException)4 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Before (org.junit.Before)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2