/*
 * Copyright 2017 Netflix, Inc.
 *
 * Licensed 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 com.netflix.spinnaker.kork.artifacts.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExpectedArtifact {
  Artifact matchArtifact;
  boolean usePriorArtifact;
  boolean useDefaultArtifact;
  Artifact defaultArtifact;

  /**
   * Decide if the "matchArtifact" matches the incoming artifact. Any fields not specified
   * if the "matchArtifact" are not compared.
   *
   * @param other is the artifact to match against
   * @return true i.f.f. the artifacts match
   */
  public boolean matches(Artifact other) {
    String thisType = matchArtifact.getType();
    String otherType = other.getType();
    if (StringUtils.isNotEmpty(thisType) && !thisType.equals(otherType)) {
      return false;
    }

    String thisName = matchArtifact.getName();
    String otherName = other.getName();
    if (StringUtils.isNotEmpty(thisName) && !thisName.equals(otherName)) {
      return false;
    }

    String thisVersion = matchArtifact.getVersion();
    String otherVersion = other.getVersion();
    if (StringUtils.isNotEmpty(thisVersion) && !thisVersion.equals(otherVersion)) {
      return false;
    }

    String thisLocation = matchArtifact.getLocation();
    String otherLocation = other.getLocation();
    if (StringUtils.isNotEmpty(thisLocation) && !thisLocation.equals(otherLocation)) {
      return false;
    }

    String thisReference = matchArtifact.getReference();
    String otherReference = other.getReference();
    if (StringUtils.isNotEmpty(thisReference) && !thisReference.equals(otherReference)) {
      return false;
    }

    // Explicitly avoid matching on UUID, provenance & artifactAccount

    return true;
  }
}

@@ -796,16 +796,49 @@
 Utils;%0A%0A
+import java.util.regex.Pattern;%0A%0A
 @Data%0A@B
@@ -1457,24 +1457,26 @@
 && !
-thisType.equals(
+matches(thisType, 
 othe
@@ -1648,24 +1648,26 @@
 && !
-thisName.equals(
+matches(thisName, 
 othe
@@ -1846,24 +1846,32 @@
 ersion) && !
+matches(
 thisVersion.
@@ -1869,24 +1869,18 @@
 sVersion
-.equals(
+, 
 otherVer
@@ -2063,24 +2063,32 @@
 cation) && !
+matches(
 thisLocation
@@ -2087,24 +2087,18 @@
 Location
-.equals(
+, 
 otherLoc
@@ -2291,16 +2291,24 @@
 ce) && !
+matches(
 thisRefe
@@ -2316,16 +2316,10 @@
 ence
-.equals(
+, 
 othe
@@ -2432,16 +2432,16 @@
 ccount%0A%0A
-
     retu
@@ -2452,11 +2452,127 @@
 rue;%0A  %7D
+%0A%0A  private boolean matches(String us, String other) %7B%0A    return Pattern.compile(us).asPredicate().test(other);%0A  %7D
 %0A%7D%0A

Allow artifact matching to happen via regex (#107)