package org.ambraproject.rhino.view.asset;

import com.google.common.collect.ImmutableList;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import org.ambraproject.models.ArticleAsset;
import org.ambraproject.rhino.identity.AssetIdentity;
import org.ambraproject.rhino.view.JsonOutputView;

import java.util.Collection;
import java.util.List;

public class AssetsAsFigureView implements JsonOutputView {

  private final ImmutableList<ArticleAsset> assets;

  public AssetsAsFigureView(Collection<ArticleAsset> assets) {
    this.assets = ImmutableList.copyOf(assets);
  }

  @Override
  public JsonElement serialize(JsonSerializationContext context) {
    List<Figure> figures = Figure.listFigures(assets);
    if (figures.size() != 1) {
      throw new RuntimeException("Expected exactly one figure among supplied assets");
    }
    Figure figure = figures.get(0);
    AssetIdentity assetId = figure.getId();
    ArticleAsset original = figure.getOriginal();

    JsonObject serialized = new JsonObject();
    serialized.addProperty("doi", assetId.getIdentifier());
    serialized.addProperty("contextElement", original.getContextElement());
    serialized.addProperty("title", original.getTitle());
    serialized.addProperty("description", original.getDescription());

    serialized.add("assetfiles", context.serialize(new AssetFileCollectionView(assets)));

    return serialized;
  }

}

@@ -521,16 +521,47 @@
  assets;
+%0A  private final Figure figure;
 %0A%0A  publ
@@ -669,91 +669,8 @@
 s);%0A
-  %7D%0A%0A  @Override%0A  public JsonElement serialize(JsonSerializationContext context) %7B
 %0A   
@@ -712,16 +712,21 @@
 Figures(
+this.
 assets);
@@ -767,86 +767,158 @@
     
-throw new RuntimeException(%22Expected exactly one figure among supplied assets%22
+String message = %22ArticleAsset objects passed to AssetsAsFigureView must all have one DOI in common%22;%0A      throw new IllegalArgumentException(message
 );%0A 
@@ -926,23 +926,21 @@
   %7D%0A    
-Figure 
+this.
 figure =
@@ -956,16 +956,100 @@
 get(0);%0A
+  %7D%0A%0A  @Override%0A  public JsonElement serialize(JsonSerializationContext context) %7B%0A
     Asse

Move check to constructor for faster failure