Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public boolean processRow() throws HopException {
}
}

if (propKey == null) {
// A properties file has no way to write a key that is not there
throw new HopTransformException(
BaseMessages.getString(PKG, "PropertyOutput.Error.NullKey", meta.getKeyField()));
}
if (propValue == null) {
// Properties take no null value: a missing value is written as an empty one (key=)
propValue = "";
}

if (!data.keySet.contains(propKey)) {
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "PropertyOutput.Log.Key", propKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ PropertyOutputMeta.CheckResult.ValueFieldOk=Value fieldname was specified
PropertyOutputMeta.keyword=properties,write,keyvalue,config,file
PropertyOutputMeta.Log.ErrorInTransform=Error in transform
PropertyOutputMeta.Log.FileNameEmty=Dynamic dilename is empty!
PropertyOutput.Error.NullKey=The key field {0} is empty in this row: a properties file has no way to write a missing key.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hop.pipeline.transforms.propertyoutput;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.io.InputStream;
import java.nio.file.Path;
import java.util.Properties;
import org.apache.hop.core.IRowSet;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.logging.ILoggingObject;
import org.apache.hop.core.row.RowMeta;
import org.apache.hop.core.row.value.ValueMetaString;
import org.apache.hop.core.vfs.HopVfs;
import org.apache.hop.junit.rules.RestoreHopEngineEnvironmentExtension;
import org.apache.hop.pipeline.transforms.mock.TransformMockHelper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

/** A properties file takes neither a null key nor a null value. */
class PropertyOutputTest {

@RegisterExtension
static RestoreHopEngineEnvironmentExtension env = new RestoreHopEngineEnvironmentExtension();

@TempDir Path folder;

private TransformMockHelper<PropertyOutputMeta, PropertyOutputData> helper;

@BeforeEach
void setUp() {
helper =
new TransformMockHelper<>(
"Properties output", PropertyOutputMeta.class, PropertyOutputData.class);
when(helper.logChannelFactory.create(any(), any(ILoggingObject.class)))
.thenReturn(helper.iLogChannel);
when(helper.pipeline.isRunning()).thenReturn(true);
}

@AfterEach
void cleanUp() {
helper.cleanUp();
}

@Test
void aMissingValueIsWrittenEmpty() throws Exception {
PropertyOutput transform = run(new Object[][] {{"a", "1"}, {"b", null}});

assertEquals(0, transform.getErrors());
Properties written = new Properties();
try (InputStream in = HopVfs.getInputStream(folder.resolve("out.properties").toString())) {
written.load(in);
}
assertEquals("1", written.getProperty("a"));
assertEquals("", written.getProperty("b"));
}

@Test
void aMissingKeyIsATransformError() throws Exception {
PropertyOutput transform = run(new Object[][] {{null, "1"}});

assertEquals(1, transform.getErrors());
}

private PropertyOutput run(Object[][] rows) throws HopException {
RowMeta inputRowMeta = new RowMeta();
inputRowMeta.addValueMeta(new ValueMetaString("key"));
inputRowMeta.addValueMeta(new ValueMetaString("value"));
IRowSet inputRowSet = helper.getMockInputRowSet(rows);
inputRowSet.setRowMeta(inputRowMeta);

PropertyOutputMeta meta = new PropertyOutputMeta();
meta.setKeyField("key");
meta.setValueField("value");
meta.getFileDetails().setFileName(folder.resolve("out").toString());
meta.getFileDetails().setExtension("properties");

PropertyOutput transform =
new PropertyOutput(
helper.transformMeta,
meta,
new PropertyOutputData(),
0,
helper.pipelineMeta,
helper.pipeline);
transform.addRowSetToInputRowSets(inputRowSet);
transform.setInputRowMeta(inputRowMeta);
transform.init();

boolean more = true;
for (int call = 0; more && call <= rows.length; call++) {
more = transform.processRow();
}
assertFalse(more, "the transform did not end");
return transform;
}
}
Loading