--- /dev/null
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * Modifications Copyright © 2018 IBM.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.assignment.processor;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;\r
+import org.onap.ccsdk.config.model.ConfigModelConstant;\r
+import org.onap.ccsdk.config.model.ConfigModelException;\r
+import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
+import org.onap.ccsdk.config.model.service.ComponentNode;\r
+import org.onap.ccsdk.config.model.utils.ResourceAssignmentUtils;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
+\r
+public class InputResourceProcessor implements ComponentNode {\r
+\r
+ public InputResourceProcessor(ConfigResourceService configResourceService) {}\r
+\r
+ @Override\r
+ public Boolean preCondition(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+ throws SvcLogicException {\r
+ return Boolean.TRUE;\r
+ }\r
+\r
+ @Override\r
+ public void preProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+ throws SvcLogicException {\r
+ // Auto-generated method stub\r
+ }\r
+\r
+ @Override\r
+ public void process(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {\r
+ // Auto-generated method stub\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public void process(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+ throws SvcLogicException {\r
+ try {\r
+ List<ResourceAssignment> batchResourceAssignment =\r
+ (List<ResourceAssignment>) componentContext.get(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS);\r
+ if (batchResourceAssignment != null && !batchResourceAssignment.isEmpty()) {\r
+ for (ResourceAssignment resourceAssignment : batchResourceAssignment) {\r
+ processResourceAssignment(ctx, componentContext, resourceAssignment);\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ throw new SvcLogicException(String.format("InputResourceProcessor Exception : (%s)", e), e);\r
+ }\r
+ }\r
+\r
+ private void processResourceAssignment(SvcLogicContext ctx, Map<String, Object> componentContext,\r
+ ResourceAssignment resourceAssignment) throws ConfigModelException, SvcLogicException {\r
+ try {\r
+ if (StringUtils.isNotBlank(resourceAssignment.getName())) {\r
+ String value = ctx.getAttribute(resourceAssignment.getName());\r
+ // if value is null don't call setResourceDataValue to populate the value\r
+ if (StringUtils.isNotBlank(value)) {\r
+ ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, value);\r
+ }\r
+ }\r
+\r
+ // Check the value has populated for mandatory case\r
+ ResourceAssignmentUtils.assertTemplateKeyValueNotNull(componentContext, resourceAssignment);\r
+ } catch (Exception e) {\r
+ ResourceAssignmentUtils.setFailedResourceDataValue(componentContext, resourceAssignment, e.getMessage());\r
+ throw new SvcLogicException(\r
+ String.format("Failed in template key (%s) assignments with : (%s)", resourceAssignment, e), e);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void postProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+ throws SvcLogicException {\r
+ // Auto-generated method stub\r
+ }\r
+\r
+}\r
--- /dev/null
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * Modifications Copyright © 2018 IBM.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.assignment.processor;\r
+\r
+import static org.mockito.Matchers.any;\r
+import java.io.File;\r
+import java.nio.charset.Charset;\r
+import java.util.Arrays;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import org.apache.commons.io.FileUtils;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Mock;\r
+import org.mockito.Mockito;\r
+import org.mockito.MockitoAnnotations;\r
+import org.mockito.invocation.InvocationOnMock;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+import org.mockito.stubbing.Answer;\r
+import org.onap.ccsdk.config.assignment.service.ConfigResourceAssignmentTestUtils;\r
+import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog;\r
+import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;\r
+import org.onap.ccsdk.config.model.ConfigModelConstant;\r
+import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
+import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;\r
+import org.onap.ccsdk.config.model.utils.TransformationUtils;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+@RunWith(MockitoJUnitRunner.class)\r
+public class InputResourceProcessorTest {\r
+\r
+ private static EELFLogger logger = EELFManager.getInstance().getLogger(InputResourceProcessorTest.class);\r
+\r
+ @Mock\r
+ private ConfigResourceService configResourceService;\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Before\r
+ public void before() {\r
+ MockitoAnnotations.initMocks(this);\r
+\r
+ try {\r
+ Mockito.doAnswer(new Answer<Void>() {\r
+ @Override\r
+ public Void answer(InvocationOnMock invocationOnMock) throws Throwable {\r
+ Object[] args = invocationOnMock.getArguments();\r
+ if (args != null) {\r
+ logger.trace("Transaction info " + Arrays.asList(args));\r
+ }\r
+ return null;\r
+ }\r
+ }).when(configResourceService).save(any(TransactionLog.class));\r
+\r
+ } catch (SvcLogicException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ @Test\r
+ public void testInputSimpleProcess() throws Exception {\r
+ logger.info(" ******************************* testInputSimpleProcess ***************************");\r
+\r
+ String recipeName = "sample-recipe";\r
+\r
+ String resourceassignmentContent = FileUtils.readFileToString(\r
+ new File("src/test/resources/mapping/input/resource-assignments-simple.json"),\r
+ Charset.defaultCharset());\r
+ List<ResourceAssignment> batchResourceAssignment =\r
+ TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);\r
+\r
+ String dictionaryContent = FileUtils.readFileToString(\r
+ new File("src/test/resources/mapping/default/default-simple.json"), Charset.defaultCharset());\r
+ Map<String, ResourceDefinition> dictionaries =\r
+ ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);\r
+\r
+ InputResourceProcessor inputResourceProcessor = new InputResourceProcessor(configResourceService);\r
+ Map<String, Object> componentContext = new HashMap<>();\r
+ componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);\r
+ componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);\r
+ componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");\r
+ componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);\r
+ componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");\r
+\r
+ Map<String, String> inParams = new HashMap<>();\r
+ SvcLogicContext ctx = new SvcLogicContext();\r
+ ctx.setAttribute("country", "US");\r
+ ctx.setAttribute("port", "830");\r
+ ctx.setAttribute("voip-enabled", "true");\r
+\r
+ inputResourceProcessor.process(inParams, ctx, componentContext);\r
+ logger.trace(" componentContext " + componentContext);\r
+\r
+ Assert.assertEquals("Failed to populate default recipe country value ", "US",\r
+ componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.country"));\r
+ Assert.assertEquals("Failed to populate default dictionary country value ", "US",\r
+ componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.country"));\r
+\r
+ Assert.assertEquals("Failed to populate default recipe port value ", 830,\r
+ componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.port"));\r
+ Assert.assertEquals("Failed to populate default dictionary port value ", 830,\r
+ componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.port"));\r
+\r
+ Assert.assertEquals("Failed to populate default recipe voip-enabled value ", true,\r
+ componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.voip-enabled"));\r
+ Assert.assertEquals("Failed to populate default dictionary voip-enabled value ", true,\r
+ componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.voip-enabled"));\r
+\r
+ }\r
+\r
+ @Test\r
+ public void testInputComplexProcess() throws Exception {\r
+ logger.info(" ******************************* testInputComplexProcess ***************************");\r
+\r
+ String recipeName = "sample-recipe";\r
+\r
+ String resourceassignmentContent = FileUtils.readFileToString(\r
+ new File("src/test/resources/mapping/input/resource-assignments-complex.json"),\r
+ Charset.defaultCharset());\r
+ List<ResourceAssignment> batchResourceAssignment =\r
+ TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);\r
+\r
+ String dictionaryContent = FileUtils.readFileToString(\r
+ new File("src/test/resources/mapping/input/input-complex.json"), Charset.defaultCharset());\r
+ Map<String, ResourceDefinition> dictionaries =\r
+ ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);\r
+\r
+ InputResourceProcessor inputResourceProcessor = new InputResourceProcessor(configResourceService);\r
+ Map<String, Object> componentContext = new HashMap<>();\r
+ componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);\r
+ componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);\r
+ componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");\r
+ componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);\r
+ componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");\r
+\r
+ Map<String, String> inParams = new HashMap<>();\r
+ SvcLogicContext ctx = new SvcLogicContext();\r
+ String datatypeContent = FileUtils.readFileToString(\r
+ new File("src/test/resources/mapping/input/dt-location.json"), Charset.defaultCharset());\r
+ ctx.setAttribute("data_types.dt-location", datatypeContent);\r
+\r
+ String samplelocation = FileUtils.readFileToString(\r
+ new File("src/test/resources/mapping/input/sample-location.json"), Charset.defaultCharset());\r
+ ctx.setAttribute("location", samplelocation);\r
+\r
+ inputResourceProcessor.process(inParams, ctx, componentContext);\r
+\r
+ logger.trace(" componentContext " + componentContext);\r
+ logger.trace(" Resource Mapping " + TransformationUtils.getJson(batchResourceAssignment, true));\r
+\r
+ Assert.assertNotNull("Failed to populate input recipe location value ",\r
+ componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.location"));\r
+ Assert.assertNotNull("Failed to populate input dictionary location value ",\r
+ componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.location"));\r
+\r
+ }\r
+\r
+}\r