diff options
Diffstat (limited to 'src/test/java/org')
5 files changed, 142 insertions, 23 deletions
diff --git a/src/test/java/org/onap/clamp/clds/it/AuthorizationControllerItCase.java b/src/test/java/org/onap/clamp/clds/it/AuthorizationControllerItCase.java new file mode 100644 index 000000000..477c71a0d --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/it/AuthorizationControllerItCase.java @@ -0,0 +1,94 @@ +/*- + * ============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.clds.it; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.clamp.authorization.AuthorizationController; +import org.onap.clamp.clds.service.SecureServicePermission; +import org.onap.clamp.util.PrincipalUtils; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.userdetails.User; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB + * and stored procedures. + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class AuthorizationControllerItCase { + + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(AuthorizationControllerItCase.class); + private Authentication authentication; + private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>(); + + /** + * Setup the variable before the tests execution. + * + * @throws IOException + * In case of issues when opening the files + */ + @Before + public void setupBefore() throws IOException { + 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); + } + + @Test + public void testIsUserPermittedNoException() { + SecurityContext securityContext = Mockito.mock(SecurityContext.class); + Mockito.when(securityContext.getAuthentication()).thenReturn(authentication); + PrincipalUtils.setSecurityContext(securityContext); + + AuthorizationController auth = new AuthorizationController (); + assertTrue(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-cl","dev","read"))); + assertTrue(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-cl-manage","dev","DEPLOY"))); + assertTrue(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-filter-vf","dev","12345-55555-55555-5555"))); + assertFalse(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-cl","test","read"))); + } +} diff --git a/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java b/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java index 58b922e04..55657c974 100644 --- a/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java @@ -49,13 +49,10 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller") public class SdcSingleControllerItCase { - private static final String SDC_FOLDER = "/tmp/csar-handler-tests"; private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar"; private static final String SERVICE_UUID = "serviceUUID"; private static final String RESOURCE1_UUID = "resource1UUID"; private static final String RESOURCE1_INSTANCE_NAME = "sim-1802 0"; - private static final String RESOURCE1_INSTANCE_NAME_IN_CSAR = "sim18020"; - private static final String BLUEPRINT1_NAME = "FOI.Simfoimap223S0112.event_proc_bp.yaml"; @Autowired private ClampProperties clampProp; diff --git a/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java index ce8a493da..d3a823fbc 100644 --- a/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java @@ -30,6 +30,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -68,7 +69,6 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller") public class CsarInstallerItCase { - private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar"; private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92"; private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec"; private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad"; @@ -89,7 +89,8 @@ public class CsarInstallerItCase { blueprintMap.put("resourceid", blueprintArtifact); Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap); Mockito.when(blueprintArtifact.getDcaeBlueprint()).thenReturn( - IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml"))); + IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml"), + StandardCharsets.UTF_8)); csarInstaller.installTheCsar(csarHandler); fail("Should have raised an SdcArtifactInstallerException"); } @@ -164,16 +165,17 @@ public class CsarInstallerItCase { csarInstaller.installTheCsar(csar); CldsModel cldsModel1 = verifyClosedLoopModelLoadedInDb(csar, "tca.yaml"); JSONAssert.assertEquals( - IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")), + IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json"), + StandardCharsets.UTF_8), cldsModel1.getPropText(), true); CldsModel cldsModel2 = verifyClosedLoopModelLoadedInDb(csar, "tca_2.yaml"); - JSONAssert.assertEquals( - IOUtils - .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json")), - cldsModel2.getPropText(), true); + JSONAssert.assertEquals(IOUtils.toString( + ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json"), + StandardCharsets.UTF_8), cldsModel2.getPropText(), true); CldsModel cldsModel3 = verifyClosedLoopModelLoadedInDb(csar, "tca_3.yaml"); JSONAssert.assertEquals( - IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")), + IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json"), + StandardCharsets.UTF_8), cldsModel3.getPropText(), true); } diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java index 544c8ca1d..e00887478 100644 --- a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java +++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java @@ -161,7 +161,7 @@ public class CsarHandlerTest { CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests"); csar.save(buildFakeSdcResut()); String policyModelYaml = csar.getPolicyModelYaml().get(); - assertTrue(policyModelYaml.contains("tosca_simple_yaml_1_1")); + assertTrue(policyModelYaml.contains("tosca_simple_yaml_1_0_0")); } @Test diff --git a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java index 6bfee4c41..d1a4bdc56 100644 --- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java @@ -23,8 +23,7 @@ package org.onap.clamp.loop; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; import java.util.ArrayList; @@ -33,6 +32,8 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import javax.transaction.Transactional; + import org.apache.commons.lang3.RandomStringUtils; import org.json.JSONException; import org.junit.Test; @@ -62,7 +63,7 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new") public class CsarInstallerItCase { - private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar"; + private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Simsfoimap0112.csar"; private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92"; private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec"; private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad"; @@ -70,6 +71,9 @@ public class CsarInstallerItCase { private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2"; @Autowired + private LoopsRepository loopsRepo; + + @Autowired private CsarInstaller csarInstaller; private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid, @@ -113,10 +117,6 @@ public class CsarInstallerItCase { "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID); blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact); - SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); - ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(Thread.currentThread().getContextClassLoader() - .getResource("example/sdc/service-Simsfoimap0112.csar").getFile()); - // Build fake csarhandler Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData); // Build fake csar Helper @@ -125,28 +125,54 @@ public class CsarInstallerItCase { Mockito.when(data.getValue("name")).thenReturn(generatedName); Mockito.when(notificationData.getServiceName()).thenReturn(generatedName); Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data); + + // Create helper based on real csar to test policy yaml and global properties + // set + SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); + ISdcCsarHelper sdcHelper = factory + .getSdcCsarHelper(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile()); Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper); + // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper); - Mockito.when(csarHandler.getPolicyModelYaml()).thenReturn(Optional.ofNullable("")); + Mockito.when(csarHandler.getPolicyModelYaml()) + .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml"))); return csarHandler; } + @Test + @Transactional public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException, InterruptedException, PolicyModelException { String generatedName = RandomStringUtils.randomAlphanumeric(5); CsarHandler csarHandler = buildFakeCsarHandler(generatedName); - assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler)); + assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse(); csarInstaller.installTheCsar(csarHandler); - assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler)); + assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue(); } @Test + @Transactional public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException { String generatedName = RandomStringUtils.randomAlphanumeric(5); CsarHandler csar = buildFakeCsarHandler(generatedName); csarInstaller.installTheCsar(csar); - + assertThat(loopsRepo + .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml"))) + .isTrue(); + assertThat(loopsRepo + .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml"))) + .isTrue(); + assertThat(loopsRepo + .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml"))) + .isTrue(); + // Verify now that policy and json representation, global properties are well + // set + Loop loop = loopsRepo + .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get(); + + assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull(); + assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull(); } } |