Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow AbstractMethodInvocationHandler implementations to validate inp…
- Loading branch information
1 parent
d51125f
commit 86412a2
Showing
4 changed files
with
176 additions
and
31 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...va/org/eclipse/milo/opcua/sdk/server/api/methods/AbstractMethodInvocationHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2021 the Eclipse Milo Authors | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.milo.opcua.sdk.server.api.methods; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.eclipse.milo.opcua.sdk.client.AddressSpace; | ||
import org.eclipse.milo.opcua.sdk.client.methods.UaMethodException; | ||
import org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode; | ||
import org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest; | ||
import org.eclipse.milo.opcua.stack.core.Identifiers; | ||
import org.eclipse.milo.opcua.stack.core.StatusCodes; | ||
import org.eclipse.milo.opcua.stack.core.UaException; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant; | ||
import org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest; | ||
import org.eclipse.milo.opcua.stack.core.types.structured.CallMethodResult; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AbstractMethodInvocationHandlerTest extends AbstractClientServerTest { | ||
|
||
@Test | ||
public void inputArgumentResultsIsEmptyOnSuccess() throws ExecutionException, InterruptedException { | ||
CallMethodResult result = client.call(new CallMethodRequest( | ||
Identifiers.ObjectsFolder, | ||
NodeId.parse("ns=2;s=onlyAcceptsPositiveInputs()"), | ||
new Variant[]{new Variant(1)} | ||
)).get(); | ||
|
||
assertEquals(StatusCode.GOOD, result.getStatusCode()); | ||
assertEquals(0, result.getInputArgumentResults().length); | ||
} | ||
|
||
@Test | ||
public void implementationCanValidateArguments() throws UaException { | ||
AddressSpace addressSpace = client.getAddressSpace(); | ||
|
||
UaObjectNode objectsNode = addressSpace.getObjectNode(Identifiers.ObjectsFolder); | ||
|
||
try { | ||
objectsNode.callMethod( | ||
new QualifiedName(2, "onlyAcceptsPositiveInputs()"), | ||
new Variant[]{new Variant(-1)} | ||
); | ||
} catch (UaMethodException e) { | ||
System.out.println("result: " + e.getStatusCode()); | ||
System.out.println("inputArgumentResults: " + Arrays.toString(e.getInputArgumentResults())); | ||
|
||
assertEquals(StatusCodes.Bad_InvalidArgument, e.getStatusCode().getValue()); | ||
assertEquals(StatusCodes.Bad_OutOfRange, e.getInputArgumentResults()[0].getValue()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...src/main/java/org/eclipse/milo/opcua/sdk/server/api/methods/InvalidArgumentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2021 the Eclipse Milo Authors | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.milo.opcua.sdk.server.api.methods; | ||
|
||
import org.eclipse.milo.opcua.stack.core.StatusCodes; | ||
import org.eclipse.milo.opcua.stack.core.UaException; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode; | ||
|
||
public class InvalidArgumentException extends UaException { | ||
|
||
private final StatusCode[] inputArgumentResults; | ||
private final DiagnosticInfo[] inputArgumentDiagnosticInfos; | ||
|
||
public InvalidArgumentException(StatusCode[] inputArgumentResults) { | ||
this(inputArgumentResults, new DiagnosticInfo[0]); | ||
} | ||
|
||
public InvalidArgumentException( | ||
StatusCode[] inputArgumentResults, | ||
DiagnosticInfo[] inputArgumentDiagnosticInfos | ||
) { | ||
|
||
super(StatusCodes.Bad_InvalidArgument, "one or more of the provided arguments is invalid"); | ||
|
||
this.inputArgumentResults = inputArgumentResults; | ||
this.inputArgumentDiagnosticInfos = inputArgumentDiagnosticInfos; | ||
} | ||
|
||
/** | ||
* Get the corresponding {@link StatusCode}s for each input argument value. | ||
* | ||
* @return the corresponding {@link StatusCode}s for each input argument value. | ||
*/ | ||
public StatusCode[] getInputArgumentResults() { | ||
return inputArgumentResults; | ||
} | ||
|
||
/** | ||
* Get the corresponding {@link DiagnosticInfo}s for each input argument value, if supported. | ||
* | ||
* @return the corresponding {@link DiagnosticInfo}s for each input argument value, if supported. | ||
*/ | ||
public DiagnosticInfo[] getInputArgumentDiagnosticInfos() { | ||
return inputArgumentDiagnosticInfos; | ||
} | ||
|
||
} |