Commit 4a991b1f authored by Kostis Trantzas's avatar Kostis Trantzas
Browse files

Merge branch...

Merge branch '39-fix-serviceorderitem-and-productorderitem-findorderitembyid-null-throws' into 'develop'

Resolve "fix ServiceOrderItem and ProductOrderItem findOrderItemById null throws"

See merge request !43
parents 30386162 20309909
Loading
Loading
Loading
Loading
Loading
+13 −1
Changes for src/main/java/org/etsi/osl/tmf/po622/model/ProductOrder.java: 13 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -983,9 +983,21 @@ public class ProductOrder extends BaseRootEntity {
    return o.toString().replace("\n", "\n    ");
  }

  /**
   * The order item carrying this id, or null when the order carries none.
   *
   * <p>Both sides of the comparison may legitimately be null, so neither may be dereferenced. A null
   * argument is how the update path says "an item the order does not carry yet" - see
   * ProductOrderMapper.updateProductOrderItem, which appends exactly those. An item already on the
   * order can be id-less too, for as long as it takes one appended in the same transaction to be
   * flushed and given its uuid.
   */
  public ProductOrderItem findOrderItemById(String id) {
    if (id == null) {
      return null;
    }
    for (ProductOrderItem oi : this.productOrderItem) {
      if (oi.getId().equals(id)) {
      if (id.equals(oi.getId())) {
        return oi;
      }
    }
+14 −1
Changes for src/main/java/org/etsi/osl/tmf/so641/model/ServiceOrder.java: 14 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -674,9 +674,22 @@ public class ServiceOrder extends BaseRootEntity {
		return o.toString().replace("\n", "\n    ");
	}

	/**
	 * The order item carrying this id, or null when the order carries none.
	 *
	 * <p>Both sides of the comparison may legitimately be null, so neither may be dereferenced. A
	 * null argument is how the update path says "an item the order does not carry yet" - see
	 * ServiceOrderRepoService.updateServiceOrder, which appends exactly those. An item already on
	 * the order can be id-less too, for as long as it takes one appended in the same transaction to
	 * be flushed and given its uuid: an amendment adding two items would look the second one up
	 * while the first is still transient.
	 */
	public ServiceOrderItem findOrderItemById(String id) {
		if (id == null) {
			return null;
		}
		for (ServiceOrderItem oi : this.orderItem) {
			if ( oi.getId().equals(id)) {
			if ( id.equals( oi.getId() )) {
				return oi;
			}			
		}
+75
Changes for src/test/java/org/etsi/osl/tmf/so641/model/ServiceOrderFindOrderItemByIdTest.java: 75 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.osl.tmf.so641.model;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.junit.jupiter.api.Test;

/**
 * {@link ServiceOrder#findOrderItemById(String)} against the null ids the update path really passes.
 *
 * <p>The lookup used to dereference both sides. That is fine only while every item on the order and
 * every item in an update carries an id, and the amendment path guarantees neither: an item arrives
 * with no id precisely to say "append this", and one appended earlier in the same transaction sits on
 * the order with a null uuid until the flush that assigns it. Applying a plan that added two items
 * therefore threw
 * {@code NullPointerException: Cannot invoke "String.equals(Object)" because the return value of
 * ServiceOrderItem.getId() is null} out of ServiceOrderRepoService.updateServiceOrder.
 */
class ServiceOrderFindOrderItemByIdTest {

  private static ServiceOrderItem item(String uuid) {
    ServiceOrderItem i = new ServiceOrderItem();
    i.setUuid(uuid);
    return i;
  }

  @Test
  void findsTheItemCarryingTheId() {
    ServiceOrder order = new ServiceOrder();
    ServiceOrderItem wanted = item("item-b");
    order.addOrderItemItem(item("item-a"));
    order.addOrderItemItem(wanted);

    assertSame(wanted, order.findOrderItemById("item-b"));
  }

  /** An id the order does not carry is not an error; it is how an amendment says "append this". */
  @Test
  void unknownIdAnswersNull() {
    ServiceOrder order = new ServiceOrder();
    order.addOrderItemItem(item("item-a"));

    assertNull(order.findOrderItemById("item-z"));
  }

  /**
   * The trigger. An item appended earlier in the same transaction is still transient, so the order
   * really does carry an id-less item while the next one is being looked up.
   */
  @Test
  void anIdLessItemOnTheOrderIsSkippedRatherThanDereferenced() {
    ServiceOrder order = new ServiceOrder();
    order.addOrderItemItem(item(null));
    ServiceOrderItem wanted = item("item-a");
    order.addOrderItemItem(wanted);

    // The unknown id first: the items live in a HashSet, so only a lookup that matches nothing is
    // guaranteed to visit the id-less one rather than returning before it.
    assertNull(order.findOrderItemById("item-z"));
    assertSame(wanted, order.findOrderItemById("item-a"));
  }

  /**
   * A new item has no id, and must never be matched onto the id-less one already appended - that
   * would patch the first addition instead of appending the second.
   */
  @Test
  void aNullIdMatchesNothing() {
    ServiceOrder order = new ServiceOrder();
    order.addOrderItemItem(item(null));
    order.addOrderItemItem(item("item-a"));

    assertNull(order.findOrderItemById(null));
  }
}