From: xuegao Date: Fri, 11 Oct 2019 13:41:49 +0000 (+0200) Subject: Add unit tests X-Git-Tag: 5.0.0~124^2~1 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=524b364a12f834e826f4e8be1119b61ba14b503c;p=clamp.git Add unit tests Add unit tests for backend and frontend. Issue-ID: CLAMP-512 Change-Id: I713ba2dce3bd132e7e3e713f4e91e3662df7aafd Signed-off-by: xuegao --- diff --git a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java index 9321423a..44ee5226 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java @@ -52,6 +52,8 @@ public class PolicyComponent extends ExternalComponent { "The policies defined have been created but NOT deployed on the policy engine", 50); public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED", "The policies defined have been created and deployed on the policy engine", 10); + public static final ExternalComponentState UNKNOWN = new ExternalComponentState("UNKNOWN", + "The current status is not clear. Need to regresh the status to get the current status.", 0); /** * Default constructor. @@ -62,7 +64,7 @@ public class PolicyComponent extends ExternalComponent { * on by one, each time we increase the level we can't decrease it anymore. * That's why it starts with the lowest one SENT_AND_DEPLOYED. */ - super(SENT_AND_DEPLOYED); + super(UNKNOWN); } @Override 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 8745cc5d..072d5771 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 authList = new LinkedList(); 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 authorityList = new LinkedList(); /** * 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 authList = new LinkedList(); + 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)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)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)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)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)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)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 00000000..560f54cf --- /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 0a3c1e16..557fdcec 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 00000000..34fcc077 --- /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 00000000..e822dfb1 --- /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"); + } +} diff --git a/ui-react/src/LoopUI.test.js b/ui-react/src/LoopUI.test.js new file mode 100644 index 00000000..e28096bd --- /dev/null +++ b/ui-react/src/LoopUI.test.js @@ -0,0 +1,171 @@ +/*- + * ============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============================================ + * =================================================================== + * + */ +import React from 'react'; +import { shallow } from 'enzyme'; +import LoopUI from './LoopUI'; + +import LoopCache from './api/LoopCache'; +import LoopActionService from './api/LoopActionService'; +import LoopService from './api/LoopService'; + +describe('Verify LoopUI', () => { + beforeEach(() => { + fetch.resetMocks(); + fetch.mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => "testUser" + + }); + }); + }) + + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "components": { + "POLICY": { + "componentState": { + "stateName": "UNKNOWN", + "description": "The policies defined have NOT yet been created on the policy engine" + } + }, + "DCAE": { + "componentState": { + "stateName": "BLUEPRINT_DEPLOYED", + "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" + } + } + } + }); + + it('Test the render method', async () => { + const flushPromises = () => new Promise(setImmediate); + + const component = shallow() + component.setState({ + loopName: "testLoopName", + showAlert: false + }); + await flushPromises(); + expect(component).toMatchSnapshot(); + }); + + test('Test closeLoop method', () => { + const historyMock = { push: jest.fn() }; + const component = shallow() + const instance = component.instance(); + instance.closeLoop(); + + expect(component.state('loopName')).toEqual("Empty (NO loop loaded yet)"); + expect(historyMock.push.mock.calls[0]).toEqual([ '/']); + }) + + test('Test logout method', async () => { + const flushPromises = () => new Promise(setImmediate); + const component = shallow() + const instance = component.instance(); + instance.logout(); + await flushPromises(); + expect(component.state('userName')).toEqual("testUser"); + }) + + test('Test loadLoop method refresh suc', async () => { + const historyMock = { push: jest.fn() }; + LoopService.getLoop = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => {} + }); + }); + + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.resolve({name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"}); + }); + + const flushPromises = () => new Promise(setImmediate); + const component = shallow() + const instance = component.instance(); + instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + + await flushPromises(); + + const resLoopCache = instance.getLoopCache(); + + expect(resLoopCache.getComponentStates()).toBeUndefined(); + expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + }) + + test('Test loadLoop method refresh fail', async () => { + const historyMock = { push: jest.fn() }; + LoopService.getLoop = jest.fn().mockImplementation(() => { + return Promise.resolve({ + name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "components": { + "POLICY": { + "componentState": { + "stateName": "UNKNOWN", + "description": "The policies defined have NOT yet been created on the policy engine" + } + }, + "DCAE": { + "componentState": { + "stateName": "BLUEPRINT_DEPLOYED", + "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" + } + } + }}); + }); + + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.reject({error: "whatever"}); + }); + + const flushPromises = () => new Promise(setImmediate); + const component = shallow() + const instance = component.instance(); + instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + + await flushPromises(); + + const resLoopCache = instance.getLoopCache(); + + expect(resLoopCache).toEqual(loopCache); + expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + }) + + test('Test alert methods', () => { + const component = shallow() + expect(component.state('showAlert')).toEqual(false); + + const instance = component.instance(); + instance.showAlert("testAlert"); + expect(component.state('showAlert')).toEqual(true); + expect(component.state('showMessage')).toEqual("testAlert"); + + instance.disableAlert(); + + expect(component.state('showAlert')).toEqual(false); + }) +}); diff --git a/ui-react/src/NotFound.test.js b/ui-react/src/NotFound.test.js new file mode 100644 index 00000000..3a5fc107 --- /dev/null +++ b/ui-react/src/NotFound.test.js @@ -0,0 +1,36 @@ +/*- + * ============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============================================ + * =================================================================== + * + */ +import React from 'react'; +import { shallow } from 'enzyme'; +import NotFound from './NotFound'; + +describe('Verify OnapClamp', () => { + + it('Test the render method', () => { + + const component = shallow() + + expect(component).toMatchSnapshot(); + }); + +}); diff --git a/ui-react/src/OnapClamp.test.js b/ui-react/src/OnapClamp.test.js new file mode 100644 index 00000000..c3336a9a --- /dev/null +++ b/ui-react/src/OnapClamp.test.js @@ -0,0 +1,36 @@ +/*- + * ============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============================================ + * =================================================================== + * + */ +import React from 'react'; +import { shallow } from 'enzyme'; +import OnapClamp from './OnapClamp'; + +describe('Verify OnapClamp', () => { + + it('Test the render method', () => { + + const component = shallow() + + expect(component).toMatchSnapshot(); + }); + +}); diff --git a/ui-react/src/__snapshots__/LoopUI.test.js.snap b/ui-react/src/__snapshots__/LoopUI.test.js.snap new file mode 100644 index 00000000..ecf439e4 --- /dev/null +++ b/ui-react/src/__snapshots__/LoopUI.test.js.snap @@ -0,0 +1,154 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify LoopUI Test the render method 1`] = ` + + + + + + + + + + + + + + + + + + + + + + CLAMP + + + + + + + Signed in as: + + + testUser + + + (logout) + + + + + + Loop Viewer - + testLoopName + + + + + + + + +`; diff --git a/ui-react/src/__snapshots__/NotFound.test.js.snap b/ui-react/src/__snapshots__/NotFound.test.js.snap new file mode 100644 index 00000000..86bcfd1c --- /dev/null +++ b/ui-react/src/__snapshots__/NotFound.test.js.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify OnapClamp Test the render method 1`] = ` +
+
+ + Page Not Found! + +
+
+ Please cick + + here + + to go back to the main page. +
+
+`; diff --git a/ui-react/src/__snapshots__/OnapClamp.test.js.snap b/ui-react/src/__snapshots__/OnapClamp.test.js.snap new file mode 100644 index 00000000..751c94b3 --- /dev/null +++ b/ui-react/src/__snapshots__/OnapClamp.test.js.snap @@ -0,0 +1,176 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify OnapClamp Test the render method 1`] = ` + + + + + + + + + + + + + + + + + + + + + + + CLAMP + + + + + + + Signed in as: + + + + (logout) + + + + + + Loop Viewer - + Empty (NO loop loaded yet) + + + + + + + + + +`;