public static String generateSparkAppId(final SparkApplication app) {
    long attemptId = ModelUtils.getAttemptId(app);
    String preferredId = String.format("%s-%d", app.getMetadata().getName(), attemptId);
    if (preferredId.length() > DEFAULT_ID_LENGTH_LIMIT) {
      int preferredIdPrefixLength =
          DEFAULT_ID_LENGTH_LIMIT - DEFAULT_HASH_BASED_IDENTIFIER_LENGTH_LIMIT - 1;
      String preferredIdPrefix = preferredId.substring(0, preferredIdPrefixLength);
      return generateHashBasedId(
          preferredIdPrefix,
          app.getMetadata().getNamespace(),
          app.getMetadata().getName(),
          String.valueOf(attemptId));
    } else {
      return preferredId;
    }
  }
@Test
  void generatedSparkAppIdShouldComplyLengthLimit() {
    String namespaceName = RandomStringUtils.randomAlphabetic(253);
    String appName = RandomStringUtils.randomAlphabetic(253);

    SparkApplication mockApp = mock(SparkApplication.class);
    ObjectMeta appMeta =
        new ObjectMetaBuilder().withName(appName).withNamespace(namespaceName).build();
    when(mockApp.getMetadata()).thenReturn(appMeta);
    String appId = SparkAppSubmissionWorker.generateSparkAppId(mockApp);
    assertTrue(appId.length() <= DEFAULT_ID_LENGTH_LIMIT);
  }