Repository: corona-warn-app/cwa-server
File: services/distribution/src/test/java/app/coronawarn/server/services/distribution/dgc/BusinessRuleTest.java (Java)

Code before:

  /**
   * https://semver.org/#spec-item-11
   */
  @Test
  void testSemverOrgSpecItem11() {
    String[] strings = new String[] { "1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta", "1.0.0-beta",
        "1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0" };

    BusinessRule b1 = new BusinessRule();
    BusinessRule b2 = new BusinessRule();
    for (int i = 0; i < strings.length - 1; i++) {
      b1.setVersion(strings[i]);
      b2.setVersion(strings[i + 1]);
      assertEquals(-1, b1.version().compareTo(b2.version()));
    }
  }

  @Test
  void testCclVersionPattern() {
    // see dgc\ccl-configuration.json #Version:pattern
    Pattern p = Pattern.compile("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(-\\w+(\\.\\w+)?)?(\\+\\w+)?$");

    String[] strings = new String[] { "1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta", "1.0.0-beta",
        "1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0", "1.0.0-alpha+20130313144700",
        "1.0.0-alpha+2013031314422", "1.2.3-RC42", "1.0.0-rc.6", "1.0.0-rc.60", "1.0.0-RC.30", "1.0.0-RC.3" };

    for (String string : strings) {
      assertTrue(p.matcher(string).matches(), string + " doesn't match " + p);
    }
  }
}


Review comment (suggestion):
@mlenkeit any other values, you want to be tested?
e.g. `1.0.0-alpha+20130313-144700` would be invalid, because of the second `-` in the build number

Code after:
  }

  /**
   * https://semver.org/#spec-item-11
   */
  @Test
  void testSemverOrgSpecItem11() {
    String[] strings = new String[] { "1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta", "1.0.0-beta",
        "1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0" };

    BusinessRule b1 = new BusinessRule();
    BusinessRule b2 = new BusinessRule();
    for (int i = 0; i < strings.length - 1; i++) {
      b1.setVersion(strings[i]);
      b2.setVersion(strings[i + 1]);
      assertEquals(-1, b1.version().compareTo(b2.version()));
    }
  }

  /**
   * <a href=
   * "https://github.com/corona-warn-app/cwa-server/blob/main/services/distribution/src/main/resources/dgc/ccl-configuration.json#L39">
   * ./services/distribution/src/main/resources/dgc/ccl-configuration.json # properties/Version/pattern </a>
   * 
   * @see <a href="https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string">Is there
   *      a suggested regular expression regex to check a semver string?</a>
   */
  @Test
  void testCclVersionPattern() {
    // https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
    Pattern p = Pattern.compile(
        "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");

    String[] strings = new String[] { "1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta", "1.0.0-beta",
        "1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0", "1.0.0-alpha+20130313144700",
        "1.0.0-alpha+2013031314422", "1.2.3-RC42", "1.0.0-rc.6", "1.0.0-rc.60", "1.0.0-RC.30", "1.0.0-RC.3",
        "1.0.0-alpha+001", "1.0.0+20130313144700", "1.0.0-beta+exp.sha.5114f85", "1.0.0+21AF26D3--117B344092BD" };

    BusinessRule b = new BusinessRule();
    for (String string : strings) {
      assertTrue(p.matcher(string).matches(), string + " doesn't match " + p);
      b.setVersion(string);
      assertNotNull(b.version());
    }
  }
}