changeset 638:66d541da3dc3

ST-88
author Devel 2
date Thu, 02 Nov 2017 15:13:07 +0100
parents 1d9bc9f80a6a
children ed03df7b7bef
files stress-tester/src/main/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformer.java stress-tester/src/test/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformerTest.java
diffstat 2 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformer.java	Thu Nov 02 14:44:31 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformer.java	Thu Nov 02 15:13:07 2017 +0100
@@ -7,6 +7,8 @@
 import com.passus.config.CValueNode;
 import com.passus.config.ConfigurationImpl;
 import com.passus.config.NodeType;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.config.schema.NodeTransformer;
 import com.passus.config.validation.Errors;
 import static com.passus.st.validation.NodeValidationUtils.validateType;
@@ -18,9 +20,9 @@
  * @author Mirosław Hawrot
  */
 public class MultiCredentialsProviderTransformer implements NodeTransformer<CValueNode> {
-    
+
     private final CredentialsProviderFactory factory = CredentialsProviderFactory.getInstance();
-    
+
     @Override
     public CValueNode transform(CNode node, Errors errors) {
         if (validateType(node, NodeType.MAP, errors)) {
@@ -33,21 +35,34 @@
                     errors.reject("Unknown provider \"%s\".", providerName);
                     return null;
                 }
-                
+
+                CCompositeNode configNode = (CCompositeNode) child.getNode();
                 CredentialsProvider provider = factory.getInstanceByName(providerName);
-                provider.configure(new ConfigurationImpl((CCompositeNode) child.getNode()));
+                try {
+                    errors.pushNestedPath(providerName);
+                    NodeDefinitionCreator nodeDefCreator = NodeDefinitionCreator.getByObject(provider);
+                    NodeDefinition nodeDef = nodeDefCreator.create();
+                    nodeDef.validate(configNode, errors);
+                    if (errors.getErrorCount() != 0) {
+                        break;
+                    }
+                } finally {
+                    errors.popNestedPath();
+                }
+
+                provider.configure(new ConfigurationImpl(configNode));
                 providers.add(provider);
             }
-            
+
             return new CValueNode(new MultiCredentialsProvider(providers));
         }
-        
+
         return null;
     }
-    
+
     @Override
     public CValueNode reverseTransform(CNode node, Errors errors) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
-    
+
 }
--- a/stress-tester/src/test/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformerTest.java	Thu Nov 02 14:44:31 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/credentials/MultiCredentialsProviderTransformerTest.java	Thu Nov 02 15:13:07 2017 +0100
@@ -73,6 +73,23 @@
         assertEquals("pass,word", actualList.get(4).getPassword());
     }
 
+    @Test
+    public void testTransformCsvUsernamePassword_InvalidConfig() throws Exception {
+        File file = ResourceUtils.getFile("csv/comma.csv");
+        String content
+                = "provider:\n"
+                + "    CsvUsernamePassword:\n"
+                + "        fileName2: '%s'\n";
+        content = String.format(content, file.getAbsolutePath());
+
+        try {
+            List<CredentialsProvider> providers = readAndTransform(content).getProviders();
+            fail("Exception required.");
+        } catch (Exception e) {
+            assertEquals("Unknown parameter \"fileName2\".", e.getMessage());
+        }
+    }
+
     private static MultiCredentialsProvider readAndTransform(String content) throws IOException, NodeException, ConversionException {
         Configuration cfg = YamlConfigurationReader.readFromString(content);
         CCompositeNode rootNode = cfg.getRootNode();