From: Tian Lee Date: Wed, 3 Apr 2019 10:34:37 +0000 (+0000) Subject: Merge "Move REQUIRE_CLIENT_AUTH code to start script" X-Git-Tag: 1.4.1~9 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=6b52f93a1965dfadd55410a4231446c9fb374633;hp=61607b8e66f19aba46d0c0f7cec3a9fe2c6e1e08;p=aai%2Fbabel.git Merge "Move REQUIRE_CLIENT_AUTH code to start script" --- diff --git a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java index 5f3d15b..d5ba793 100644 --- a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java @@ -187,11 +187,14 @@ public class VnfVendorImageExtractor { * the path to the CSAR file * @return a List of Vendor Image Configurations * @throws SdcToscaParserException + * if the SDC TOSCA parser determines that the CSAR is invalid * @throws ToscaToCatalogException + * if there are no software versions defined for an image * @throws InvalidNumberOfNodesException + * if multiple VNF configuration nodes are found in the CSAR */ private List createVendorImageConfigurations(String csarFilepath) - throws SdcToscaParserException, InvalidNumberOfNodesException { + throws SdcToscaParserException, ToscaToCatalogException, InvalidNumberOfNodesException { ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath); List serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper) @@ -215,7 +218,11 @@ public class VnfVendorImageExtractor { + vnfConfigs.size() + " nodes were found in the CSAR."); } - return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode); + try { + return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode); + } catch (IllegalArgumentException e) { + throw new ToscaToCatalogException(e.getMessage()); + } } return Collections.emptyList(); @@ -263,8 +270,11 @@ public class VnfVendorImageExtractor { * the node template for the VF * * @return a stream of VendorImageConfiguration objects + * @throws IllegalArgumentException + * if the VF has no child node templates which contain images (complex properties) that have software + * version strings */ - private Stream buildVendorImageConfigurations( + Stream buildVendorImageConfigurations( Collection>> flavorMaps, NodeTemplate vfNodeTemplate) { String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor"); applicationLogger.debug("Resource Vendor " + resourceVendor); @@ -273,6 +283,10 @@ public class VnfVendorImageExtractor { extractSoftwareVersions(vfNodeTemplate.getSubMappingToscaTemplate().getNodeTemplates()); applicationLogger.debug("Software Versions: " + softwareVersions); + if (softwareVersions.isEmpty()) { + throw new IllegalArgumentException("No software versions could be found for this CSAR file"); + } + return flavorMaps.stream() // .map(value -> value.entrySet().stream() // .filter(entry -> VENDOR_INFO.equals(entry.getKey())) // diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java index 6fbb1f1..d285aca 100644 --- a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java @@ -32,7 +32,7 @@ public class SdcToscaHelper { /** * Create the test SubstitutionMappings. - * + * * @return the new Substitution Mappings */ public SubstitutionMappings buildMappings() { @@ -81,28 +81,32 @@ public class SdcToscaHelper { /** * Create a new NodeTemplate and add it to the list (for populating the Substitution Mappings). + * + * @return the new NodeTemplate */ - public void addNodeTemplate() { + public NodeTemplate addNodeTemplate() { String name = "node name"; String type = "tosca.nodes.custom"; - LinkedHashMap nodeTemplate = new LinkedHashMap<>(); - nodeTemplate.put("type", type); - nodeTemplate.put("properties", null); + LinkedHashMap ntMap = new LinkedHashMap<>(); + ntMap.put("type", type); + ntMap.put("properties", null); - LinkedHashMap ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate); + LinkedHashMap ntnodeTemplates = buildCustomTypeDefinitions(name, ntMap); ntnodeTemplates.put("derived_from", null); ntnodeTemplates.put("properties", getImagesDefProps()); LinkedHashMap typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps()); LinkedHashMap customDefs = buildCustomTypeDefinitions(type, typeInfo); - smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null)); + NodeTemplate nodeTemplate = new NodeTemplate(name, ntnodeTemplates, customDefs, null, null); + smnodetemplates.add(nodeTemplate); + return nodeTemplate; } /** * Simulate the creation of a NodeTemplate by the SDC TOSCA parser. Populate the properties of the NodeTemplate with * the supplied images. - * + * * @param images * the value of the images property */ diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/TestVnfVendorImageExtractor.java similarity index 88% rename from src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java rename to src/test/java/org/onap/aai/babel/csar/vnfcatalog/TestVnfVendorImageExtractor.java index 7a076c5..d76551a 100644 --- a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/TestVnfVendorImageExtractor.java @@ -28,6 +28,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; +import com.google.common.collect.ImmutableMap; import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; @@ -37,11 +38,13 @@ import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType; import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; /** * Tests {@link VnfVendorImageExtractor}. */ -public class VnfVendorImageExtractorTest { +public class TestVnfVendorImageExtractor { @Test(expected = NullPointerException.class) public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException { @@ -89,6 +92,18 @@ public class VnfVendorImageExtractorTest { is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json")))); } + /** + * Test that an Exception is created when there are no software versions defined for a VF. + */ + @Test(expected = IllegalArgumentException.class) + public void testBuildVendorImageConfigurations() { + SdcToscaHelper helper = new SdcToscaHelper(); + NodeTemplate vf = helper.addNodeTemplate(); + vf.setMetaData(new Metadata(ImmutableMap.of("resourceVendor", "vendor"))); + vf.setSubMappingToscaTemplate(helper.buildMappings()); + new VnfVendorImageExtractor().buildVendorImageConfigurations(null, vf); + } + @Test public void testSoftwareVersions() throws ToscaToCatalogException { VnfVendorImageExtractor extractor = new VnfVendorImageExtractor(); diff --git a/src/test/java/org/onap/aai/babel/logging/TestApplicationLogger.java b/src/test/java/org/onap/aai/babel/logging/TestApplicationLogger.java index 02df4e8..ea30c01 100644 --- a/src/test/java/org/onap/aai/babel/logging/TestApplicationLogger.java +++ b/src/test/java/org/onap/aai/babel/logging/TestApplicationLogger.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. + * Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,13 +26,17 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; +import com.att.eelf.configuration.EELFLogger.Level; +import com.att.eelf.configuration.EELFManager; import java.io.IOException; import java.util.Arrays; +import javax.servlet.ServletRequest; import javax.ws.rs.core.HttpHeaders; import org.apache.commons.lang3.time.StopWatch; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; +import org.onap.aai.babel.logging.LogHelper.MdcParameter; import org.onap.aai.babel.logging.LogHelper.TriConsumer; import org.onap.aai.cl.api.LogFields; import org.onap.aai.cl.api.Logger; @@ -61,6 +65,7 @@ public class TestApplicationLogger { @Test public void logAllMessages() throws IOException { Logger logger = LogHelper.INSTANCE; + LogHelper.INSTANCE.clearContext(); LogReader errorReader = new LogReader(LogHelper.getLogDirectory(), "error"); LogReader debugReader = new LogReader(LogHelper.getLogDirectory(), "debug"); String[] args = {"1", "2", "3", "4"}; @@ -81,9 +86,6 @@ public class TestApplicationLogger { logger.debug(msg, args); validateLoggedMessage(msg, debugReader, "DEBUG"); - - // The trace level is not enabled - logger.trace(msg, args); } } @@ -102,6 +104,28 @@ public class TestApplicationLogger { assertThat(str, is(notNullValue())); } + @Test + public void logTraceMessage() throws IOException { + LogReader reader = new LogReader(LogHelper.getLogDirectory(), "debug"); + EELFManager.getInstance().getDebugLogger().setLevel(Level.TRACE); + LogHelper.INSTANCE.trace(ApplicationMsgs.LOAD_PROPERTIES, "a message"); + String str = reader.getNewLines(); + assertThat(str, is(notNullValue())); + EELFManager.getInstance().getAuditLogger().setLevel(Level.INFO); + LogHelper.INSTANCE.trace(ApplicationMsgs.LOAD_PROPERTIES, "message not written"); + } + + /** + * Call logAuditError() for code coverage stats. + */ + @Test + public void logAuditError() { + LogHelper.INSTANCE.logAuditError(new Exception("test")); + EELFManager.getInstance().getAuditLogger().setLevel(Level.OFF); + LogHelper.INSTANCE.logAuditError(new Exception("test")); + EELFManager.getInstance().getAuditLogger().setLevel(Level.INFO); + } + /** * Check logAudit with HTTP headers. * @@ -153,6 +177,32 @@ public class TestApplicationLogger { assertThat("audit message content", str, containsString("foo")); } + /** + * Check logAudit with mocked Servlet request. + * + * @throws IOException + * if the log file cannot be read + */ + @Test + public void logAuditMessageWithServletRequest() throws IOException { + ServletRequest servletRequest = Mockito.mock(ServletRequest.class); + LogHelper logger = LogHelper.INSTANCE; + LogReader reader = new LogReader(LogHelper.getLogDirectory(), "audit"); + logger.startAudit(null, servletRequest); + logger.logAuditSuccess("foo"); + String str = reader.getNewLines(); + assertThat(str, is(notNullValue())); + assertThat("audit message log level", str, containsString("INFO")); + assertThat("audit message content", str, containsString("foo")); + } + + @Test + public void setDefaultContextValue() { + LogHelper logger = LogHelper.INSTANCE; + logger.setDefaultContextValue("key", "value"); + logger.setDefaultContextValue(MdcParameter.USER, null); + } + /** * Check logMetrics. * diff --git a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java index a98b7c2..8be81d6 100644 --- a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java +++ b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java @@ -128,7 +128,7 @@ public class ArtifactTestUtils { public String loadResourceAsString(String resourceName) throws IOException { try { - return IOUtils.toString(getResource(resourceName), Charset.defaultCharset()); + return IOUtils.toString(getResource(resourceName), Charset.defaultCharset()); } catch (NullPointerException e) { throw new IllegalArgumentException("No such resource " + resourceName); }