/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2022 Nordix Foundation.
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
-import org.apache.commons.lang3.StringUtils;
import org.onap.policy.apex.model.modelapi.ApexApiResult;
import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
import org.onap.policy.common.utils.resources.TextFileUtils;
-import org.onap.policy.gui.editors.apex.rest.ApexEditorMain;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
* Uploads a TOSCA Policy Model to a configured endpoint.
*
- * @param userid the userid to use for upload
+ * @param userId the userId to use for upload. If blank, the commandline
+ * parameter "upload-userid" is used.
* @return an ApexAPIResult that contains the operation status and success/error messages
*/
@GET
@Path("Model/Upload")
- public ApexApiResult uploadModel(@QueryParam("userId") final String userid) {
- if (!StringUtils.isBlank(userid)) {
- ApexEditorMain.getParameters().setUploadUserid(userid);
- }
- return processRestCommand(RestCommandType.MODEL, RestCommand.UPLOAD);
+ public ApexApiResult uploadModel(@QueryParam("userId") final String userId) {
+ return processRestCommand(RestCommandType.MODEL, RestCommand.UPLOAD, userId);
}
/**
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2022 Nordix Foundation.
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
return listModel(session);
case DOWNLOAD:
return downloadModel(session);
- case UPLOAD:
- return uploadModel(session);
case DELETE:
return deleteModel(session);
default:
return createModel(session, jsonString);
case UPDATE:
return updateModel(session, jsonString);
+ case UPLOAD:
+ return uploadModel(session, jsonString);
default:
return getUnsupportedCommandResultMessage(session, commandType, command);
}
* Upload the model for this session to the configured URL.
*
* @param session the Apex model editing session
+ * @param userId the userId to use for upload. If blank, the commandline
+ * parameter "upload-userid" is used.
* @return a result indicating if the upload was successful or not
*/
- private ApexApiResult uploadModel(final RestSession session) {
+ private ApexApiResult uploadModel(final RestSession session, String userId) {
LOGGER.entry();
- ApexApiResult result = session.uploadModel();
+ ApexApiResult result = session.uploadModel(userId);
LOGGER.exit("Model/Download" + (result != null && result.isOk() ? OK : NOT_OK));
return result;
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2022 Nordix Foundation.
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
/**
* Upload the apex model as a TOSCA service template YAML string to the configured URL.
*
+ * @param userId the userId to use for upload. If blank, the commandline
+ * parameter "upload-userid" is used.
* @return a result indicating if the upload was successful or not
*/
- public ApexApiResult uploadModel() {
+ public ApexApiResult uploadModel(final String userId) {
// Get the model in TOSCA format
ApexApiResult result = downloadModel();
if (result.isNok()) {
var policyModelUUid = apexModelBeingUploaded.getPolicyModel().getKeyInformation().get(policyModelKey)
.getUuid().toString();
- return new PolicyUploadHandler().doUpload(result.getMessage(), policyModelKey, policyModelUUid);
+ return new PolicyUploadHandler().doUpload(result.getMessage(), policyModelKey, policyModelUUid, userId);
}
/**
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2020 Nordix Foundation
+ * Copyright (C) 2020-2022 Nordix Foundation
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* @param toscaServiceTemplate the TOSCA service template
* @param policyModelKey the key of the policy model
* @param policyModelUuid the UUID of the policy model
+ * @param uploadUserId the userId to use for upload. If blank, the commandline
+ * parameter "upload-userid" is used.
* @return the result of the upload process
*/
public ApexApiResult doUpload(final String toscaServiceTemplate, final AxArtifactKey policyModelKey,
- final String policyModelUuid) {
+ final String policyModelUuid, String uploadUserId) {
LOGGER.entry();
- if (StringUtils.isBlank(ApexEditorMain.getParameters().getUploadUrl())) {
+ final String uploadUrl = ApexEditorMain.getParameters().getUploadUrl();
+ if (StringUtils.isBlank(uploadUrl)) {
final var apexApiResult = new ApexApiResult(Result.FAILED);
apexApiResult.addMessage("Model upload is disabled, parameter upload-url is not set on server");
LOGGER.exit(MODEL_UPLOAD_NOT_OK);
return apexApiResult;
+ }
+ if (StringUtils.isBlank(uploadUserId)) {
+ uploadUserId = ApexEditorMain.getParameters().getUploadUserid();
}
final var uploadPolicyRequestDto = new UploadPolicyRequestDto();
- uploadPolicyRequestDto.setUserId(ApexEditorMain.getParameters().getUploadUserid());
+ uploadPolicyRequestDto.setUserId(uploadUserId);
uploadPolicyRequestDto
.setFileData(Base64.getEncoder().encodeToString(toscaServiceTemplate.getBytes(StandardCharsets.UTF_8)));
uploadPolicyRequestDto.setFilename(
String.format("%s.%s.%s", policyModelUuid, policyModelKey.getName(), policyModelKey.getVersion()));
try {
- final var response = ClientBuilder.newClient().target(ApexEditorMain.getParameters().getUploadUrl())
+ final var response = ClientBuilder.newClient().target(uploadUrl)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(uploadPolicyRequestDto, MediaType.APPLICATION_JSON));
if (response.getStatus() == 201) {
final var apexApiResult = new ApexApiResult(Result.SUCCESS);
- String.format("uploading Policy '%s' to URL '%s' with userId '%s' was successful",
- policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
- ApexEditorMain.getParameters().getUploadUserid());
+ apexApiResult.addMessage(
+ String.format("uploading Policy '%s' to URL '%s' with userId '%s' was successful",
+ policyModelKey.getId(), uploadUrl, uploadUserId));
LOGGER.exit("Model/Upload: OK");
return apexApiResult;
} else {
final var apexApiResult = new ApexApiResult(Result.FAILED);
apexApiResult.addMessage(
String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with status %s",
- policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
- ApexEditorMain.getParameters().getUploadUserid(), response.getStatus()));
+ policyModelKey.getId(), uploadUrl, uploadUserId, response.getStatus()));
LOGGER.exit(MODEL_UPLOAD_NOT_OK);
return apexApiResult;
}
final var apexApiResult = new ApexApiResult(Result.FAILED);
apexApiResult
.addMessage(String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with error %s",
- policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
- ApexEditorMain.getParameters().getUploadUserid(), e.getMessage()));
+ policyModelKey.getId(), uploadUrl, uploadUserId, e.getMessage()));
LOGGER.exit(MODEL_UPLOAD_NOT_OK);
return apexApiResult;
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
+ * Copyright (C) 2021-2022 Nordix Foundation.
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
final var toscaPath = Path.of("src/test/resources/models/PolicyModel.yaml");
final var toscaString = Files.readString(toscaPath);
restSession.loadFromString(toscaString);
- final var apexApiResult = restSession.uploadModel();
+ final var apexApiResult = restSession.uploadModel("");
assertThat(apexApiResult.isNok()).isTrue();
assertThat(apexApiResult.getMessage()).contains("Model upload is disabled");
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
+ * Copyright (C) 2021-2022 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.nio.file.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
public class PolicyUploadHandlerTest {
+ private static final String CMDLINE_UPLOAD_USERID = "MyUser";
private PolicyUploadHandler uploadHandler;
private AxArtifactKey axArtifactKey;
private String toscaServiceTemplate;
private MockedStatic<ClientBuilder> clientBuilderMockedStatic;
+ private ArgumentCaptor<Entity<UploadPolicyRequestDto>> dtoEntityCaptor;
/**
* Prepares test environment.
@Test
public void testDoUploadNoUrl() {
- final String[] args = {"--upload-userid", "MyUser"};
+ final String[] args = {"--upload-userid", CMDLINE_UPLOAD_USERID};
final var outBaStream = new ByteArrayOutputStream();
final var outStream = new PrintStream(outBaStream);
new ApexEditorMain(args, outStream);
- final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
+ final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
assertThat(result.isNok()).isTrue();
assertThat(result.getMessage()).contains("Model upload is disable");
}
prepareApexEditorMain();
- final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
+ final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
assertThat(result.isNok()).isTrue();
assertThat(result.getMessage()).contains("failed with error");
prepareApexEditorMain();
- final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
+ final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
assertThat(result.isOk()).isTrue();
}
prepareApexEditorMain();
- final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
+ final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
assertThat(result.isNok()).isTrue();
assertThat(result.getMessage()).contains("failed with status 500");
}
+ @Test
+ public void testDoUploadUserId() {
+ final var response = Mockito.mock(Response.class);
+ mockRsHttpClient(response);
+
+ Mockito.doReturn(201).when(response).getStatus();
+
+ prepareApexEditorMain();
+
+ // If uploadUserId is specified, that value should be in DTO.
+ var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "",
+ "OverrideUser");
+ assertThat(result.isOk()).isTrue();
+ var dto = dtoEntityCaptor.getValue().getEntity();
+ assertThat(dto.getUserId()).isEqualTo("OverrideUser");
+
+ // If uploadUserId is blank, the value from command line parameter 'upload-userid' is used.
+ result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
+ assertThat(result.isOk()).isTrue();
+ dto = dtoEntityCaptor.getValue().getEntity();
+ assertThat(dto.getUserId()).isEqualTo(CMDLINE_UPLOAD_USERID);
+
+ // If uploadUserId is null, the value from command line parameter 'upload-userid' is used.
+ result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", null);
+ assertThat(result.isOk()).isTrue();
+ dto = dtoEntityCaptor.getValue().getEntity();
+ assertThat(dto.getUserId()).isEqualTo(CMDLINE_UPLOAD_USERID);
+ }
+
private void mockRsHttpClient(Response response) {
final var webTarget = Mockito.mock(WebTarget.class);
final var client = Mockito.mock(Client.class);
clientBuilderMockedStatic = Mockito.mockStatic(ClientBuilder.class);
+ dtoEntityCaptor = ArgumentCaptor.forClass(Entity.class);
+
Mockito.when(ClientBuilder.newClient()).thenReturn(client);
Mockito.when(client.target(ArgumentMatchers.anyString())).thenReturn(webTarget);
Mockito.when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);
Mockito.when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);
- Mockito.when(invocationBuilder.post(ArgumentMatchers.any())).thenReturn(response);
+ Mockito.when(invocationBuilder.post(dtoEntityCaptor.capture())).thenReturn(response);
}
private void prepareApexEditorMain() {
- final String[] args = {"--upload-userid", "MyUser", "--upload-url", "http://127.0.0.1"};
+ final String[] args = {"--upload-userid", CMDLINE_UPLOAD_USERID, "--upload-url", "http://127.0.0.1"};
final var outBaStream = new ByteArrayOutputStream();
final var outStream = new PrintStream(outBaStream);
new ApexEditorMain(args, outStream);