Skip to content

Commit

Permalink
Inactionware#99 Add logic to check interceptor input and out meta
Browse files Browse the repository at this point in the history
  • Loading branch information
minjing committed Mar 1, 2019
1 parent 6078ee3 commit 1d05f30
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
23 changes: 23 additions & 0 deletions uapi.behavior/src/main/java/uapi/behavior/ActionInputMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package uapi.behavior;

import uapi.common.ArgumentChecker;
import uapi.common.StringHelper;

/**
* The meta class hold bass information for action input argument.
Expand All @@ -33,4 +34,26 @@ public ActionInputMeta(
public Class<?> type() {
return this._type;
}

@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (! (obj instanceof ActionInputMeta)) {
return false;
}
ActionInputMeta other = (ActionInputMeta) obj;
return this._type.equals(other._type);
}

@Override
public int hashCode() {
return this._type.hashCode();
}

@Override
public String toString() {
return StringHelper.makeString("ActionInputMeta[type={}]", this._type.getCanonicalName());
}
}
61 changes: 61 additions & 0 deletions uapi.behavior/src/main/java/uapi/behavior/BehaviorErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package uapi.behavior;

import uapi.common.CollectionHelper;
import uapi.exception.FileBasedExceptionErrors;
import uapi.exception.IndexedParameters;

Expand Down Expand Up @@ -61,6 +62,8 @@ public class BehaviorErrors extends FileBasedExceptionErrors<BehaviorException>
public static final int RESERVED_ACTION_OUTPUT_NAME = 36;
public static final int UNKNOWN_FAILURE_ON_INTERCEPTOR = 37;
public static final int INCORRECT_ACTION_OUTPUT_NAME = 38;
public static final int INCONSISTENT_INTERCEPTOR_INPUT_METAS = 39;
public static final int INTERCEPTOR_HAS_OUTPU_META = 40;

private static final Map<Integer, String> keyCodeMapping;

Expand Down Expand Up @@ -104,6 +107,8 @@ public class BehaviorErrors extends FileBasedExceptionErrors<BehaviorException>
keyCodeMapping.put(RESERVED_ACTION_OUTPUT_NAME, ReservedActionOutputName.KEY);
// keyCodeMapping.put(UNKNOWN_FAILURE_ON_INTERCEPTOR, UnknownFailureOnInterceptor.KEY);
// keyCodeMapping.put(INCORRECT_ACTION_OUTPUT_NAME, IncorrectActionOutputName.KEY);
keyCodeMapping.put(INCONSISTENT_INTERCEPTOR_INPUT_METAS, InconsistentInterceptorInputMetas.KEY);
keyCodeMapping.put(INTERCEPTOR_HAS_OUTPU_META, InterceptorHasOutputMeta.KEY);
}

public BehaviorErrors() {
Expand Down Expand Up @@ -1140,4 +1145,60 @@ public Object[] get() {
// return new Object[] { this._outputName, this._actionId };
// }
// }

/**
* Error string template:
* The Interceptor input metas {} does not match intercepted action input metas {}
*/
public static final class InconsistentInterceptorInputMetas extends IndexedParameters<InconsistentInterceptorInputMetas> {

public static final String KEY = "InconsistentInterceptorInputMetas";

private ActionInputMeta[] _iInputMetas;
private ActionInputMeta[] _aInputMetas;

public InconsistentInterceptorInputMetas interceptorInputMetas(final ActionInputMeta[] inputMetas) {
this._iInputMetas = inputMetas;
return this;
}

public InconsistentInterceptorInputMetas intercepedActionInputMetas(final ActionInputMeta[] inputMetas) {
this._aInputMetas = inputMetas;
return this;
}

@Override
public Object[] get() {
return new Object[] {
CollectionHelper.asString(this._iInputMetas),
CollectionHelper.asString(this._aInputMetas)
};
}
}

/**
* Error String template:
* Interceptor does not support output, interceptor - {}, output meta - {}
*/
public static final class InterceptorHasOutputMeta extends IndexedParameters<InterceptorHasOutputMeta> {

public static final String KEY = "InterceptorHasOutputMeta";

private ActionIdentify _interceptorId;
private ActionOutputMeta[] _outMetas;

public InterceptorHasOutputMeta interceptorId(final ActionIdentify actionId) {
this._interceptorId = actionId;
return this;
}

public InterceptorHasOutputMeta outputMeta(final ActionOutputMeta[] outputMetas) {
this._outMetas = outputMetas;
return this;
}

public Object[] get() {
return new Object[] { this._interceptorId, CollectionHelper.asString(this._outMetas) };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ private void newNextAction(
.build();
}
}

// create new action holder
if (action instanceof IIntercepted) {
this._current = new InterceptedActionHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package uapi.behavior.internal;

import uapi.behavior.*;
import uapi.common.CollectionHelper;
import uapi.common.Functionals;
import uapi.common.Repository;
import uapi.rx.Looper;
Expand Down Expand Up @@ -50,6 +51,24 @@ public class InterceptedActionHolder extends ActionHolder {
.actionId(interceptorId))
.build();
}
// Check interceptor which input metas should be same as intercepted action's input meta
if (CollectionHelper.equals(action.inputMetas(), interceptor.inputMetas())) {
throw BehaviorException.builder()
.errorCode(BehaviorErrors.INCONSISTENT_INTERCEPTOR_INPUT_METAS)
.variables(new BehaviorErrors.InconsistentInterceptorInputMetas()
.interceptorInputMetas(interceptor.inputMetas())
.intercepedActionInputMetas(action.inputMetas()))
.build();
}
// The interceptor output must be 0
if (interceptor.outputMetas().length != 0) {
throw BehaviorException.builder()
.errorCode(BehaviorErrors.INTERCEPTOR_HAS_OUTPU_META)
.variables(new BehaviorErrors.InterceptorHasOutputMeta()
.interceptorId(interceptor.getId())
.outputMeta(interceptor.outputMetas()))
.build();
}
return (IInterceptor) interceptor;
}).toArray();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public String name() {
@Override
public IBehaviorBuilder newBehavior(
final String name,
final String eventType
final String eventTopic
) throws BehaviorException {
return newBehavior(name, BehaviorEvent.class, eventType);
return newBehavior(name, BehaviorEvent.class, eventTopic);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions uapi.behavior/src/main/resources/behaviorErrors.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ InputObjectTypeMismatch = The input object [type: {}, value: {}] can
ReservedActionOutputName = The action output name is reserved by system - {}
UnknownFailureOnInterceptor = Execute Interceptor [{}} failed, intercepted action - {}
IncorrectActionOutputName = Can not set output to output name [{}] on action [{}] - unsupported output name
InconsistentInterceptorInputMetas = The Interceptor input metas {} does not match intercepted action input metas {}
InterceptorHasOutputMeta = Interceptor does not support output, interceptor - {}, output meta - {}

0 comments on commit 1d05f30

Please sign in to comment.