Skip to content

Commit

Permalink
extend SDK by builder for nested properties (#2262)
Browse files Browse the repository at this point in the history
* extend SDK by builder for nested properties

* refactor: remove `clear()` method
  • Loading branch information
bossenti authored Dec 4, 2023
1 parent 2339b2c commit 5e27cc3
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.streampipes.sdk.builder;

import org.apache.streampipes.model.schema.EventProperty;
import org.apache.streampipes.model.schema.EventPropertyNested;

import java.util.ArrayList;
import java.util.List;

/**
* Builder class for creating instances of {@link EventPropertyNested}.
*/
public class NestedPropertyBuilder {

/**
* The {@link EventPropertyNested} being constructed by this builder.
*/
private final EventPropertyNested eventPropertyNested;

/**
* List of {@link EventProperty} instances contained within the {@link EventPropertyNested}.
*/
private final List<EventProperty> containedEventProperties;

private NestedPropertyBuilder(String propertyName) {
if (propertyName == null || propertyName.isEmpty()) {
throw new IllegalArgumentException("Property name cannot be null or empty");
}
this.eventPropertyNested = new EventPropertyNested(propertyName);
this.containedEventProperties = new ArrayList<>();
}

/**
* Static factory method to create a new instance of the builder.
*
* @param propertyName The name of the property.
* @return A new instance of {@code NestedPropertyBuilder}.
*/
public static NestedPropertyBuilder create(String propertyName) {
return new NestedPropertyBuilder(propertyName);
}

/**
* Adds a single {@link EventProperty} to the list of nested properties.
*
* @param eventProperty The {@link EventProperty} to add.
* @return This builder instance for method chaining.
*/
public NestedPropertyBuilder withEventProperty(EventProperty eventProperty) {
this.containedEventProperties.add(eventProperty);
return this;
}

/**
* Adds multiple {@link EventProperty} instances to the list of nested properties.
*
* @param eventProperties The array of {@link EventProperty} instances to add.
* @return This builder instance for method chaining.
*/
public NestedPropertyBuilder withEventProperties(EventProperty... eventProperties) {
this.containedEventProperties.addAll(List.of(eventProperties));
return this;
}

/**
* Builds and returns the final {@link EventPropertyNested} instance.
* Note: This method creates a new instance to ensure immutability.
*
* @return The constructed {@link EventPropertyNested}.
*/
public EventPropertyNested build() {
// Create a new instance to make the class immutable
EventPropertyNested built = new EventPropertyNested(this.eventPropertyNested.getRuntimeName());
built.setEventProperties(containedEventProperties);
return built;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.streampipes.sdk.builder;

import org.apache.streampipes.model.schema.EventPropertyNested;
import org.apache.streampipes.sdk.utils.Datatypes;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class NestedPropertyBuilderTest {

@Test
public void buildNestedProperty() {
EventPropertyNested result = NestedPropertyBuilder.create("TestProperty")
.withEventProperty(PrimitivePropertyBuilder.create(
Datatypes.String,
"SubProperty1"
)
.build())
.withEventProperty(PrimitivePropertyBuilder.create(
Datatypes.String,
"SubProperty2"
)
.build())
.build();

assertNotNull(result);
assertEquals("TestProperty", result.getRuntimeName());
assertEquals(
2,
result.getEventProperties()
.size()
);
}

@Test
public void buildNestedPropertyWithoutProperties() {
EventPropertyNested result = NestedPropertyBuilder.create("TestProperty")
.build();

assertNotNull(result);
assertEquals("TestProperty", result.getRuntimeName());
assertTrue(result.getEventProperties()
.isEmpty());
}

@Test
public void buildNestedPropertyWithArray() {
EventPropertyNested result = NestedPropertyBuilder.create("TestProperty")
.withEventProperties(
PrimitivePropertyBuilder.create(
Datatypes.String,
"SubProperty1"
)
.build(),
PrimitivePropertyBuilder.create(
Datatypes.String,
"SubProperty2"
)
.build()
)
.build();

assertNotNull(result);
assertEquals("TestProperty", result.getRuntimeName());
assertEquals(
2,
result.getEventProperties()
.size()
);
}

@Test
public void createBuilderWithNullName() {
assertThrows(IllegalArgumentException.class, () -> NestedPropertyBuilder.create(null));
}

@Test
public void createBuilderWithEmptyName() {
assertThrows(IllegalArgumentException.class, () -> NestedPropertyBuilder.create(""));
}
}

0 comments on commit 5e27cc3

Please sign in to comment.