Commit 1f3f3b90 authored by Romain Bioteau's avatar Romain Bioteau
Browse files

BS-9737

Add coverage
parent 0752f07f
......@@ -5,7 +5,8 @@ Bundle-SymbolicName: org.bonitasoft.studio.tests-utils
Bundle-Version: 6.4.0.qualifier
Bundle-Vendor: BonitaSoft S.A.
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: org.bonitasoft.studio.model.expression.assertions,
Export-Package: org.bonitasoft.studio.model.connectorconfiguration.builders,
org.bonitasoft.studio.model.expression.assertions,
org.bonitasoft.studio.model.expression.builders,
org.bonitasoft.studio.model.form.builders,
org.bonitasoft.studio.model.parameter.builders,
......
......@@ -35,8 +35,10 @@ public class ConnectorConfigurationBuilder {
instance = ConnectorConfigurationFactory.eINSTANCE.createConnectorConfiguration();
}
public ConnectorConfigurationBuilder havingParameter(final ConnectorParameterBuilder parameter) {
instance.getParameters().add(parameter.build());
public ConnectorConfigurationBuilder havingParameters(final ConnectorParameterBuilder... parameters) {
for (final ConnectorParameterBuilder parameter : parameters) {
instance.getParameters().add(parameter.build());
}
return this;
}
......
......@@ -17,9 +17,18 @@
package org.bonitasoft.studio.validation.constraints.connector;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import org.bonitasoft.studio.model.connectorconfiguration.builders.ConnectorConfigurationBuilder;
import org.bonitasoft.studio.model.connectorconfiguration.builders.ConnectorParameterBuilder;
import org.bonitasoft.studio.model.expression.builders.ExpressionBuilder;
import org.bonitasoft.studio.model.process.Connector;
import org.bonitasoft.studio.model.process.builders.ConnectorBuilder;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.validation.IValidationContext;
import org.junit.After;
import org.junit.Before;
......@@ -55,6 +64,11 @@ public class CMISConnectorDefinitionConstraintTest {
public void tearDown() throws Exception {
}
@Test
public void should_getConstraintId_return_ID_constant() throws Exception {
assertThat(cmisConnectorDefinitionConstraint.getConstraintId()).isEqualTo(CMISConnectorDefinitionConstraint.ID);
}
@Test
public void should_performLiveValidation_return_null() throws Exception {
assertThat(cmisConnectorDefinitionConstraint.performLiveValidation(context)).isNull();
......@@ -62,25 +76,141 @@ public class CMISConnectorDefinitionConstraintTest {
}
@Test
public void should_performBatchValidation() throws Exception {
public void should_performBatchValidation_return_a_failure_status_for_a_cmis_connector_without_binding_type() throws Exception {
when(context.getTarget()).thenReturn(anInvalidCMISConfigWithoutBindingType());
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context).createFailureStatus(anyString());
}
@Test
public void should_performBatchValidation_return_a_failure_status_for_a_cmis_connector_with_invalid_binding_type() throws Exception {
when(context.getTarget()).thenReturn(anInvalidCMISConfigWithInvalidBindingType());
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context).createFailureStatus(anyString());
}
@Test
public void should_performBatchValidation_return_a_succes_status_for_non_cmis_connector() throws Exception {
when(context.getTarget()).thenReturn(aNonCMISConnectorConfig());
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context).createSuccessStatus();
}
@Test
public void should_performBatchValidation_return_a_failure_status_for_a_cmis_connector_with_invalid_atom_pub_config() throws Exception {
when(context.getTarget()).thenReturn(aCMISConfigWithUrl("", ""));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
when(context.getTarget()).thenReturn(aCMISConfigWithUrl(null, ""));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
when(context.getTarget()).thenReturn(aCMISConfigWithUrl("atompub", ""));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
when(context.getTarget()).thenReturn(aCMISConfigWithUrl("atompub", null));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
when(context.getTarget()).thenReturn(anAtompubConfigWithoutURL("atompub"));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context, times(5)).createFailureStatus(anyString());
}
@Test
public void should_performBatchValidation_return_a_failure_status_for_a_cmis_connector_with_invalid_webservice_config() throws Exception {
when(context.getTarget()).thenReturn(aCMISConfigWithWSUrl("webservices", "", ""));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context).createFailureStatus(anyString());
}
@Test
public void should_performBatchValidation_return_a_succes_status_for_a_valid_websercie_cmis_connector_config() throws Exception {
when(context.getTarget()).thenReturn(aCMISConfigWithWSUrl("webservices", "http://some/url", null));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
when(context.getTarget()).thenReturn(aCMISConfigWithWSUrl("webservices", null, "http://some/url"));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context, times(2)).createSuccessStatus();
}
@Test
public void should_performBatchValidation_return_a_succes_status_for_a_valid_atompub_cmis_connector_config() throws Exception {
when(context.getTarget()).thenReturn(aCMISConfigWithUrl("atompub", "http://some/url"));
cmisConnectorDefinitionConstraint.performBatchValidation(context);
verify(context).createSuccessStatus();
}
@Test
public void should_isAnExpressionWithContent_return_false() throws Exception {
assertThat(cmisConnectorDefinitionConstraint.isAnExpressionWithContent(ExpressionBuilder.create().build())).isFalse();
assertThat(cmisConnectorDefinitionConstraint.isAnExpressionWithContent(ExpressionBuilder.create().withContent("").build())).isFalse();
assertThat(cmisConnectorDefinitionConstraint.isAnExpressionWithContent(null)).isFalse();
}
@Test
public void should_isAnExpressionWithContent_return_true() throws Exception {
assertThat(cmisConnectorDefinitionConstraint.isAnExpressionWithContent(ExpressionBuilder.create().withContent("content").build())).isTrue();
}
private Connector aNonCMISConnectorConfig() {
return ConnectorBuilder.createConnectorBuilder().withDefinitionId("whatever").withDefinitionVersion("1.0").build();
}
private Connector anInvalidCMISConfigWithoutBindingType() {
return ConnectorBuilder.createConnectorBuilder().
withDefinitionId("cmis-createfolder").
withDefinitionVersion("1.0").
havingConfiguration(ConnectorConfigurationBuilder.create().
havingParameters(aConnectorParameter("url", "http://some/url"))).build();
}
private Connector anInvalidCMISConfigWithInvalidBindingType() {
return ConnectorBuilder.createConnectorBuilder().
withDefinitionId("cmis-createfolder").
withDefinitionVersion("1.0").
havingConfiguration(ConnectorConfigurationBuilder.create().
havingParameters(aConnectorParameter("binding_type", "anotherType"),
aConnectorParameter("url", "http://some/url"))).build();
}
private Connector aValidAtompubConfig() {
// final ConnectorBuilder
return null;
private ConnectorParameterBuilder aConnectorParameter(final String key, final String expressionContent) {
final ConnectorParameterBuilder builder = ConnectorParameterBuilder.create().
withKey(key);
if(expressionContent != null){
builder.havingExpression(ExpressionBuilder.create().withContent(expressionContent));
}
return builder;
}
private Connector aValidWerbserviceConfig() {
return null;
private Connector aCMISConfigWithUrl(final String bindingContent, final String urlContent) {
return ConnectorBuilder.createConnectorBuilder().
withDefinitionId("cmis-createfolder").
withDefinitionVersion("1.0").
havingConfiguration(ConnectorConfigurationBuilder.create().
havingParameters(aConnectorParameter("binding_type", bindingContent),
aConnectorParameter("url", urlContent))).build();
}
private Connector anInvalidAtompubConfig() {
return null;
private Connector anAtompubConfigWithoutURL(final String bindingContent) {
return ConnectorBuilder.createConnectorBuilder().
withDefinitionId("cmis-createfolder").
withDefinitionVersion("1.0").
havingConfiguration(ConnectorConfigurationBuilder.create().
havingParameters(aConnectorParameter("binding_type", bindingContent))).build();
}
private Connector anInvalidWebserviceConfig() {
return null;
private EObject aCMISConfigWithWSUrl(final String bindingContent, final String wsServiceUrl, final String wsServiceEndpointURL) {
return ConnectorBuilder.createConnectorBuilder().
withDefinitionId("cmis-createfolder").
withDefinitionVersion("1.0").
havingConfiguration(ConnectorConfigurationBuilder.create().
havingParameters(aConnectorParameter("binding_type", bindingContent),
aConnectorParameter("wsServiceUrl", wsServiceUrl),
aConnectorParameter("wsServiceEndpointUrl", wsServiceEndpointURL))).build();
}
}
......@@ -7,7 +7,6 @@ import java.util.Set;
import org.bonitasoft.studio.model.connectorconfiguration.ConnectorConfiguration;
import org.bonitasoft.studio.model.connectorconfiguration.ConnectorParameter;
import org.bonitasoft.studio.model.expression.AbstractExpression;
import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.process.Connector;
import org.bonitasoft.studio.model.process.Element;
......@@ -44,14 +43,16 @@ public class CMISConnectorDefinitionConstraint extends AbstractLiveValidationMar
final ConnectorConfiguration configuration = connector.getConfiguration();
if (isCMISConnectorDefinition(connector)) {
final ConnectorParameter bindingTypeParam = getParameter("binding_type", configuration);
if (bindingTypeParam != null) {
final String type = getBindingType(bindingTypeParam);
if (ATOM_PUB.equals(type)) {
return validateAtomPubConfiguration(configuration, context);
} else if (WEB_SERVICES.equals(type)) {
return validateWebserviceConfiguration(configuration, context);
}
if (bindingTypeParam == null) {
return context.createFailureStatus("binding_type parameter not set");
}
final String type = getBindingType(bindingTypeParam);
if (ATOM_PUB.equals(type)) {
return validateAtomPubConfiguration(configuration, context);
} else if (WEB_SERVICES.equals(type)) {
return validateWebserviceConfiguration(configuration, context);
}
return context.createFailureStatus("Unknown binding_type parameter");
}
return context.createSuccessStatus();
......@@ -59,10 +60,10 @@ public class CMISConnectorDefinitionConstraint extends AbstractLiveValidationMar
private IStatus validateWebserviceConfiguration(final ConnectorConfiguration configuration, final IValidationContext context) {
final List<ConnectorParameter> serviceUrlParams = getAllParametersContaining("ServiceUrl", configuration);
final List<ConnectorParameter> serviceEdpointUrlParams = getAllParametersContaining("ServiceEndpointUrl", configuration);
if (serviceUrlParams.isEmpty() && serviceEdpointUrlParams.isEmpty()) {
if (serviceUrlParams.isEmpty()
&& serviceEdpointUrlParams.isEmpty()) {
return context.createFailureStatus(Messages.bind(Messages.cmisConnectorWebserviceConfigMissingUrl, ((Element) context.getTarget()).getName()));
}
......@@ -71,20 +72,24 @@ public class CMISConnectorDefinitionConstraint extends AbstractLiveValidationMar
private IStatus validateAtomPubConfiguration(final ConnectorConfiguration configuration, final IValidationContext context) {
final ConnectorParameter wsObjectServiceUrlParam = getParameter("url", configuration);
if (wsObjectServiceUrlParam == null
|| wsObjectServiceUrlParam.getExpression() == null
|| ((Expression) wsObjectServiceUrlParam.getExpression()).getContent() == null
|| ((Expression) wsObjectServiceUrlParam.getExpression()).getContent().isEmpty()) {
final ConnectorParameter urlParam = getParameter("url", configuration);
if (urlParam == null
|| !isAnExpressionWithContent((Expression) urlParam.getExpression())) {
return context.createFailureStatus(Messages.bind(Messages.cmisConnectorAtomPubConfigMissingUrl, ((Element) context.getTarget()).getName()));
}
return context.createSuccessStatus();
}
protected boolean isAnExpressionWithContent(final Expression expression) {
return expression != null
&& expression.getContent() != null
&& !expression.getContent().isEmpty();
}
protected String getBindingType(final ConnectorParameter bindingTypeParam) {
final AbstractExpression expression = bindingTypeParam.getExpression();
if (expression instanceof Expression) {
return ((Expression) expression).getContent();
final Expression expression = (Expression) bindingTypeParam.getExpression();
if (expression != null) {
return expression.getContent();
}
return null;
}
......@@ -101,7 +106,8 @@ public class CMISConnectorDefinitionConstraint extends AbstractLiveValidationMar
private List<ConnectorParameter> getAllParametersContaining(final String containing, final ConnectorConfiguration configuration) {
final List<ConnectorParameter> result = new ArrayList<ConnectorParameter>();
for (final ConnectorParameter param : configuration.getParameters()) {
if (param.getKey().contains(containing)) {
if (param.getKey().contains(containing)
&& isAnExpressionWithContent((Expression) param.getExpression())) {
result.add(param);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment