Search in sources :

Example 1 with LogEntry

use of org.opentosca.toscana.core.transformation.logging.LogEntry in project TOSCAna by StuPro-TOSCAna.

the class CsarControllerTest method setUp.

@Before
public void setUp() throws Exception {
    List<Csar> mockedCsars = new ArrayList<>();
    service = mock(CsarService.class);
    when(service.getCsars()).thenReturn(mockedCsars);
    when(service.getCsar(anyString())).thenReturn(Optional.empty());
    for (String name : MOCK_CSAR_NAMES) {
        LogEntry entry = new LogEntry(0, "TestContext", "Test Message", Level.DEBUG);
        Log mockLog = logMock();
        when(mockLog.getLogEntries(0)).thenReturn(Collections.singletonList(entry));
        Csar csar = spy(new CsarImpl(new File(""), name, mockLog));
        when(service.getCsar(name)).thenReturn(Optional.of(csar));
        mockedCsars.add(csar);
    }
    when(service.submitCsar(anyString(), any(InputStream.class))).thenAnswer(iom -> {
        // Check if the csar is already known
        if (service.getCsar(iom.getArguments()[0].toString()).isPresent()) {
            return null;
        }
        // Copy "sent" data into a byte array
        InputStream in = (InputStream) iom.getArguments()[1];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(in, out);
        dataRead = out.toByteArray();
        // Create Csar Mock
        return new CsarImpl(new File(""), iom.getArguments()[0].toString(), logMock());
    });
    CsarController controller = new CsarController(service);
    mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
Also used : Csar(org.opentosca.toscana.core.csar.Csar) Log(org.opentosca.toscana.core.transformation.logging.Log) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) CsarImpl(org.opentosca.toscana.core.csar.CsarImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CsarService(org.opentosca.toscana.core.csar.CsarService) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) File(java.io.File) LogEntry(org.opentosca.toscana.core.transformation.logging.LogEntry) Before(org.junit.Before)

Example 2 with LogEntry

use of org.opentosca.toscana.core.transformation.logging.LogEntry in project TOSCAna by StuPro-TOSCAna.

the class TransformationControllerTest method preInitNonCreationTests.

// </editor-fold>
// <editor-fold desc="Util Methods">
public List<Transformation> preInitNonCreationTests() throws PlatformNotFoundException {
    // add a transformation
    Optional<Csar> csar = csarService.getCsar(VALID_CSAR_NAME);
    assertTrue(csar.isPresent());
    String[] pnames = { VALID_PLATFORM_NAME, SECOND_VALID_PLATFORM_NAME };
    List<Transformation> transformations = new ArrayList<>();
    for (String pname : pnames) {
        LogEntry entry = new LogEntry(0, "Test Context", "Test Message", Level.DEBUG);
        Log mockLog = logMock();
        when(mockLog.getLogEntries(0)).thenReturn(Collections.singletonList(entry));
        Transformation transformation = new TransformationImpl(csar.get(), platformService.findPlatformById(pname).get(), mockLog, modelMock());
        transformation = spy(transformation);
        transformations.add(transformation);
        csar.get().getTransformations().put(pname, transformation);
    }
    return transformations;
}
Also used : Csar(org.opentosca.toscana.core.csar.Csar) TransformationImpl(org.opentosca.toscana.core.transformation.TransformationImpl) Transformation(org.opentosca.toscana.core.transformation.Transformation) Log(org.opentosca.toscana.core.transformation.logging.Log) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LogEntry(org.opentosca.toscana.core.transformation.logging.LogEntry)

Example 3 with LogEntry

use of org.opentosca.toscana.core.transformation.logging.LogEntry in project TOSCAna by StuPro-TOSCAna.

the class CsarController method getLogs.

/**
 *     Returns the logs from a given start index to the current end of the logger file. If the start index is higher then
 *     the current end index, a empty list is returned! The start parameter is a URL encoded parameter
 *     (<code>?start=0</code>)
 *     <p>
 *     Accessed with http call <code>GET /csars/{csar}/transformations/{platform}/logs</code>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a Json object containing the desired part of the logger for the transformation</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns a error message if the csar is not found or if the csar does not have a transformation for the given
 *     name (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@RequestMapping(path = "/{name}/logs", method = RequestMethod.GET, produces = "application/hal+json")
@ApiOperation(value = "Get the logs of a csar", tags = { "csars" }, notes = "Returns the logs for a csar, starting at a specific position. from the given start index all " + "following log lines get returned. If the start index is larger than the current last log index the operation " + "will return a empty list.")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully", response = LogResponse.class), @ApiResponse(code = 400, message = "The given start value is less than zero", response = RestErrorResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier", response = RestErrorResponse.class) })
public ResponseEntity<LogResponse> getLogs(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "name") String csarId, @ApiParam(value = "The index of the first log entry you want (0 returns the whole log)", required = true, example = "0") @RequestParam(name = "start", required = false, defaultValue = "0") Long start) {
    if (start < 0) {
        throw new IndexOutOfBoundsException("the start index has to be at least 0");
    }
    Csar csar = getCsarForName(csarId);
    Log log = csar.getLog();
    List<LogEntry> entries = log.getLogEntries(Math.toIntExact(start));
    return ResponseEntity.ok().body(new LogResponse(start, entries.size() == 0 ? start : start + entries.size() - 1, entries, csarId));
}
Also used : Csar(org.opentosca.toscana.core.csar.Csar) LogResponse(org.opentosca.toscana.api.model.LogResponse) Log(org.opentosca.toscana.core.transformation.logging.Log) LogEntry(org.opentosca.toscana.core.transformation.logging.LogEntry) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with LogEntry

use of org.opentosca.toscana.core.transformation.logging.LogEntry in project TOSCAna by StuPro-TOSCAna.

the class TransformationController method getTransformationLogs.

/**
 *     Returns the logs from a given start index to the current end of the logger file. If the start index is higher then
 *     the current end index, a empty list is returned! The start parameter is a URL encoded parameter
 *     (<code>?start=0</code>)
 *     <p>
 *     Accessed with http call <code>GET /csars/{csar}/transformations/{platform}/logs</code>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a Json object containing the desired part of the logger for the transformation</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns a error message if the csar is not found or if the csar does not have a transformation for the given
 *     name (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@RequestMapping(path = "/{platform}/logs", method = RequestMethod.GET, produces = "application/hal+json")
@ApiOperation(value = "Get the logs for a Transformation", tags = { "transformations" }, notes = "Returns the logs for a transformation, starting at a specific position. from the given start index all " + "following log lines get returned. If the start index is larger than the current last log index the operation " + "will return a empty list.")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully", response = LogResponse.class), @ApiResponse(code = 400, message = "The given start value is less than zero", response = RestErrorResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier or the CSAR does not have " + "a Transformation for the specified platform.", response = RestErrorResponse.class) })
public ResponseEntity<LogResponse> getTransformationLogs(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "csarId") String csarId, @ApiParam(value = "The identifier for the platform", required = true, example = "kubernetes") @PathVariable(name = "platform") String platformId, @ApiParam(value = "The index of the first log entry you want (0 returns the whole log)", required = true, example = "0") @RequestParam(name = "start", required = false, defaultValue = "0") Long start) {
    if (start < 0) {
        throw new IndexOutOfBoundsException("the start index has to be at least 0");
    }
    Csar csar = findByCsarId(csarId);
    Transformation transformation = findTransformationByPlatform(csar, platformId);
    Log log = transformation.getLog();
    List<LogEntry> entries = log.getLogEntries(Math.toIntExact(start));
    return ResponseEntity.ok().body(new LogResponse(start, // TODO Maybe move this calculation into the LogResponse Class
    entries.size() == 0 ? start : start + entries.size() - 1, entries, platformId, csarId));
}
Also used : Csar(org.opentosca.toscana.core.csar.Csar) Transformation(org.opentosca.toscana.core.transformation.Transformation) LogResponse(org.opentosca.toscana.api.model.LogResponse) Log(org.opentosca.toscana.core.transformation.logging.Log) LogEntry(org.opentosca.toscana.core.transformation.logging.LogEntry) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Csar (org.opentosca.toscana.core.csar.Csar)4 Log (org.opentosca.toscana.core.transformation.logging.Log)4 LogEntry (org.opentosca.toscana.core.transformation.logging.LogEntry)4 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 LogResponse (org.opentosca.toscana.api.model.LogResponse)2 Transformation (org.opentosca.toscana.core.transformation.Transformation)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Before (org.junit.Before)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Mockito.anyString (org.mockito.Mockito.anyString)1 CsarImpl (org.opentosca.toscana.core.csar.CsarImpl)1 CsarService (org.opentosca.toscana.core.csar.CsarService)1 TransformationImpl (org.opentosca.toscana.core.transformation.TransformationImpl)1 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)1