Passing userId to upload policy 00/111100/3
authorKrupaNagabhushan <krupa.nagabhushan@est.tech>
Mon, 10 Aug 2020 17:42:48 +0000 (18:42 +0100)
committerVasyl Razinkov <vasyl.razinkov@est.tech>
Mon, 10 Aug 2020 18:08:46 +0000 (18:08 +0000)
Issue-ID: POLICY-2751
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
Change-Id: Ifc91bba369161a63f2ad759dcdc014bb12837e88

gui-editors/gui-editor-apex/src/main/java/org/onap/policy/gui/editors/apex/rest/handling/ApexEditorRestResource.java
gui-editors/gui-editor-apex/src/main/java/org/onap/policy/gui/editors/apex/rest/handling/PolicyUploadHandler.java
gui-editors/gui-editor-apex/src/main/resources/webapp/index.html
gui-editors/gui-editor-apex/src/main/resources/webapp/js/ApexUser.js [new file with mode: 0644]
gui-editors/gui-editor-apex/src/main/resources/webapp/upload/dialog.html
gui-editors/gui-editor-apex/src/test/java/org/onap/policy/gui/editors/apex/rest/handling/converter/tosca/PolicyUploadHandlerTest.java

index 33aa04d..3083c4a 100644 (file)
@@ -74,7 +74,6 @@ import org.slf4j.ext.XLoggerFactory;
 public class ApexEditorRestResource implements RestCommandHandler {
 
     // Get a reference to the logger
-
     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorRestResource.class);
     // Location of the periodi event template
 
@@ -262,13 +261,15 @@ public class ApexEditorRestResource implements RestCommandHandler {
     @Path("Model/Upload")
     @Consumes({MediaType.MULTIPART_FORM_DATA})
     public ApexApiResult uploadModel(@FormDataParam("tosca-template-file") InputStream toscaTemplateFileStream,
-                                     @FormDataParam("apex-config-file") InputStream apexConfigFileStream) {
+           @FormDataParam("apex-config-file") InputStream apexConfigFileStream,
+           @FormDataParam("userId") String userId) {
         final ApexApiResult result = new ApexApiResult();
         final RestSession session = SESSION_HANDLER.getSession(sessionId, result);
         if (session == null) {
             return result;
         }
-        return policyUploadHandler.doUpload(session.getApexModel(), toscaTemplateFileStream, apexConfigFileStream);
+        return policyUploadHandler.doUpload(session.getApexModel(), toscaTemplateFileStream,
+                                            apexConfigFileStream, userId);
     }
 
     /**
@@ -295,7 +296,7 @@ public class ApexEditorRestResource implements RestCommandHandler {
     @GET
     @Path("KeyInformation/Get")
     public ApexApiResult listKeyInformation(@QueryParam(NAME) final String name,
-            @QueryParam(VERSION) final String version) {
+           @QueryParam(VERSION) final String version) {
         return processRestCommand(RestCommandType.KEY_INFO, RestCommand.LIST, name, version);
     }
 
@@ -587,7 +588,6 @@ public class ApexEditorRestResource implements RestCommandHandler {
         return processRestCommand(RestCommandType.TASK, RestCommand.VALIDATE, name, version);
     }
 
-    // CHECKSTYLE:OFF: MethodLength
     /**
      * Creates a policy with the information in the JSON string passed.
      *
@@ -741,7 +741,7 @@ public class ApexEditorRestResource implements RestCommandHandler {
      */
     @Override
     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
-            final RestCommand command) {
+           final RestCommand command) {
         switch (commandType) {
             case MODEL:
                 return MODEL_HANDLER.executeRestCommand(session, commandType, command);
@@ -773,7 +773,7 @@ public class ApexEditorRestResource implements RestCommandHandler {
      */
     @Override
     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
-            final RestCommand command, final String jsonString) {
+           final RestCommand command, final String jsonString) {
         switch (commandType) {
             case MODEL:
                 return MODEL_HANDLER.executeRestCommand(session, commandType, command, jsonString);
@@ -806,7 +806,7 @@ public class ApexEditorRestResource implements RestCommandHandler {
      */
     @Override
     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
-            final RestCommand command, final String name, final String version) {
+           final RestCommand command, final String name, final String version) {
         switch (commandType) {
             case MODEL:
                 return MODEL_HANDLER.executeRestCommand(session, commandType, command, name, version);
index af8d9dd..3540b2d 100644 (file)
@@ -83,7 +83,7 @@ public class PolicyUploadHandler {
      * @return the result of the upload process
      */
     public ApexApiResult doUpload(final ApexModel apexModel, final InputStream toscaTemplateInputStream,
-                                  final InputStream apexConfigInputStream) {
+                                  final InputStream apexConfigInputStream, final String userId) {
         final ProcessedTemplate processedToscaTemplate;
         try {
             processedToscaTemplate = toscaTemplateProcessor.process(toscaTemplateInputStream);
@@ -113,10 +113,11 @@ public class PolicyUploadHandler {
         if (!processedApexConfig.isValid()) {
             return buildResponse(processedApexConfig);
         }
-        return doUpload(apexModel, processedToscaTemplate.getContent(), processedApexConfig.getContent());
+        return doUpload(apexModel, processedToscaTemplate.getContent(), processedApexConfig.getContent(), userId);
     }
 
-    private ApexApiResult doUpload(final ApexModel apexModel, final String toscaTemplate, final String apexConfig) {
+    private ApexApiResult doUpload(final ApexModel apexModel, final String toscaTemplate, final String apexConfig,
+                                   final String userId) {
         LOGGER.entry();
         if (!isUploadPluginEnabled()) {
             final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
@@ -128,6 +129,7 @@ public class PolicyUploadHandler {
         final UploadPolicyRequestDto uploadPolicyRequestDto = new UploadPolicyRequestDto();
         final AxArtifactKey policyKey = policyModel.getKeyInformation().getKey();
         final java.util.UUID uuid = policyModel.getKeyInformation().get(policyKey).getUuid();
+        uploadPolicyRequestDto.setUserId(userId);
         uploadPolicyRequestDto
             .setFilename(String.format("%s.%s.%s", uuid, policyKey.getName(), policyKey.getVersion()));
         final String apexPolicy = convert(result.getMessage(), toscaTemplate, apexConfig).orElse(null);
index 53e205e..7aa3508 100644 (file)
     <script type="text/javascript" src="js/ApexConfig.js"></script>
     <script type="text/javascript" src="js/ApexUpload.js"></script>
     <script type="text/javascript" src="js/ApexMain.js"></script>
+    <script type="text/javascript" src="js/ApexUser.js"></script>
 
 </body>
 </html>
diff --git a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/ApexUser.js b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/ApexUser.js
new file mode 100644 (file)
index 0000000..684fdf7
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
+ * ================================================================================
+ * 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=========================================================
+ */
+
+const userManager = {
+  userId: null
+};
+
+(function () {
+  //grabbing the userId
+  const urlParams = new URLSearchParams(window.location.search);
+  userManager.userId = urlParams.get('userId');
+})();
index aac7762..912bec4 100644 (file)
@@ -30,6 +30,7 @@
         const toscaTemplateFile = $('#tosca-template-file')[0].files[0];
         data.append('apex-config-file', apexConfigFile);
         data.append('tosca-template-file', toscaTemplateFile);
+        data.append('userId', userManager.userId);
         uploadPlugin.upload(data, function () {
           dialog.dialog("close");
         });
@@ -48,6 +49,7 @@
         }
       });
     });
+
   </script>
 </head>
 <body>
   <p>Provide the following files to compose the policy TOSCA</p>
   <div id="upload-form">
     <label for="apex-config-file">Apex Config</label> <br/>
-    <input id="apex-config-file" type="file" required="required" accept="application/json" class="ui-widget-content ui-corner-all"/> <br/>
+    <input id="apex-config-file" type="file" required="required" accept="application/json"
+           class="ui-widget-content ui-corner-all"/> <br/>
     <label for="tosca-template-file">Tosca Template</label> <br/>
-    <input id="tosca-template-file" type="file" required="required" accept="application/json" class="ui-widget-content ui-corner-all"/>
+    <input id="tosca-template-file" type="file" required="required" accept="application/json"
+           class="ui-widget-content ui-corner-all"/>
   </div>
 </div>
 </body>
-</html>
\ No newline at end of file
+</html>
index 7a6ead7..6ec5eef 100644 (file)
@@ -52,6 +52,7 @@ import org.onap.policy.gui.editors.apex.rest.handling.plugin.upload.UploadPolicy
 
 public class PolicyUploadHandlerTest {
 
+    private static final String USER_ID = "cs0008";
     @Mock
     private PolicyToscaConverter policyToscaConverter;
     @Mock
@@ -87,7 +88,7 @@ public class PolicyUploadHandlerTest {
             .thenReturn(Response.ok().status(201).build());
 
         ApexApiResult apexApiResult = policyUploadHandler
-            .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
+            .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class), USER_ID);
 
         assertThat("Response should be ok", apexApiResult.isOk(), is(true));
         String expectedSuccessMsg =
@@ -99,7 +100,7 @@ public class PolicyUploadHandlerTest {
             .thenReturn(Response.serverError().build());
 
         apexApiResult = policyUploadHandler
-            .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
+            .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class), USER_ID);
 
         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
         expectedSuccessMsg =
@@ -118,7 +119,7 @@ public class PolicyUploadHandlerTest {
         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
         final ApexApiResult apexApiResult = policyUploadHandler
-            .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class));
+            .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class), USER_ID);
 
         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
         assertThat("Response message should be as expected",
@@ -134,7 +135,7 @@ public class PolicyUploadHandlerTest {
         processedToscaTemplate.addToErrors(Collections.singleton(errorMsg));
         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
         final ApexApiResult apexApiResult = policyUploadHandler
-            .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class));
+            .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class), USER_ID);
 
         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
         assertThat("Response message should be as expected",
@@ -151,7 +152,7 @@ public class PolicyUploadHandlerTest {
         processedApexConfig.addToErrors(Collections.singleton(errorMsg));
         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
         final ApexApiResult apexApiResult = policyUploadHandler
-            .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class));
+            .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class), USER_ID);
 
         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
         assertThat("Response message should be as expected",
@@ -173,7 +174,7 @@ public class PolicyUploadHandlerTest {
             .thenReturn(Response.ok().status(201).build());
 
         final ApexApiResult apexApiResult = policyUploadHandler
-            .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
+            .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class), USER_ID);
 
         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
         final String expectedErrorMsg = String
@@ -197,4 +198,4 @@ public class PolicyUploadHandlerTest {
         when(apexModel.getPolicyModel()).thenReturn(axPolicyModel);
         return apexModel;
     }
-}
\ No newline at end of file
+}