Added support for EiJob status
[nonrtric.git] / enrichment-coordinator-service / src / test / java / org / oransc / enrichment / controller / ConsumerSimulatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment.controller;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33 import lombok.Getter;
34
35 import org.oransc.enrichment.controllers.VoidResponse;
36 import org.oransc.enrichment.controllers.consumer.ConsumerEiJobStatus;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.PostMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RestController;
46
47 @RestController("ConsumerSimulatorController")
48 @Api(tags = {"Consumer Callbacks"})
49 public class ConsumerSimulatorController {
50
51     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52
53     public static class TestResults {
54
55         public List<ConsumerEiJobStatus> status = Collections.synchronizedList(new ArrayList<ConsumerEiJobStatus>());
56
57         public void reset() {
58             status.clear();
59         }
60     }
61
62     @Getter
63     private TestResults testResults = new TestResults();
64
65     public static String getJobStatusUrl(String eiJobId) {
66         return "/consumer_simulator/eijobs/" + eiJobId + "/status";
67     }
68
69     @PostMapping(path = "/consumer_simulator/eijobs/{eiJobId}/status", produces = MediaType.APPLICATION_JSON_VALUE)
70     @ApiOperation(value = "Callback for EI job status", notes = "")
71     @ApiResponses(
72         value = { //
73             @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)} //
74     )
75     public ResponseEntity<Object> jobStatusCallback( //
76         @PathVariable("eiJobId") String eiJobId, //
77         @RequestBody ConsumerEiJobStatus status) {
78         logger.info("Job status callback status: {} eiJobId: {}", status.state, eiJobId);
79         this.testResults.status.add(status);
80         return new ResponseEntity<>(HttpStatus.OK);
81     }
82
83 }