/**
* DataCleaner (community edition)
* Copyright (C) 2014 Neopost - Customer Information Management
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.datacleaner.restclient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ComponentRESTClientTest {
private static final Logger logger = LoggerFactory.getLogger(ComponentRESTClientTest.class);
private static final String HOST = "http://localhost:1234";
private static final String TENANT = "admin";
private static final String USERNAME = ComponentRESTClientTest.TENANT;
private static final String PASSWORD = "admin";
private static final String COMPONENT_NAME = "Concatenator";
private static final String INSTANCE_ID = "0166fb12-d403-408f-b2bc-61eb898ee338";
private ComponentRESTClient componentRESTClient =
new ComponentRESTClient(HOST, USERNAME, PASSWORD, "demo", "version");
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testGetAllComponents() throws Exception {
try {
final ComponentList componentList = componentRESTClient.getAllComponents(false);
logger.info(componentList.toString());
Assert.assertTrue(componentList != null);
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("Connection refused"));
}
}
@Test
public void testGetComponentInfo() throws Exception {
try {
final ComponentList.ComponentInfo componentInfo =
componentRESTClient.getComponentInfo(COMPONENT_NAME, false);
logger.info(componentInfo.toString());
Assert.assertTrue(componentInfo != null);
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("Connection refused"));
}
}
@Test
public void testProcessStateless() throws Exception {
try {
final ProcessStatelessInput processStatelessInput = new ProcessStatelessInput();
processStatelessInput.configuration = getConfiguration();
processStatelessInput.data = getInputData();
final ProcessStatelessOutput processStatelessOutput =
componentRESTClient.processStateless(COMPONENT_NAME, processStatelessInput);
logger.info(processStatelessOutput.toString());
Assert.assertTrue(processStatelessOutput != null);
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("Connection refused"));
}
}
@Test
public void testCreateComponent() throws Exception {
try {
final String timeout = "60000";
final CreateInput createInput = getCreateInput();
final String response = componentRESTClient.createComponent(COMPONENT_NAME, timeout, createInput);
logger.info(response);
Assert.assertFalse(response.isEmpty());
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("Connection refused"));
}
}
@Test
public void testProcessComponent() throws Exception {
try {
final ProcessInput processInput = getProcessInput();
final ProcessOutput processOutput = componentRESTClient.processComponent(INSTANCE_ID, processInput);
logger.info(processOutput.toString());
Assert.assertTrue(processOutput != null);
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("Connection refused"));
}
}
@Test
public void testGetFinalResult() throws Exception {
try {
final ProcessResult processResult = componentRESTClient.getFinalResult(INSTANCE_ID);
if (processResult != null) {
logger.info(processResult.toString());
}
} catch (final Exception e) {
Assert.assertTrue(e.getMessage().contains("Connection refused"));
}
}
@Test
public void testDeleteComponent() throws Exception {
try {
componentRESTClient.deleteComponent(INSTANCE_ID);
} catch (final Exception e) {
Assert.assertTrue(
e.getMessage().contains("Connection refused") || e.getMessage().contains("HTTP error code: 404"));
}
}
private CreateInput getCreateInput() {
final CreateInput createInput = new CreateInput();
createInput.configuration = getConfiguration();
return createInput;
}
private ProcessInput getProcessInput() {
final ProcessInput processInput = new ProcessInput();
processInput.data = getInputData();
return processInput;
}
private ComponentConfiguration getConfiguration() {
final ComponentConfiguration componentConfiguration = new ComponentConfiguration();
final JsonNode column1 = getJsonNode("c1");
final JsonNode column2 = getJsonNode("c2");
componentConfiguration.getColumns().add(column1);
componentConfiguration.getColumns().add(column2);
final Map<String, JsonNode> properties = new HashMap<>();
properties.put("Separator", getJsonNode("x"));
componentConfiguration.getProperties().putAll(properties);
return componentConfiguration;
}
private JsonNode getInputData() {
try {
final List<String> inputData = new ArrayList<>();
inputData.add("c1-value");
inputData.add("c2-value");
final List<Object> list = new ArrayList<>();
list.add(inputData);
final String serialization = objectMapper.writeValueAsString(list);
return getJsonNode(serialization);
} catch (final JsonProcessingException e) {
logger.error(e.getMessage());
return null;
}
}
private JsonNode getJsonNode(final String value) {
try {
return objectMapper.convertValue(value, JsonNode.class);
} catch (final IllegalArgumentException e) {
logger.error(e.getMessage());
return null;
}
}
}