diff options
Diffstat (limited to 'src/test/java')
5 files changed, 612 insertions, 28 deletions
diff --git a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java index 8745cc5d2..072d57712 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java @@ -23,10 +23,13 @@ package org.onap.clamp.clds.it; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.IOException; import java.io.InputStream; @@ -35,6 +38,7 @@ import java.util.List; import java.util.Properties; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.NotAuthorizedException; import org.junit.Before; import org.junit.Test; @@ -44,7 +48,6 @@ import org.mockito.Mockito; import org.onap.clamp.clds.model.CldsInfo; import org.onap.clamp.clds.service.CldsService; import org.onap.clamp.clds.util.LoggingUtils; -import org.onap.clamp.clds.util.ResourceFileUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -66,15 +69,12 @@ public class CldsServiceItCase { @Autowired private CldsService cldsService; - private String bpmnText; - private String imageText; - private String bpmnPropText; - private String docText; - private Authentication authentication; - private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>(); private LoggingUtils util; - + private SecurityContext securityContext = mock(SecurityContext.class); + private Authentication auth = Mockito.mock(Authentication.class); + private UserDetails userDetails = Mockito.mock(UserDetails.class); + private List<GrantedAuthority> authorityList = new LinkedList<GrantedAuthority>(); /** * Setup the variable before the tests execution. * @@ -82,20 +82,6 @@ public class CldsServiceItCase { */ @Before public void setupBefore() throws IOException { - bpmnText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml"); - imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml"); - bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json"); - docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml"); - - authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*")); - authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read")); - authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update")); - authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read")); - authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update")); - authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*")); - authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*")); - authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList); - util = Mockito.mock(LoggingUtils.class); Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class)); cldsService.setLoggingUtil(util); @@ -104,12 +90,9 @@ public class CldsServiceItCase { @Test public void testCldsInfoNotAuthorized() { - SecurityContext securityContext = Mockito.mock(SecurityContext.class); - Authentication localAuth = Mockito.mock(Authentication.class); - UserDetails userDetails = Mockito.mock(UserDetails.class); Mockito.when(userDetails.getUsername()).thenReturn("admin"); - Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth); - Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails); + Mockito.when(securityContext.getAuthentication()).thenReturn(auth); + Mockito.when(auth.getPrincipal()).thenReturn(userDetails); cldsService.setSecurityContext(securityContext); CldsInfo cldsInfo = cldsService.getCldsInfo(); @@ -121,7 +104,17 @@ public class CldsServiceItCase { @Test public void testCldsInfoAuthorized() throws Exception { - SecurityContext securityContext = Mockito.mock(SecurityContext.class); + Authentication authentication; + List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>(); + authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*")); + authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read")); + authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update")); + authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read")); + authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update")); + authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*")); + authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*")); + authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList); + Mockito.when(securityContext.getAuthentication()).thenReturn(authentication); cldsService.setSecurityContext(securityContext); @@ -138,4 +131,110 @@ public class CldsServiceItCase { assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version")); assertEquals(cldsInfo.getUserName(), "admin"); } + + @Test(expected = NotAuthorizedException.class) + public void isAuthorizedForVfTestNotAuthorized1() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + when(securityContext.getAuthentication()).thenReturn(auth); + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test(expected = NotAuthorizedException.class) + public void isAuthorizedForVfTestNotAuthorized2() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|prod|*")); + when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList); + when(securityContext.getAuthentication()).thenReturn(auth); + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test(expected = NotAuthorizedException.class) + public void isAuthorizedForVfTestNotAuthorized3() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|testId2")); + when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList); + when(securityContext.getAuthentication()).thenReturn(auth); + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test(expected = NullPointerException.class) + public void isAuthorizedForVfTestNotAuthorized4() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + when(securityContext.getAuthentication()).thenReturn(null); + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test + public void isAuthorizedForVfTest1() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|*|*")); + when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList); + when(securityContext.getAuthentication()).thenReturn(auth); + + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test + public void isAuthorizedForVfTest2() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*")); + when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList); + when(securityContext.getAuthentication()).thenReturn(auth); + + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test + public void isAuthorizedForVfTest3() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|testId")); + when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList); + when(securityContext.getAuthentication()).thenReturn(auth); + + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test + public void isAuthorizedForVfTest4() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|*|testId")); + when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList); + when(securityContext.getAuthentication()).thenReturn(auth); + + cldsService.setSecurityContext(securityContext); + boolean res = cldsService.isAuthorizedForVf("testId"); + assertThat(res).isTrue(); + } + + @Test + public void getUserIdTest() throws Exception { + when(userDetails.getUsername()).thenReturn("testName"); + when(auth.getPrincipal()).thenReturn(userDetails); + when(securityContext.getAuthentication()).thenReturn(auth); + + cldsService.setSecurityContext(securityContext); + assertThat(cldsService.getUserId()).isEqualTo("testName"); + } } diff --git a/src/test/java/org/onap/clamp/clds/model/jsontype/JsonTypeDescriptorTest.java b/src/test/java/org/onap/clamp/clds/model/jsontype/JsonTypeDescriptorTest.java new file mode 100644 index 000000000..560f54cf4 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/model/jsontype/JsonTypeDescriptorTest.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Samsung. 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.clamp.clds.model.jsontype; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.gson.JsonObject; + +import org.hibernate.HibernateException; +import org.junit.Test; +import org.onap.clamp.dao.model.jsontype.JsonTypeDescriptor; + +public class JsonTypeDescriptorTest { + + private JsonTypeDescriptor descriptor = new JsonTypeDescriptor(); + + @Test + public void testFromString() { + JsonObject object = new JsonObject(); + object.addProperty("one","oneValue"); + JsonObject child = new JsonObject(); + child.addProperty("two","twoValue"); + object.add("child",child); + + JsonObject jsonResult = descriptor.fromString("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}"); + + assertThat(jsonResult).isEqualTo(object); + } + + @Test + public void testUnwrap() { + JsonObject res1 = descriptor.unwrap(null, null, null); + assertThat(res1).isNull(); + + JsonObject object = new JsonObject(); + object.addProperty("one","oneValue"); + JsonObject child = new JsonObject(); + child.addProperty("two","twoValue"); + object.add("child",child); + String res2 = descriptor.unwrap(object, String.class, null); + assertThat(res2.replace("\n", "").replace(" ", "")) + .isEqualTo("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}"); + + Object res3 = descriptor.unwrap(object, JsonObject.class, null); + String res3Str = ((String) res3).replace(" ", "").replace("\\n", "").replace("\\", "") + .replace("\"{", "{").replace("}\"", "}"); + assertThat(res3Str).isEqualTo("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}"); + } + + @Test(expected = HibernateException.class) + public void testUnwrapExpectationThrown() { + JsonObject object = new JsonObject(); + object.addProperty("one","oneValue"); + + descriptor.unwrap(object, Integer.class, null); + } + + @Test + public void testWrap() { + JsonObject res1 = descriptor.wrap(null, null); + assertThat(res1).isNull(); + + JsonObject object = new JsonObject(); + object.addProperty("one","oneValue"); + JsonObject child = new JsonObject(); + child.addProperty("two","twoValue"); + object.add("child",child); + JsonObject res2 = descriptor.wrap("{\"one\":\"oneValue\",\"child\":{\"two\":\"twoValue\"}}", null); + assertThat(res2).isEqualTo(object); + } + + @Test(expected = HibernateException.class) + public void testWrapExpectationThrown() { + descriptor.wrap(1, null); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/DcaeComponentTest.java b/src/test/java/org/onap/clamp/loop/DcaeComponentTest.java index 0a3c1e167..557fdcecf 100644 --- a/src/test/java/org/onap/clamp/loop/DcaeComponentTest.java +++ b/src/test/java/org/onap/clamp/loop/DcaeComponentTest.java @@ -31,9 +31,13 @@ import com.google.gson.JsonObject; import java.io.IOException; import java.util.HashSet; +import org.apache.camel.Exchange; +import org.apache.camel.Message; import org.junit.Test; +import org.mockito.Mockito; import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse; import org.onap.clamp.loop.components.external.DcaeComponent; +import org.onap.clamp.loop.components.external.ExternalComponentState; import org.onap.clamp.policy.microservice.MicroServicePolicy; public class DcaeComponentTest { @@ -90,4 +94,61 @@ public class DcaeComponentTest { assertThat(unDeploymentPayload).isEqualTo(expectedPayload); } + @Test + public void computeStateTest() throws IOException { + Exchange exchange = Mockito.mock(Exchange.class); + Message message = Mockito.mock(Message.class); + Exchange exchange2 = Mockito.mock(Exchange.class); + Mockito.when(exchange.getIn()).thenReturn(message); + Mockito.when(message.getExchange()).thenReturn(exchange2); + Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(null); + + DcaeComponent dcae = new DcaeComponent(); + + // initial state + ExternalComponentState state = dcae.computeState(exchange); + assertThat(state.getStateName()).isEqualTo("BLUEPRINT_DEPLOYED"); + + // OperationalType = install + DcaeOperationStatusResponse dcaeResponse = Mockito.mock(DcaeOperationStatusResponse.class); + Mockito.when(dcaeResponse.getOperationType()).thenReturn("install"); + + Mockito.when(dcaeResponse.getStatus()).thenReturn("succeeded"); + Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(dcaeResponse); + ExternalComponentState state2 = dcae.computeState(exchange); + assertThat(state2.getStateName()).isEqualTo("MICROSERVICE_INSTALLED_SUCCESSFULLY"); + Mockito.when(dcaeResponse.getStatus()).thenReturn("processing"); + ExternalComponentState state3 = dcae.computeState(exchange); + assertThat(state3.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_INSTALLATION"); + + Mockito.when(dcaeResponse.getStatus()).thenReturn("failed"); + ExternalComponentState state4 = dcae.computeState(exchange); + assertThat(state4.getStateName()).isEqualTo("MICROSERVICE_INSTALLATION_FAILED"); + + // OperationalType = uninstall + Mockito.when(dcaeResponse.getOperationType()).thenReturn("uninstall"); + + Mockito.when(dcaeResponse.getStatus()).thenReturn("succeeded"); + Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(dcaeResponse); + ExternalComponentState state5 = dcae.computeState(exchange); + assertThat(state5.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLED_SUCCESSFULLY"); + + Mockito.when(dcaeResponse.getStatus()).thenReturn("processing"); + ExternalComponentState state6 = dcae.computeState(exchange); + assertThat(state6.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_UNINSTALLATION"); + + Mockito.when(dcaeResponse.getStatus()).thenReturn("failed"); + ExternalComponentState state7 = dcae.computeState(exchange); + assertThat(state7.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLATION_FAILED"); + + // error cases + Mockito.when(dcaeResponse.getOperationType()).thenReturn("whatever"); + ExternalComponentState state8 = dcae.computeState(exchange); + assertThat(state8.getStateName()).isEqualTo("IN_ERROR"); + + Mockito.when(dcaeResponse.getOperationType()).thenReturn("install"); + Mockito.when(dcaeResponse.getStatus()).thenReturn("anythingelse"); + ExternalComponentState state9 = dcae.computeState(exchange); + assertThat(state9.getStateName()).isEqualTo("IN_ERROR"); + } } diff --git a/src/test/java/org/onap/clamp/loop/ExternalComponentStateTest.java b/src/test/java/org/onap/clamp/loop/ExternalComponentStateTest.java new file mode 100644 index 000000000..34fcc077f --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/ExternalComponentStateTest.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.loop; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.onap.clamp.loop.components.external.ExternalComponentState; + +public class ExternalComponentStateTest { + private ExternalComponentState state = new ExternalComponentState("NOT_SENT", + "The policies defined have NOT yet been created on the policy engine", 90); + + @Test + public void generalTest() { + assertThat(state.toString()).isEqualTo("NOT_SENT"); + state.setLevel(70); + assertThat(state.getLevel()).isEqualTo(70); + } + + @Test + public void equalsTest() { + assertThat(state.equals(null)).isEqualTo(false); + + ExternalComponentState state2 = new ExternalComponentState("NOT_SENT", + "The policies defined have NOT yet been created on the policy engine", 90); + assertThat(state.equals(state2)).isEqualTo(true); + + assertThat(state.equals(12)).isEqualTo(false); + + state2.setLevel(70); + assertThat(state.equals(state2)).isEqualTo(true); + + ExternalComponentState state3 = new ExternalComponentState("SENT", + "The policies defined have NOT yet been created on the policy engine", 90); + assertThat(state.equals(state3)).isEqualTo(false); + + ExternalComponentState state4 = new ExternalComponentState(null, + "The policies defined have NOT yet been created on the policy engine", 90); + ExternalComponentState state5 = new ExternalComponentState(null, + "The policies defined have NOT yet been", 50); + assertThat(state4.equals(state3)).isEqualTo(false); + assertThat(state4.equals(state5)).isEqualTo(true); + } + + @Test + public void compareToTest() { + ExternalComponentState state2 = new ExternalComponentState("NOT_SENT", + "The policies defined have NOT yet been created on the policy engine", 90); + assertThat(state.compareTo(state2)).isEqualTo(0); + + ExternalComponentState state3 = new ExternalComponentState("SENT", + "The policies defined have NOT yet been created on the policy engine", 50); + assertThat(state.compareTo(state3)).isEqualTo(1); + + ExternalComponentState state4 = new ExternalComponentState(null, + "The policies defined have NOT yet been created on the policy engine", 100); + assertThat(state.compareTo(state4)).isEqualTo(-1); + + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java b/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java new file mode 100644 index 000000000..e822dfb1a --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java @@ -0,0 +1,246 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.loop; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.clamp.loop.components.external.ExternalComponentState; +import org.onap.clamp.loop.components.external.PolicyComponent; + +public class PolicyComponentTest { + + /** + * Test the computeState method. + * oldState newState expectedFinalState + * NOT_SENT SENT_AND_DEPLOYED NOT_SENT + * NOT_SENT SENT NOT_SENT + * NOT_SENT NOT_SENT NOT_SENT + * NOT_SENT IN_ERROR IN_ERROR + */ + @Test + public void computeStateTestOriginalStateUnknown() { + Exchange exchange = Mockito.mock(Exchange.class); + Message message = Mockito.mock(Message.class); + Exchange exchange2 = Mockito.mock(Exchange.class); + Mockito.when(exchange.getIn()).thenReturn(message); + Mockito.when(message.getExchange()).thenReturn(exchange2); + // policy found + deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + PolicyComponent policy = new PolicyComponent(); + + ExternalComponentState state = policy.computeState(exchange); + assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED"); + // policy found + not deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state2 = policy.computeState(exchange); + assertThat(state2.getStateName()).isEqualTo("SENT"); + // policy not found + not deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state4 = policy.computeState(exchange); + assertThat(state4.getStateName()).isEqualTo("NOT_SENT"); + // policy not found + deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state3 = policy.computeState(exchange); + assertThat(state3.getStateName()).isEqualTo("IN_ERROR"); + } + /** + * Test the computeState method. + * oldState newState expectedFinalState + * NOT_SENT SENT_AND_DEPLOYED NOT_SENT + * NOT_SENT SENT NOT_SENT + * NOT_SENT NOT_SENT NOT_SENT + * NOT_SENT IN_ERROR IN_ERROR + */ + @Test + public void computeStateTestOriginalStateNotSent() { + Exchange exchange = Mockito.mock(Exchange.class); + Message message = Mockito.mock(Message.class); + Exchange exchange2 = Mockito.mock(Exchange.class); + Mockito.when(exchange.getIn()).thenReturn(message); + Mockito.when(message.getExchange()).thenReturn(exchange2); + // policy found + deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + PolicyComponent policy = new PolicyComponent(); + ExternalComponentState notSent = new ExternalComponentState("NOT_SENT", + "The policies defined have NOT yet been created on the policy engine", 90); + policy.setState(notSent); + ExternalComponentState state = policy.computeState(exchange); + assertThat(state.getStateName()).isEqualTo("NOT_SENT"); + // policy found + not deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state2 = policy.computeState(exchange); + assertThat(state2.getStateName()).isEqualTo("NOT_SENT"); + // policy not found + not deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state4 = policy.computeState(exchange); + assertThat(state4.getStateName()).isEqualTo("NOT_SENT"); + // policy not found + deployed + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state3 = policy.computeState(exchange); + assertThat(state3.getStateName()).isEqualTo("IN_ERROR"); + } + + + /** + * Test the computeState method. + * oldState newState expectedFinalState + * SENT SENT SENT + * SENT SENT_AND_DEPLOYED SENT + * SENT IN_ERROR IN_ERROR + * SENT NOT_SENT NOT_SENT + */ + @Test + public void computeStateTestOriginalStateSent() throws IOException { + Exchange exchange = Mockito.mock(Exchange.class); + Message message = Mockito.mock(Message.class); + Exchange exchange2 = Mockito.mock(Exchange.class); + Mockito.when(exchange.getIn()).thenReturn(message); + Mockito.when(message.getExchange()).thenReturn(exchange2); + PolicyComponent policy = new PolicyComponent(); + ExternalComponentState sent = new ExternalComponentState("SENT", + "The policies defined have been created but NOT deployed on the policy engine", 50); + policy.setState(sent); + // new policy state SENT + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state = policy.computeState(exchange); + assertThat(state.getStateName()).isEqualTo("SENT"); + // new policy state SENT_AND_DEPLOYED + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state2 = policy.computeState(exchange); + assertThat(state2.getStateName()).isEqualTo("SENT"); + // new policy state IN_ERROR + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state3 = policy.computeState(exchange); + assertThat(state3.getStateName()).isEqualTo("IN_ERROR"); + // new policy state NOT_SENT + policy.setState(sent); + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state4 = policy.computeState(exchange); + assertThat(state4.getStateName()).isEqualTo("NOT_SENT"); + } + + /** + * Test the computeState method. + * oldState newState expectedFinalState + * SENT_AND_DEPLOYED SENT_AND_DEPLOYED SENT_AND_DEPLOYED + * SENT_AND_DEPLOYED SENT SENT + * SENT_AND_DEPLOYED IN_ERROR IN_ERROR + * SENT_AND_DEPLOYED NOT_SENT NOT_SENT + */ + @Test + public void computeStateTestOriginalStateSentAndDeployed() throws IOException { + Exchange exchange = Mockito.mock(Exchange.class); + Message message = Mockito.mock(Message.class); + Exchange exchange2 = Mockito.mock(Exchange.class); + Mockito.when(exchange.getIn()).thenReturn(message); + Mockito.when(message.getExchange()).thenReturn(exchange2); + PolicyComponent policy = new PolicyComponent(); + ExternalComponentState sendDeployed = new ExternalComponentState("SENT_AND_DEPLOYED", + "The policies defined have been created and deployed on the policy engine", 10); + policy.setState(sendDeployed); + // new policy state SENT_AND_DEPLOYED + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state = policy.computeState(exchange); + assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED"); + // new policy state SENT + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state2 = policy.computeState(exchange); + assertThat(state2.getStateName()).isEqualTo("SENT"); + // new policy state IN_ERROR + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state3 = policy.computeState(exchange); + assertThat(state3.getStateName()).isEqualTo("IN_ERROR"); + // new policy state NOT_SENT + policy.setState(sendDeployed); + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state4 = policy.computeState(exchange); + assertThat(state4.getStateName()).isEqualTo("NOT_SENT"); + } + + + /** + * Test the computeState method. + * oldState newState expectedFinalState + * IN_ERROR SENT_AND_DEPLOYED IN_ERROR + * IN_ERROR SENT IN_ERROR + * IN_ERROR IN_ERROR IN_ERROR + * IN_ERROR NOT_SENT IN_ERROR + */ + @Test + public void computeStateTestOriginalStateInError() throws IOException { + Exchange exchange = Mockito.mock(Exchange.class); + Message message = Mockito.mock(Message.class); + Exchange exchange2 = Mockito.mock(Exchange.class); + Mockito.when(exchange.getIn()).thenReturn(message); + Mockito.when(message.getExchange()).thenReturn(exchange2); + PolicyComponent policy = new PolicyComponent(); + ExternalComponentState inError = new ExternalComponentState("IN_ERROR", + "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", + 100); + policy.setState(inError); + // new policy state SENT_AND_DEPLOYED + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state = policy.computeState(exchange); + assertThat(state.getStateName()).isEqualTo("IN_ERROR"); + // new policy state SENT + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state2 = policy.computeState(exchange); + assertThat(state2.getStateName()).isEqualTo("IN_ERROR"); + // new policy state IN_ERROR + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true); + ExternalComponentState state3 = policy.computeState(exchange); + assertThat(state3.getStateName()).isEqualTo("IN_ERROR"); + // new policy state NOT_SENT + Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false); + Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false); + ExternalComponentState state4 = policy.computeState(exchange); + + assertThat(state4.getStateName()).isEqualTo("IN_ERROR"); + } +} |