From 1c192d2dd68724e292b6a30f463085a262e1e813 Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Wed, 15 Feb 2017 23:11:26 -0800 Subject: Moving all files to root directory Change-Id: Ica5535fd6ec85f350fe1640b42137b49f83f10f0 Signed-off-by: Patrick Brady --- .../state-machine-lib/.gitignore | 1 + .../org.eclipse.wst.common.project.facet.core.xml | 4 + .../state-machine-lib/pom.xml | 32 +++++++ .../openecomp/appc/statemachine/StateMachine.java | 29 +++++++ .../statemachine/impl/StateMachineFactory.java | 43 ++++++++++ .../appc/statemachine/impl/StateMachineImpl.java | 93 ++++++++++++++++++++ .../openecomp/appc/statemachine/objects/Event.java | 59 +++++++++++++ .../objects/InvalidInputException.java | 28 +++++++ .../appc/statemachine/objects/Response.java | 30 +++++++ .../openecomp/appc/statemachine/objects/State.java | 79 +++++++++++++++++ .../statemachine/objects/StateMachineMetadata.java | 79 +++++++++++++++++ .../statemachine/objects/StateMachineResponse.java | 48 +++++++++++ .../appc/statemachine/objects/Transition.java | 45 ++++++++++ .../appc/statemachine/TestStateMachine.java | 98 ++++++++++++++++++++++ 14 files changed, 668 insertions(+) create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/.gitignore create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/pom.xml create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/StateMachine.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineFactory.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineImpl.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Event.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/InvalidInputException.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Response.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/State.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineMetadata.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineResponse.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Transition.java create mode 100644 appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/test/java/org/openecomp/appc/statemachine/TestStateMachine.java (limited to 'appc-dispatcher/appc-dispatcher-common/state-machine-lib') diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/.gitignore b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/.gitignore new file mode 100644 index 000000000..b83d22266 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/.settings/org.eclipse.wst.common.project.facet.core.xml b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 000000000..f4ef8aa0a --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/pom.xml b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/pom.xml new file mode 100644 index 000000000..10f74c6da --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + org.openecomp.appc + appc-dispatcher-common + 1.0.0 + + state-machine-lib + bundle + + state-machine-lib + http://maven.apache.org + + + UTF-8 + + + + + org.apache.felix + maven-bundle-plugin + + + ${project.artifactId} + ${project.version} + org.openecomp.appc.statemachine.* + + + + + + diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/StateMachine.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/StateMachine.java new file mode 100644 index 000000000..daa8a1ed3 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/StateMachine.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine; + +import org.openecomp.appc.statemachine.objects.*; + + +public interface StateMachine { + StateMachineResponse handleEvent(State currentState, Event event) throws InvalidInputException; +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineFactory.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineFactory.java new file mode 100644 index 000000000..d8a135be4 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineFactory.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.impl; + +import java.util.HashSet; +import java.util.Set; + +import org.openecomp.appc.statemachine.StateMachine; +import org.openecomp.appc.statemachine.objects.Event; +import org.openecomp.appc.statemachine.objects.State; +import org.openecomp.appc.statemachine.objects.StateMachineMetadata; + + +public class StateMachineFactory { + + private StateMachineFactory(){ + + } + + public static StateMachine getStateMachine(StateMachineMetadata metadata){ + StateMachine machine = new StateMachineImpl(metadata); + return machine; + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineImpl.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineImpl.java new file mode 100644 index 000000000..f4f6612f8 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/impl/StateMachineImpl.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.impl; + +import java.util.HashSet; +import java.util.Set; + +import org.openecomp.appc.statemachine.StateMachine; +import org.openecomp.appc.statemachine.objects.*; + + +public class StateMachineImpl implements StateMachine { + + private final Set states; + + private final Set events; + + StateMachineImpl(StateMachineMetadata metadata){ + this.states = new HashSet(); + this.states.addAll(metadata.getStates()); + this.events = new HashSet(); + this.events.addAll(metadata.getEvents()); + } + + public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{ + + if(!validateInputs(inputState,event)){ + throw new InvalidInputException("VNF State or incoming event is invalid. State = " +inputState + " event = " + event ); + } + + StateMachineResponse response = new StateMachineResponse(); + State currentState = null,nextState = null; + for(State stateInSet:states){ + if(stateInSet.equals(inputState)){ + currentState = stateInSet; + break; + } + } + for(Transition transition : currentState.getTransitions()){ + if(event.equals(transition.getEvent())){ + nextState = transition.getNextState(); + } + } + if(nextState == null){ + response.setResponse(Response.NO_TRANSITION_DEFINED); + } + else if(inputState.equals(nextState)){ + response.setResponse(Response.NO_STATE_CHANGE); + } + else{ + response.setResponse(Response.VALID_TRANSITION); + } + response.setNextState(nextState); + return response; + } + + private boolean validateInputs(State state,Event event){ + if(state ==null || event == null){ + return false; + } + if(!(this.states.contains(state) && this.events.contains(event))){ + return false; + } + return true; + } + + @Override + public String toString() { + return "StateMachineImpl{" + + "states=" + states + + ", events=" + events + + '}'; + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Event.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Event.java new file mode 100644 index 000000000..377cd8138 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Event.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + +public class Event{ + + private String eventName; + + private Event(){ + + } + @Override + public int hashCode(){ + return this.eventName.hashCode(); + } + @Override + public boolean equals(Object obj){ + if(obj == null){ + return false; + } + if(!(obj instanceof Event)){ + return false; + } + Event event = (Event)obj; + return this.eventName.equals(event.getEventName()); + } + + public Event(String eventName){ + this(); + this.eventName = eventName; + } + + public String getEventName() { + return eventName; + } + @Override + public String toString(){ + return this.eventName; + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/InvalidInputException.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/InvalidInputException.java new file mode 100644 index 000000000..db6b6d3ce --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/InvalidInputException.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + +public class InvalidInputException extends Exception{ + public InvalidInputException(String message){ + super(message); + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Response.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Response.java new file mode 100644 index 000000000..8f24c46a0 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Response.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + + +public enum Response { + NO_TRANSITION_DEFINED,NO_STATE_CHANGE,VALID_TRANSITION; + public String toString(){ + return this.name(); + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/State.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/State.java new file mode 100644 index 000000000..18daef02e --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/State.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + + +public class State{ + private String stateName; + private List transitions; + + private State(){ + + } + + public State(String state){ + this(); + this.stateName = state; + this.transitions = new ArrayList(); + } + + @Override + public int hashCode(){ + return this.stateName.hashCode(); + } + + @Override + public boolean equals(Object obj){ + if(obj == null){ + return false; + } + if(!(obj instanceof State)){ + return false; + } + State state = (State)obj; + return this.stateName.equals(state.getStateName()); + } + + + public String getStateName(){ + return stateName; + } + + void addTransition(Transition transition){ + this.transitions.add(transition); + } + + public List getTransitions() { + return transitions; + } + + @Override + public String toString(){ + return this.stateName; + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineMetadata.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineMetadata.java new file mode 100644 index 000000000..6625fef19 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineMetadata.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + + +public class StateMachineMetadata { + + Set states; + Set events; + + private StateMachineMetadata(StateMachineMetadataBuilder builder){ + states = builder.states; + events = builder.events; + } + + public Set getStates() { + return states; + } + + public Set getEvents() { + return events; + } + + public static class StateMachineMetadataBuilder{ + + private Set states; + private Set events; + + public StateMachineMetadataBuilder(){ + states = new HashSet(); + events = new HashSet(); + } + + public StateMachineMetadataBuilder addState(State state){ + this.states.add(state); + return this; + } + + public StateMachineMetadataBuilder addEvent(Event event){ + this.events.add(event); + return this; + } + + public StateMachineMetadataBuilder addTransition(State currentState,Event event,State nextState){ + Transition transition = new Transition(event,nextState); + currentState.addTransition(transition); + return this; + } + + public StateMachineMetadata build(){ + StateMachineMetadata machine = new StateMachineMetadata(this); + return machine; + } + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineResponse.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineResponse.java new file mode 100644 index 000000000..ce75693fc --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/StateMachineResponse.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + + +public class StateMachineResponse { + State nextState; + Response response; + + public StateMachineResponse(){ + + } + + public State getNextState() { + return nextState; + } + + public Response getResponse() { + return response; + } + + public void setNextState(State nextState) { + this.nextState = nextState; + } + + public void setResponse(Response response) { + this.response = response; + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Transition.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Transition.java new file mode 100644 index 000000000..4a268f931 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/main/java/org/openecomp/appc/statemachine/objects/Transition.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine.objects; + + +public class Transition { + private Event event; + private State nextState; + + private Transition(){ + + } + public Transition(Event event,State nextState){ + this(); + this.event = event; + this.nextState = nextState; + } + + public Event getEvent() { + return event; + } + + public State getNextState() { + return nextState; + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/test/java/org/openecomp/appc/statemachine/TestStateMachine.java b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/test/java/org/openecomp/appc/statemachine/TestStateMachine.java new file mode 100644 index 000000000..735e8e13c --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/state-machine-lib/src/test/java/org/openecomp/appc/statemachine/TestStateMachine.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.appc.statemachine; + + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.appc.statemachine.impl.StateMachineFactory; +import org.openecomp.appc.statemachine.objects.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + + +public class TestStateMachine { + + @Test + public void handleEvent() throws InvalidInputException { + +// MetadataReader metadataReader = new MetadataReader(); +// StateMachineMetadata metadata = metadataReader.readMetadata(null); +// +// StateMachine machine = StateMachineFactory.getStateMachine(metadata); +// +// /* +// Testing Positive Scenario passing the valid events and validating the StateMachineResponse +// */ +// for(State state:metadata.getStates()){ +// +// for(Transition transition:state.getTransitions()){ +// Event event = transition.getEvent(); +// State nextState = transition.getNextState(); +// +// StateMachineResponse response = machine.handleEvent(state,event); +// Assert.assertEquals(response.getNextState(),nextState); +// Assert.assertEquals(response.getResponse(),Response.VALID_TRANSITION); +// } +// } +// +// /* +// Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in +// Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not +// registered as events in the StateMachineMetadata and validating StateMachineResponse +// */ +// for(State state:metadata.getStates()){ +// +// for(Transition transition:state.getTransitions()){ +// List negativeEvents = getNegativeEvents(state,metadata.getEvents()); +// +// for(Event negativeEvent:negativeEvents){ +// StateMachineResponse response = machine.handleEvent(state,negativeEvent); +// Assert.assertEquals(response.getNextState(),null); +// Assert.assertEquals(response.getResponse(),Response.NO_TRANSITION_DEFINED); +// +// boolean flag =false; +// try{ +// response = machine.handleEvent(state,new Event("PUT")); +// } +// catch(InvalidInputException e){ +// flag = true; +// } +// Assert.assertTrue(flag); +// +// } +// } +// } + } + +// private List getNegativeEvents(State state,Set events) { +// List negativeEventList = new ArrayList<>(); +// negativeEventList.addAll(events); +// +// for(Transition transition: state.getTransitions()){ +// negativeEventList.remove(transition.getEvent()); +// } +// return negativeEventList; +// } +} -- cgit 1.2.3-korg