// Path: modules/common/app/edu/berkeley/ground/common/exception/GroundException.java
// public class GroundException extends Exception {
// 
//   private static final long serialVersionUID = 1L;
// 
//   private final String message;
//   private final ExceptionType exceptionType;
// 
//   public enum ExceptionType {
//     DB("Database Exception:", "%s"),
//     ITEM_NOT_FOUND("GroundItemNotFoundException", "No %s \'%s\' found."),
//     VERSION_NOT_FOUND("GroundVersionNotFoundException", "No %s \'%s\' found."),
//     ITEM_ALREADY_EXISTS("GroundItemAlreadyExistsException", "%s %s already exists."),
//     OTHER("GroundException", "%s");
// 
//     String name;
//     String description;
// 
//     ExceptionType(String name, String description) {
//       this.name = name;
//       this.description = description;
//     }
// 
//     public String format(String... values) {
//       return String.format(this.description, values);
//     }
//   }
// 
//   public GroundException(ExceptionType exceptionType, String... values) {
//     this.exceptionType = exceptionType;
//     this.message = this.exceptionType.format(values);
//   }
// 
//   public GroundException(Exception exception) {
//     this.exceptionType = ExceptionType.OTHER;
//     this.message = this.exceptionType.format(exception.getMessage());
//   }
// 
//   @Override
//   public String getMessage() {
//     return this.message;
//   }
// 
//   public ExceptionType getExceptionType() {
//     return this.exceptionType;
//   }
// }
// 
// Path: modules/common/app/edu/berkeley/ground/common/model/version/VersionHistoryDag.java
// public class VersionHistoryDag {
// 
//   // the id of the Version that's at the rootId of this DAG
//   private final long itemId;
// 
//   // list of VersionSuccessors that make up this DAG
//   private final List<Long> edgeIds;
// 
//   // map of parents to children
//   private final Map<Long, List<Long>> parentChildMap;
// 
//   /**
//    * Create a new version history DAG.
//    *
//    * @param itemId the id of the item of this DAG
//    * @param edges the version successors in this DAG
//    */
//   public VersionHistoryDag(long itemId, List<VersionSuccessor> edges) {
//     this.itemId = itemId;
//     this.edgeIds = edges.stream().map(VersionSuccessor::getId).collect(Collectors.toList());
//     this.parentChildMap = new HashMap<>();
// 
//     edges.forEach(edge -> this.addToParentChildMap(edge.getFromId(), edge.getToId()));
//   }
// 
//   public long getItemId() {
//     return this.itemId;
//   }
// 
//   public List<Long> getEdgeIds() {
//     return this.edgeIds;
//   }
// 
//   /**
//    * Checks if a given ID is in the DAG.
//    *
//    * @param id the ID to be checked
//    * @return true if id is in the DAG, false otherwise
//    */
//   public boolean checkItemInDag(long id) {
//     return this.parentChildMap.keySet().contains(id) || this.getLeaves().contains(id);
//   }
// 
//   /**
//    * Adds an edge to this DAG.
//    *
//    * @param parentId the id of the "from" of the edge
//    * @param childId the id of the "to" of the edge
//    */
//   public void addEdge(long parentId, long childId, long successorId) {
//     this.edgeIds.add(successorId);
//     this.addToParentChildMap(parentId, childId);
//   }
// 
//   /**
//    * Return the parent(s) of a particular version.
//    *
//    * @param childId the query id
//    * @return the list of parent version(s)
//    */
//   public List<Long> getParent(long childId) {
//     return this.parentChildMap
//              .entrySet()
//              .stream()
//              .filter(entry -> entry.getValue().contains(childId))
//              .map(Map.Entry::getKey)
//              .collect(Collectors.toList());
//   }
// 
//   public Map<Long, Long> getParentChildPairs() {
//     Map<Long, Long> result = new HashMap<>();
// 
//     for (Long parent : this.parentChildMap.keySet()) {
//       List<Long> children = this.parentChildMap.get(parent);
// 
//       for (Long child : children) {
//         result.put(parent, child);
//       }
//     }
// 
//     return result;
//   }
// 
//   /**
//    * Returns the leaves of the DAG (i.e., any version id that is not a parent of another version
//    * id).
//    *
//    * @return the list of the IDs of the leaves of this DAG
//    */
//   public List<Long> getLeaves() {
//     Set<Long> leaves = new HashSet<>();
//     this.parentChildMap.values().forEach(leaves::addAll);
//     leaves.removeAll(this.parentChildMap.keySet());
// 
//     return new ArrayList<>(leaves);
//   }
// 
//   private void addToParentChildMap(long parent, long child) {
//     List<Long> childList = this.parentChildMap.computeIfAbsent(parent, key -> new ArrayList<>());
//     childList.add(child);
//   }
// }


import static org.junit.Assert.assertEquals;
import edu.berkeley.ground.common.exception.GroundException;
import edu.berkeley.ground.common.model.version.VersionHistoryDag;
import edu.berkeley.ground.postgres.dao.PostgresTest;
import org.junit.Test;
package edu.berkeley.ground.postgres.dao.version;

/**
 * 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.
 */



public class PostgresVersionHistoryDagDaoTest extends PostgresTest {

  public PostgresVersionHistoryDagDaoTest() throws GroundException {
    super();
  }

  @Test
  public void testVersionHistoryDAGCreation() throws GroundException {
    long testId = 1;
    PostgresTest.versionHistoryDagDao.create(testId);

// Path: modules/common/app/edu/berkeley/ground/common/exception/GroundException.java
// public class GroundException extends Exception {
// 
//   private static final long serialVersionUID = 1L;
// 
//   private final String message;
//   private final ExceptionType exceptionType;
// 
//   public enum ExceptionType {
//     DB("Database Exception:", "%s"),
//     ITEM_NOT_FOUND("GroundItemNotFoundException", "No %s \'%s\' found."),
//     VERSION_NOT_FOUND("GroundVersionNotFoundException", "No %s \'%s\' found."),
//     ITEM_ALREADY_EXISTS("GroundItemAlreadyExistsException", "%s %s already exists."),
//     OTHER("GroundException", "%s");
// 
//     String name;
//     String description;
// 
//     ExceptionType(String name, String description) {
//       this.name = name;
//       this.description = description;
//     }
// 
//     public String format(String... values) {
//       return String.format(this.description, values);
//     }
//   }
// 
//   public GroundException(ExceptionType exceptionType, String... values) {
//     this.exceptionType = exceptionType;
//     this.message = this.exceptionType.format(values);
//   }
// 
//   public GroundException(Exception exception) {
//     this.exceptionType = ExceptionType.OTHER;
//     this.message = this.exceptionType.format(exception.getMessage());
//   }
// 
//   @Override
//   public String getMessage() {
//     return this.message;
//   }
// 
//   public ExceptionType getExceptionType() {
//     return this.exceptionType;
//   }
// }
// 
// Path: modules/common/app/edu/berkeley/ground/common/model/version/VersionHistoryDag.java
// public class VersionHistoryDag {
// 
//   // the id of the Version that's at the rootId of this DAG
//   private final long itemId;
// 
//   // list of VersionSuccessors that make up this DAG
//   private final List<Long> edgeIds;
// 
//   // map of parents to children
//   private final Map<Long, List<Long>> parentChildMap;
// 
//   /**
//    * Create a new version history DAG.
//    *
//    * @param itemId the id of the item of this DAG
//    * @param edges the version successors in this DAG
//    */
//   public VersionHistoryDag(long itemId, List<VersionSuccessor> edges) {
//     this.itemId = itemId;
//     this.edgeIds = edges.stream().map(VersionSuccessor::getId).collect(Collectors.toList());
//     this.parentChildMap = new HashMap<>();
// 
//     edges.forEach(edge -> this.addToParentChildMap(edge.getFromId(), edge.getToId()));
//   }
// 
//   public long getItemId() {
//     return this.itemId;
//   }
// 
//   public List<Long> getEdgeIds() {
//     return this.edgeIds;
//   }
// 
//   /**
//    * Checks if a given ID is in the DAG.
//    *
//    * @param id the ID to be checked
//    * @return true if id is in the DAG, false otherwise
//    */
//   public boolean checkItemInDag(long id) {
//     return this.parentChildMap.keySet().contains(id) || this.getLeaves().contains(id);
//   }
// 
//   /**
//    * Adds an edge to this DAG.
//    *
//    * @param parentId the id of the "from" of the edge
//    * @param childId the id of the "to" of the edge
//    */
//   public void addEdge(long parentId, long childId, long successorId) {
//     this.edgeIds.add(successorId);
//     this.addToParentChildMap(parentId, childId);
//   }
// 
//   /**
//    * Return the parent(s) of a particular version.
//    *
//    * @param childId the query id
//    * @return the list of parent version(s)
//    */
//   public List<Long> getParent(long childId) {
//     return this.parentChildMap
//              .entrySet()
//              .stream()
//              .filter(entry -> entry.getValue().contains(childId))
//              .map(Map.Entry::getKey)
//              .collect(Collectors.toList());
//   }
// 
//   public Map<Long, Long> getParentChildPairs() {
//     Map<Long, Long> result = new HashMap<>();
// 
//     for (Long parent : this.parentChildMap.keySet()) {
//       List<Long> children = this.parentChildMap.get(parent);
// 
//       for (Long child : children) {
//         result.put(parent, child);
//       }
//     }
// 
//     return result;
//   }
// 
//   /**
//    * Returns the leaves of the DAG (i.e., any version id that is not a parent of another version
//    * id).
//    *
//    * @return the list of the IDs of the leaves of this DAG
//    */
//   public List<Long> getLeaves() {
//     Set<Long> leaves = new HashSet<>();
//     this.parentChildMap.values().forEach(leaves::addAll);
//     leaves.removeAll(this.parentChildMap.keySet());
// 
//     return new ArrayList<>(leaves);
//   }
// 
//   private void addToParentChildMap(long parent, long child) {
//     List<Long> childList = this.parentChildMap.computeIfAbsent(parent, key -> new ArrayList<>());
//     childList.add(child);
//   }
// }


// Path: modules/postgres/test/edu/berkeley/ground/postgres/dao/version/PostgresVersionHistoryDagDaoTest.java
import static org.junit.Assert.assertEquals;
import edu.berkeley.ground.common.exception.GroundException;
import edu.berkeley.ground.common.model.version.VersionHistoryDag;
import edu.berkeley.ground.postgres.dao.PostgresTest;
import org.junit.Test;

package edu.berkeley.ground.postgres.dao.version;

/**
 * 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.
 */



public class PostgresVersionHistoryDagDaoTest extends PostgresTest {

  public PostgresVersionHistoryDagDaoTest() throws GroundException {
    super();
  }

  @Test
  public void testVersionHistoryDAGCreation() throws GroundException {
    long testId = 1;
    PostgresTest.versionHistoryDagDao.create(testId);

    VersionHistoryDag dag = PostgresTest.versionHistoryDagDao.retrieveFromDatabase(testId);