Add 'unpublished' REST interface for DataJobs Write to allow K6 testing 04/140604/8
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Thu, 27 Mar 2025 10:15:46 +0000 (10:15 +0000)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Wed, 2 Apr 2025 15:26:21 +0000 (16:26 +0100)
- Created a new hidden REST endpoint to use write job for internal
  testing like KPI and endurence.

Issue-ID: CPS-2718
Change-Id: I1c4c7bc145dd522d344bd98ea04f05068b3e43eb
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/DataJobControllerForTest.java [new file with mode: 0644]
cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/DataJobControllerForTestSpec.groovy [new file with mode: 0644]
cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyRestExceptionHandlerSpec.groovy
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DataJobRequest.java [new file with mode: 0644]

diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/DataJobControllerForTest.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/DataJobControllerForTest.java
new file mode 100644 (file)
index 0000000..d259d91
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.rest.controller;
+
+import io.swagger.v3.oas.annotations.Hidden;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.ncmp.api.datajobs.DataJobService;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest;
+import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteResponse;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Controller responsible for handling data job write operations.
+ * This class exposes an API endpoint that accepts a write request for a data job and processes it.
+ */
+@Slf4j
+@RestController
+@RequestMapping("/do-not-use/dataJobs")
+@RequiredArgsConstructor
+public class DataJobControllerForTest {
+
+    private final DataJobService dataJobService;
+
+    /**
+     * Handles POST requests to write a data job. This endpoint is unsupported and intended for testing purposes only.
+     * This internal endpoint processes a data job write request by extracting necessary metadata and data
+     * from the request body and delegating the operation to the {@link DataJobService}.
+     * <p><b>Note:</b> The {@link DataJobRequest} parameter is created and used for testing purposes only.
+     * In a production environment, data job write operations are not triggered through internal workflows.</p>
+     *
+     * @param authorization  The optional authorization token sent in the request header.
+     * @param dataJobId      The unique identifier for the data job, extracted from the URL path.
+     * @param dataJobRequest The request payload containing metadata and data for the data job write operation.
+     * @return A {@link ResponseEntity} containing a list of {@link SubJobWriteResponse} objects representing the
+     *     status of each sub-job within the data job, or an error response with an appropriate HTTP status code.
+     */
+    @PostMapping("/{dataJobId}/write")
+    @Hidden
+    public ResponseEntity<List<SubJobWriteResponse>> writeDataJob(@RequestHeader(value = "Authorization",
+                                                                          required = false) final String authorization,
+                                                                  @PathVariable("dataJobId") final String dataJobId,
+                                                                  @RequestBody final DataJobRequest dataJobRequest) {
+        log.info("Internal API: writeDataJob invoked for {}", dataJobId);
+        final DataJobMetadata dataJobMetadata = dataJobRequest.dataJobMetadata();
+        final DataJobWriteRequest dataJobWriteRequest = dataJobRequest.dataJobWriteRequest();
+        final List<SubJobWriteResponse> subJobWriteResponses = dataJobService.writeDataJob(authorization, dataJobId,
+                dataJobMetadata, dataJobWriteRequest);
+        return ResponseEntity.ok(subJobWriteResponses);
+    }
+}
+
diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/DataJobControllerForTestSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/DataJobControllerForTestSpec.groovy
new file mode 100644 (file)
index 0000000..6fc4a69
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.rest.controller
+
+import org.onap.cps.ncmp.api.datajobs.DataJobService
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata
+import org.onap.cps.ncmp.api.datajobs.models.DataJobRequest
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest
+import org.onap.cps.ncmp.api.datajobs.models.WriteOperation
+import org.springframework.http.HttpStatus
+import spock.lang.Specification
+
+class DataJobControllerForTestSpec extends Specification {
+
+    DataJobService mockDataJobService = Mock()
+
+    def objectUnderTest = new DataJobControllerForTest(mockDataJobService)
+
+    def 'Write Data Job request'() {
+        given: 'a valid datajob write request'
+            def dataJobMetadata = new DataJobMetadata('some destination', 'some accept type', 'some content type')
+            def writeOperations = [ new WriteOperation('/path/to/node', 'create', 'op123', 'value1') ]
+            def dataJobWriteRequest = new DataJobWriteRequest(writeOperations)
+            def dataJobRequest = new DataJobRequest(dataJobMetadata, dataJobWriteRequest)
+        when: 'write data job is called'
+            def result = objectUnderTest.writeDataJob('my authorization', 'my job', dataJobRequest)
+        then: 'response is 200 OK'
+            assert result.statusCode == HttpStatus.OK
+        and: 'the service method is called once with expected parameters'
+            1 * mockDataJobService.writeDataJob('my authorization', 'my job', dataJobMetadata, dataJobWriteRequest)
+    }
+}
index aad04a1..3a9a0bb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 highstreet technologies GmbH
- *  Modifications Copyright (C) 2021-2024 Nordix Foundation
+ *  Modifications Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -102,6 +102,9 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
     @SpringBean
     NcmpPassthroughResourceRequestHandler StubbedNcmpPassthroughResourceRequestHandler = Stub()
 
+    @SpringBean
+    DataJobControllerForTest stubbedDataJobControllerForTest = Stub()
+
     @Value('${rest.api.ncmp-base-path}')
     def basePathNcmp
 
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DataJobRequest.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DataJobRequest.java
new file mode 100644 (file)
index 0000000..fe73a60
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.api.datajobs.models;
+
+public record DataJobRequest(DataJobMetadata dataJobMetadata, DataJobWriteRequest dataJobWriteRequest) {
+}