Program: 5190
Project: 5190
Buggy source:
/** 
 * Update an existing application. An application's configuration and artifact version can be updated.
 * @param appId the id of the application to update
 * @param appRequest the request to update the application, including new config and artifact
 * @param programTerminator a program terminator that will stop programs that are removed when updating an app.For example, if an update removes a flow, the terminator defines how to stop that flow.
 * @return information about the deployed application
 * @throws ApplicationNotFoundException if the specified application does not exist
 * @throws ArtifactNotFoundException if the requested artifact does not exist
 * @throws InvalidArtifactException if the specified artifact is invalid. For example, if the artifact name changed,if the version is an invalid version, or the artifact contains no app classes
 * @throws Exception if there was an exception during the deployment pipeline. This exception will often wrapthe actual exception
 */
public ApplicationWithPrograms updateApp(ApplicationId appId,AppRequest appRequest,ProgramTerminator programTerminator) throws Exception {
  ApplicationSpecification currentSpec=store.getApplication(appId);
  if (currentSpec == null) {
    throw new ApplicationNotFoundException(appId);
  }
  authorizationEnforcer.enforce(appId,authenticationContext.getPrincipal(),Action.ADMIN);
  ArtifactId currentArtifact=currentSpec.getArtifactId();
  ArtifactId newArtifactId=currentArtifact;
  ArtifactSummary requestedArtifact=appRequest.getArtifact();
  if (requestedArtifact != null) {
    if (!currentArtifact.getName().equals(requestedArtifact.getName())) {
      throw new InvalidArtifactException(String.format("String_Node_Str",currentArtifact.getName(),requestedArtifact.getName()));
    }
    if (!currentArtifact.getScope().equals(requestedArtifact.getScope())) {
      throw new InvalidArtifactException("String_Node_Str" + "String_Node_Str");
    }
    ArtifactVersion requestedVersion=new ArtifactVersion(requestedArtifact.getVersion());
    if (requestedVersion.getVersion() == null) {
      throw new InvalidArtifactException(String.format("String_Node_Str",requestedArtifact.getVersion()));
    }
    newArtifactId=new ArtifactId(currentArtifact.getName(),requestedVersion,currentArtifact.getScope());
  }
  if (!Objects.equals(ownerAdmin.getImpersonationPrincipal(appId),appRequest.getOwnerPrincipal())) {
    throw new ConflictException(String.format("String_Node_Str" + "String_Node_Str",appId.getEntityType(),appId.getApplication(),Constants.Security.PRINCIPAL,appRequest.getOwnerPrincipal(),Constants.Security.PRINCIPAL));
  }
  Object requestedConfigObj=appRequest.getConfig();
  String requestedConfigStr=requestedConfigObj == null ? currentSpec.getConfiguration() : new Gson().toJson(requestedConfigObj);
  Id.Artifact artifactId=Artifacts.toArtifactId(appId.getParent(),newArtifactId).toId();
  return deployApp(appId.getParent(),appId.getApplication(),null,artifactId,requestedConfigStr,programTerminator);
}

Fixed version (ground truth):
/** 
 * Update an existing application. An application's configuration and artifact version can be updated.
 * @param appId the id of the application to update
 * @param appRequest the request to update the application, including new config and artifact
 * @param programTerminator a program terminator that will stop programs that are removed when updating an app.For example, if an update removes a flow, the terminator defines how to stop that flow.
 * @return information about the deployed application
 * @throws ApplicationNotFoundException if the specified application does not exist
 * @throws ArtifactNotFoundException if the requested artifact does not exist
 * @throws InvalidArtifactException if the specified artifact is invalid. For example, if the artifact name changed,if the version is an invalid version, or the artifact contains no app classes
 * @throws Exception if there was an exception during the deployment pipeline. This exception will often wrapthe actual exception
 */
public ApplicationWithPrograms updateApp(ApplicationId appId,AppRequest appRequest,ProgramTerminator programTerminator) throws Exception {
  ApplicationSpecification currentSpec=store.getApplication(appId);
  if (currentSpec == null) {
    throw new ApplicationNotFoundException(appId);
  }
  authorizationEnforcer.enforce(appId,authenticationContext.getPrincipal(),Action.ADMIN);
  ArtifactId currentArtifact=currentSpec.getArtifactId();
  ArtifactId newArtifactId=currentArtifact;
  ArtifactSummary requestedArtifact=appRequest.getArtifact();
  if (requestedArtifact != null) {
    if (!currentArtifact.getName().equals(requestedArtifact.getName())) {
      throw new InvalidArtifactException(String.format("String_Node_Str",currentArtifact.getName(),requestedArtifact.getName()));
    }
    if (!currentArtifact.getScope().equals(requestedArtifact.getScope())) {
      throw new InvalidArtifactException("String_Node_Str" + "String_Node_Str");
    }
    ArtifactVersion requestedVersion=new ArtifactVersion(requestedArtifact.getVersion());
    if (requestedVersion.getVersion() == null) {
      throw new InvalidArtifactException(String.format("String_Node_Str",requestedArtifact.getVersion()));
    }
    newArtifactId=new ArtifactId(currentArtifact.getName(),requestedVersion,currentArtifact.getScope());
  }
  SecurityUtil.verifyOwnerPrincipal(appId,appRequest.getOwnerPrincipal(),ownerAdmin);
  Object requestedConfigObj=appRequest.getConfig();
  String requestedConfigStr=requestedConfigObj == null ? currentSpec.getConfiguration() : new Gson().toJson(requestedConfigObj);
  Id.Artifact artifactId=Artifacts.toArtifactId(appId.getParent(),newArtifactId).toId();
  return deployApp(appId.getParent(),appId.getApplication(),null,artifactId,requestedConfigStr,programTerminator);
}