import org.onap.policy.cds.api.CdsProcessorListener;
import org.onap.policy.cds.client.CdsProcessorGrpcClient;
import org.onap.policy.cds.properties.CdsServerProperties;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.controlloop.ControlLoopOperation;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
-import org.onap.policy.controlloop.actor.cds.beans.CdsActionRequest;
import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
+import org.onap.policy.controlloop.actor.cds.request.CdsActionRequest;
import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
import org.onap.policy.controlloop.policy.Policy;
import org.slf4j.Logger;
Builder struct = Struct.newBuilder();
try {
- String requestStr = request.toString();
+ String requestStr = request.generateCdsPayload();
Preconditions.checkState(!Strings.isNullOrEmpty(requestStr), "Unable to build "
+ "config-deploy-request from payload parameters: {}", payload);
JsonFormat.parser().merge(requestStr, struct);
- } catch (InvalidProtocolBufferException e) {
- LOGGER.error("Failed to parse received message. blueprint({}:{}) for action({})", cbaName, cbaVersion,
- cbaActionName, e);
+ } catch (InvalidProtocolBufferException | CoderException e) {
+ LOGGER.error("Failed to embed CDS payload string into the input request. blueprint({}:{}) for action({})",
+ cbaName, cbaVersion, cbaActionName, e);
return Optional.empty();
}
+++ /dev/null
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2019 Bell Canada. 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.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.controlloop.actor.cds.beans;
-
-import com.google.gson.annotations.SerializedName;
-
-import java.io.Serializable;
-import java.util.Map;
-import lombok.Getter;
-import lombok.Setter;
-import org.onap.policy.common.utils.coder.Coder;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Getter
-@Setter
-public class CdsActionRequest implements Serializable {
- private static final long serialVersionUID = -4172157702597791493L;
-
- private static final Logger LOGGER = LoggerFactory.getLogger(CdsActionRequest.class);
- private static final Coder CODER = new StandardCoder();
-
- @SerializedName("policy-payload")
- private Map<String, String> policyPayload;
-
- @SerializedName("aai-properties")
- private Map<String, String> aaiProperties;
-
- @SerializedName("resolution-key")
- private String resolutionKey;
-
- private transient String actionName;
-
- @Override
- public String toString() {
- try {
- return "{" + "\"" + actionName + "-request\":" + CODER.encode(this) + '}';
- } catch (CoderException e) {
- LOGGER.error("Failure serializing CdsActionRequest object: ", e);
- return "";
- }
- }
-}
// CDS blueprint archive parameters
public static final String KEY_CBA_NAME = "artifact_name";
public static final String KEY_CBA_VERSION = "artifact_version";
+ public static final String KEY_RESOLUTION_KEY = "resolution-key";
+ public static final String CDS_REQUEST_SUFFIX = "-request";
+ public static final String CDS_REQUEST_PROPERTIES_SUFFIX = "-properties";
public static final String ORIGINATOR_ID = "POLICY";
// Temporarily set to synchronous mode to support current rules, since callbacks aren't supported yet
public static final String CDS_MODE = "sync";
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Bell Canada. 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.actor.cds.request;
+
+import java.io.Serializable;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import lombok.Getter;
+import lombok.Setter;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
+
+@Getter
+@Setter
+public class CdsActionRequest implements Serializable {
+
+ private static final long serialVersionUID = -4172157702597791493L;
+ private static final StandardCoder CODER = new StandardCoder();
+
+ private String actionName;
+ private String resolutionKey;
+ private Map<String, String> aaiProperties;
+ private Map<String, String> policyPayload;
+
+ /**
+ * Generate the CDS gRPC request payload from the action-name (aka operational policy recipe).
+ * The CDS gRPC request payload generation follows the below pattern:
+ * {
+ * "{@link CdsActionRequest#getActionName()}-request": {
+ * "resolution-key": "{@link CdsActionRequest#getResolutionKey()} ()}",
+ * "{@link CdsActionRequest#getActionName()}-properties": {
+ * "{@link CdsActionRequest#getAaiProperties()} ()}",
+ * "{@link CdsActionRequest#getPolicyPayload()} ()}"
+ * }
+ * }
+ * }
+ * @return JSON string equivalent of the CDS request object
+ * @throws CoderException if error occurs when serializing to JSON string
+ */
+ public String generateCdsPayload() throws CoderException {
+ // 1. Build the innermost object to include AAI properties and policy payload information
+ Map<String, String> cdsActionPropsMap = new LinkedHashMap<>();
+ cdsActionPropsMap.putAll(aaiProperties);
+ cdsActionPropsMap.putAll(policyPayload);
+
+ // 2. Build the enclosing CDS action request properties object to contain (1) and the resolution-key
+ Map<String, Object> cdsActionRequestMap = new LinkedHashMap<>();
+ cdsActionRequestMap.put(CdsActorConstants.KEY_RESOLUTION_KEY, resolutionKey);
+ cdsActionRequestMap.put(generateCdsActionPropertiesKey(), cdsActionPropsMap);
+
+ // 3. Finally build the CDS action request object
+ Map<String, Object> cdsActionRequestObj = new LinkedHashMap<>();
+ cdsActionRequestObj.put(generateCdsActionRequestKey(), cdsActionRequestMap);
+
+ // 4. Serialize the CDS action request object
+ return CODER.encode(cdsActionRequestObj);
+ }
+
+ private String generateCdsActionPropertiesKey() {
+ return actionName + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX;
+ }
+
+ private String generateCdsActionRequestKey() {
+ return actionName + CdsActorConstants.CDS_REQUEST_SUFFIX;
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Bell Canada. 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.actor.cds.request;
+
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
+
+public class CdsActionRequestTest {
+
+ private static final String TEST_ACTION_NAME = "vfw-modify-config";
+
+ @Test
+ public void testGenerateCdsPayload() throws CoderException {
+ // Setup the CdsActionRequest object
+ CdsActionRequest req = new CdsActionRequest();
+ req.setActionName(TEST_ACTION_NAME);
+ req.setResolutionKey("1234567890");
+ Map<String, String> payloadProps = ImmutableMap.of("data", "{\"active-streams\":\"5\"}");
+ req.setPolicyPayload(payloadProps);
+
+ Map<String, String> aaiParams =
+ ImmutableMap.of("service-instance.service-instance-id", "1234", "generic-vnf.vnf-id", "5678");
+ req.setAaiProperties(aaiParams);
+
+ // Act
+ String result = req.generateCdsPayload();
+
+ // Assert
+ assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX));
+ assertTrue(result.contains(TEST_ACTION_NAME + CdsActorConstants.CDS_REQUEST_SUFFIX));
+ assertTrue(result.contains(CdsActorConstants.KEY_RESOLUTION_KEY));
+ }
+}
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.UUID;
+import lombok.Getter;
+import lombok.Setter;
+@Getter
+@Setter
public abstract class ControlLoopEvent implements Serializable {
private static final long serialVersionUID = 2391252138583119195L;
-
+
@SerializedName("closedLoopControlName")
private String closedLoopControlName;
-
+
@SerializedName("version")
private String version = "1.0.2";
-
+
@SerializedName("requestID")
private UUID requestId;
-
+
@SerializedName("closedLoopEventClient")
private String closedLoopEventClient;
-
+
@SerializedName("target_type")
- private ControlLoopTargetType targetType;
-
+ private String targetType;
+
@SerializedName("target")
private String target;
-
+
@SerializedName("from")
private String from;
-
+
@SerializedName("policyScope")
private String policyScope;
-
+
@SerializedName("policyName")
private String policyName;
-
+
@SerializedName("policyVersion")
private String policyVersion;
-
+
@SerializedName("closedLoopEventStatus")
private ControlLoopEventStatus closedLoopEventStatus;
- @SerializedName("payload")
- private String payload;
-
public ControlLoopEvent() {
}
this.policyName = event.policyName;
this.policyVersion = event.policyVersion;
this.closedLoopEventStatus = event.closedLoopEventStatus;
- this.payload = event.payload;
}
public boolean isEventStatusValid() {
return this.closedLoopEventStatus != null;
}
-
- public String getClosedLoopControlName() {
- return closedLoopControlName;
- }
-
- public void setClosedLoopControlName(String closedLoopControlName) {
- this.closedLoopControlName = closedLoopControlName;
- }
-
- public String getVersion() {
- return version;
- }
-
- public void setVersion(String version) {
- this.version = version;
- }
-
- public UUID getRequestId() {
- return requestId;
- }
-
- public void setRequestId(UUID requestId) {
- this.requestId = requestId;
- }
-
- public String getClosedLoopEventClient() {
- return closedLoopEventClient;
- }
-
- public void setClosedLoopEventClient(String closedLoopEventClient) {
- this.closedLoopEventClient = closedLoopEventClient;
- }
-
- public ControlLoopTargetType getTargetType() {
- return targetType;
- }
-
- public void setTargetType(ControlLoopTargetType targetType) {
- this.targetType = targetType;
- }
-
- public String getTarget() {
- return target;
- }
-
- public void setTarget(String target) {
- this.target = target;
- }
-
- public String getFrom() {
- return from;
- }
-
- public void setFrom(String from) {
- this.from = from;
- }
-
- public String getPolicyScope() {
- return policyScope;
- }
-
- public void setPolicyScope(String policyScope) {
- this.policyScope = policyScope;
- }
-
- public String getPolicyName() {
- return policyName;
- }
-
- public void setPolicyName(String policyName) {
- this.policyName = policyName;
- }
-
- public String getPolicyVersion() {
- return policyVersion;
- }
-
- public void setPolicyVersion(String policyVersion) {
- this.policyVersion = policyVersion;
- }
-
- public ControlLoopEventStatus getClosedLoopEventStatus() {
- return closedLoopEventStatus;
- }
-
- public void setClosedLoopEventStatus(ControlLoopEventStatus closedLoopEventStatus) {
- this.closedLoopEventStatus = closedLoopEventStatus;
- }
-
- public String getPayload() {
- return payload;
- }
-
- public void setPayload(String payload) {
- this.payload = payload;
- }
}
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
+import lombok.Getter;
+import lombok.Setter;
+@Getter
+@Setter
public abstract class ControlLoopNotification implements Serializable {
private static final long serialVersionUID = 7538596984567127915L;
private String version = "1.0.2";
private UUID requestId;
private String closedLoopEventClient;
- private ControlLoopTargetType targetType;
+ private String targetType;
private String target;
private String from;
private String policyScope;
this.setTargetType(event.getTargetType());
this.setTarget(event.getTarget());
}
-
- public String getClosedLoopControlName() {
- return closedLoopControlName;
- }
-
- public void setClosedLoopControlName(String closedLoopControlName) {
- this.closedLoopControlName = closedLoopControlName;
- }
-
- public String getVersion() {
- return version;
- }
-
- public void setVersion(String version) {
- this.version = version;
- }
-
- public UUID getRequestId() {
- return requestId;
- }
-
- public void setRequestId(UUID requestId) {
- this.requestId = requestId;
- }
-
- public String getClosedLoopEventClient() {
- return closedLoopEventClient;
- }
-
- public void setClosedLoopEventClient(String closedLoopEventClient) {
- this.closedLoopEventClient = closedLoopEventClient;
- }
-
- public ControlLoopTargetType getTargetType() {
- return targetType;
- }
-
- public void setTargetType(ControlLoopTargetType targetType) {
- this.targetType = targetType;
- }
-
- public String getTarget() {
- return target;
- }
-
- public void setTarget(String target) {
- this.target = target;
- }
-
- public String getFrom() {
- return from;
- }
-
- public void setFrom(String from) {
- this.from = from;
- }
-
- public String getPolicyScope() {
- return policyScope;
- }
-
- public void setPolicyScope(String policyScope) {
- this.policyScope = policyScope;
- }
-
- public String getPolicyName() {
- return policyName;
- }
-
- public void setPolicyName(String policyName) {
- this.policyName = policyName;
- }
-
- public String getPolicyVersion() {
- return policyVersion;
- }
-
- public void setPolicyVersion(String policyVersion) {
- this.policyVersion = policyVersion;
- }
-
- public ControlLoopNotificationType getNotification() {
- return notification;
- }
-
- public void setNotification(ControlLoopNotificationType notification) {
- this.notification = notification;
- }
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- public ZonedDateTime getNotificationTime() {
- return notificationTime;
- }
-
- public void setNotificationTime(ZonedDateTime notificationTime) {
- this.notificationTime = notificationTime;
- }
-
- public Integer getOpsClTimer() {
- return opsClTimer;
- }
-
- public void setOpsClTimer(Integer opsClTimer) {
- this.opsClTimer = opsClTimer;
- }
-
- public List<ControlLoopOperation> getHistory() {
- return history;
- }
-
- public void setHistory(List<ControlLoopOperation> history) {
- this.history = history;
- }
}
package org.onap.policy.controlloop;
-public enum ControlLoopTargetType {
- VM("VM"), VF("VF"), VFC("VFC"), VNF("VNF");
-
- private String type;
-
- private ControlLoopTargetType(String type) {
- this.type = type;
- }
-
- @Override
- public String toString() {
- return this.type;
- }
-
- /**
- * Convert a String type to a ControlLoopTargetType.
- *
- * @param type the String type
- * @return the ControlLoopTargetType
- */
- public static ControlLoopTargetType toType(String type) {
- if (VM.toString().equals(type)) {
- return VM;
- }
- if (VF.toString().equals(type)) {
- return VF;
- }
- if (VFC.toString().equals(type)) {
- return VFC;
- }
- if (VNF.toString().equals(type)) {
- return VNF;
- }
-
- return null;
- }
+public class ControlLoopTargetType {
+ public static final String VM = "VM";
+ public static final String VF = "VF";
+ public static final String VFC = "VFC";
+ public static final String VNF = "VNF";
}
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
+import lombok.Getter;
+import lombok.Setter;
+@Getter
+@Setter
public class VirtualControlLoopEvent extends ControlLoopEvent {
private static final long serialVersionUID = -5752405682246066226L;
-
+
+ @SerializedName("payload")
+ private String payload;
+
@SerializedName("closedLoopAlarmStart")
private Instant closedLoopAlarmStart;
-
+
@SerializedName("closedLoopAlarmEnd")
private Instant closedLoopAlarmEnd;
-
+
@SerializedName("AAI")
private Map<String, String> aai = new HashMap<>();
if (event.aai != null) {
this.aai = new HashMap<>(event.aai);
}
+ this.payload = event.payload;
this.closedLoopAlarmStart = event.closedLoopAlarmStart;
this.closedLoopAlarmEnd = event.closedLoopAlarmEnd;
}
-
- public Instant getClosedLoopAlarmStart() {
- return closedLoopAlarmStart;
- }
-
- public void setClosedLoopAlarmStart(Instant closedLoopAlarmStart) {
- this.closedLoopAlarmStart = closedLoopAlarmStart;
- }
-
- public Instant getClosedLoopAlarmEnd() {
- return closedLoopAlarmEnd;
- }
-
- public void setClosedLoopAlarmEnd(Instant closedLoopAlarmEnd) {
- this.closedLoopAlarmEnd = closedLoopAlarmEnd;
- }
-
- public Map<String, String> getAai() {
- return aai;
- }
-
- public void setAai(Map<String, String> aai) {
- this.aai = aai;
- }
}
import java.time.format.DateTimeFormatter;
import org.onap.policy.controlloop.ControlLoopNotificationType;
-import org.onap.policy.controlloop.ControlLoopTargetType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(ZonedDateTime.class, new GsonUtcAdapter())
.registerTypeAdapter(Instant.class, new GsonInstantAdapter())
.registerTypeAdapter(ControlLoopNotificationType.class, new NotificationTypeAdapter())
- .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter()).create();
+ .create();
public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
.registerTypeAdapter(ZonedDateTime.class, new GsonUtcAdapter())
.registerTypeAdapter(Instant.class, new GsonInstantAdapter())
.registerTypeAdapter(ControlLoopNotificationType.class, new NotificationTypeAdapter())
- .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter()).create();
+ .create();
public static final Gson gsonJunit = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
.registerTypeAdapter(ZonedDateTime.class, new GsonUtcAdapter())
.registerTypeAdapter(Instant.class, new GsonInstantAdapter())
- .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter()).create();
+ .create();
private Serialization() {}
}
}
- public static class TargetTypeAdapter
- implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> {
- @Override
- public JsonElement serialize(ControlLoopTargetType src, Type typeOfSrc, JsonSerializationContext context) {
- return new JsonPrimitive(src.toString());
- }
-
- @Override
- public ControlLoopTargetType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
- return ControlLoopTargetType.toType(json.getAsString());
- }
- }
-
public static class GsonUtcAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
private static final Logger logger = LoggerFactory.getLogger(GsonUtcAdapter.class);
public static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
event.setFrom("from");
assertEquals("from", event.getFrom());
- event.setPayload("payload");
- assertEquals("payload", event.getPayload());
-
event.setPolicyName("policyname");
assertEquals("policyname", event.getPolicyName());
@Test
public void test() {
- assertEquals(ControlLoopTargetType.VM, ControlLoopTargetType.toType("VM"));
- assertEquals(ControlLoopTargetType.VF, ControlLoopTargetType.toType("VF"));
- assertEquals(ControlLoopTargetType.VFC, ControlLoopTargetType.toType("VFC"));
- assertEquals(ControlLoopTargetType.VNF, ControlLoopTargetType.toType("VNF"));
-
- assertNull(ControlLoopTargetType.toType("foo"));
+ assertEquals("VM", ControlLoopTargetType.VM);
+ assertEquals("VF", ControlLoopTargetType.VF);
+ assertEquals("VFC", ControlLoopTargetType.VFC);
+ assertEquals("VNF", ControlLoopTargetType.VNF);
}
}
assertNotNull(event);
assertNotNull(event.getAai());
+ event.setPayload("payload");
+ assertEquals("payload", event.getPayload());
+
Instant now = Instant.now();
event.setClosedLoopAlarmStart(now);
event.setClosedLoopAlarmEnd(now);
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
return sendRequest(delete);
}
+ /**
+ * Perform REST Patch.
+ *
+ * @param url the url
+ * @param username the user name
+ * @param password the password
+ * @param headers any headers
+ * @param body body to send
+ * @return the response status code and the body
+ */
+ public Pair<Integer, String> patch(String url, String username, String password,
+ Map<String, String> headers, String body) {
+ String contentType = "application/merge-patch+json";
+ HttpPatch patch = new HttpPatch(url);
+ addHeaders(patch, username, password, headers);
+ patch.addHeader(CONTENT_TYPE, contentType);
+ try {
+ StringEntity input = new StringEntity(body);
+ input.setContentType(contentType);
+ patch.setEntity(input);
+ } catch (Exception e) {
+ logger.error("patch threw: ", e);
+ return null;
+ }
+ return sendRequest(patch);
+ }
+
/**
* Send REST request.
*
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
+import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@Path("RestTest")
public class RestTest {
-
+ private static final String MERGE_PATCH_PLUS_JSON = "application/merge-patch+json";
private static final String NAME_PARAM = "Bob";
private static final String AGE_PARAM = "10";
private static String putUriBlank;
private static String postUri;
private static String postUriBlank;
+ private static String patchUri;
+ private static String patchUriBlank;
private static HttpServletServer server;
putUriBlank = baseUri + "RestTest/PutBlank";
postUri = baseUri + "RestTest/PostHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
postUriBlank = baseUri + "RestTest/PostBlank";
+ patchUri = baseUri + "RestTest/PatchHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
+ patchUriBlank = baseUri + "RestTest/PatchBlank";
server = HttpServletServerFactoryInstance.getServerFactory()
.build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
mgr.delete(null, "user", null, null, null, null);
}
+ @Test(expected = NullPointerException.class)
+ public void testPatchUrlNull() {
+ RestManager mgr = new RestManager();
+ mgr.patch(null, "user", null, null, PAYLOAD);
+ }
+
@Test
public void testUsernameNull() {
RestManager mgr = new RestManager();
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.patch(patchUri, null, null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.patch(patchUriBlank, null, null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.second);
}
@Test
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.patch(patchUri, "", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.patch(patchUriBlank, "", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.second);
}
@Test
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.patch(patchUri, "user", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.patch(patchUriBlank, "user", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.second);
}
@Test
result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
assertEquals((Integer)404, result.first);
+
+ result = mgr.patch(baseUri + "RestTest/PatchHello/", null, null, null, PAYLOAD);
+ assertEquals((Integer)404, result.first);
}
@Test
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
+
+ result = mgr.patch(baseUri + "RestTest/PatchHello/" + NAME_PARAM, null, null,
+ null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
}
@Test
result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
assertEquals((Integer)404, result.first);
+
+ result = mgr.patch(baseUri + "NonExistant/URL/", null, null, null, PAYLOAD);
+ assertEquals((Integer)404, result.first);
}
@Test
result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
assertEquals((Integer)405, result.first);
+
+ result = mgr.patch(getUri, null, null, null, PAYLOAD);
+ assertEquals((Integer)405, result.first);
}
@GET
public String postBlank( String payload) {
return "POST: " + payload + RETURN_STRING;
}
+
+ @Target({ElementType.METHOD})
+ @Retention(RetentionPolicy.RUNTIME)
+ @HttpMethod("PATCH")
+ @Documented
+ public static @interface Patch {
+ }
+
+ @Patch
+ @Path("/PatchHello/{name}")
+ @Consumes(MERGE_PATCH_PLUS_JSON)
+ @Produces(MERGE_PATCH_PLUS_JSON)
+ public String patchIt(
+ String payload,
+ @PathParam("name") String name,
+ @DefaultValue("90") @QueryParam("age") String age) {
+
+ return "PATCH: " + payload + RETURN_STRING + name + " aged " + age;
+ }
+
+ @Patch
+ @Path("/PatchBlank")
+ @Produces(MERGE_PATCH_PLUS_JSON)
+ public String patchBlank( String payload) {
+ return "PATCH: " + payload + RETURN_STRING;
+ }
}
@SerializedName("policy-type")
private ToscaPolicyTypeIdentifier policyType;
- @SerializedName("policy")
+ @SerializedName("policy-id")
private ToscaPolicyIdentifier policy;
/**