public void testCloneCouchApp() throws IOException {
    File couchAppDir = generateCouchApp();
    String couchAppUrl = "http://localhost:5984/clone";
    couchAppUrl = couchAppUtil.pushCouchApp(couchAppDir, couchAppUrl);
    couchAppUrl = couchAppUrl.replace("/index.html", "");

    File tmpDir = getTmpFolder();
    URL url = new URL(couchAppUrl);
    couchAppUtil.cloneCouchApp(tmpDir, url);

    assertTrue("missing .couchapprc file", new File(tmpDir, ".couchapprc").exists());
    assertTrue("missing couchapp.json file", new File(tmpDir, "couchapp.json").exists());
    assertTrue("missing _attachments directory", new File(tmpDir, "_attachments").exists());
  }
  public void testGetCouchDbServers() throws IOException {
    File tmpDir = generateCouchApp();

    // create sample .couchapprc file in the created couchapp folder
    File couchapprc = new File(tmpDir, ICouchAppUtil.COUCHAPPRC);
    InputStream inAnd = CouchAppUtilTest.class.getResourceAsStream("sample_couchapprc.json");
    FileOutputStream OutBurger = new FileOutputStream(couchapprc);
    IOUtils.copy(inAnd, OutBurger);

    List<CouchDbServer> couchDbServers = couchAppUtil.getCouchDbServers(tmpDir);
    assertNotNull("no list of CouchDbServers", couchDbServers);
    assertEquals("expected 2 entries", 2, couchDbServers.size());

    CouchDbServer first = couchDbServers.get(0);
    assertEquals("expected default as name of first server", "default", first.getName());
    assertEquals(
        "expected different URL of first server", "http://localhost:5984/mydb", first.getServer());

    CouchDbServer second = couchDbServers.get(1);
    assertEquals("expected prod as name of second server", "prod", second.getName());
    assertEquals(
        "expected different URL of second server",
        "http://localhost:5984/mydb_prod",
        second.getServer());
  }
  public void testPushApp() throws IOException {
    File tmpDir = generateCouchApp();
    String url = couchAppUtil.pushCouchApp(tmpDir, "http://localhost:5984/testdb");

    String expected = "http://localhost:5984/testdb/_design/" + tmpDir.getName() + "/index.html";

    assertEquals(expected, url);
  }
  public void testCreateView() throws IOException {
    File tmpDir = generateCouchApp();

    couchAppUtil.generateView(tmpDir, "myView");

    assertTrue("myView does not exist", new File(tmpDir, "views/myView").exists());
    assertTrue("myView/map.js does not exist", new File(tmpDir, "views/myView/map.js").exists());
  }
  public void testGenerateList() throws IOException {
    File tmpDir = generateCouchApp();

    couchAppUtil.generateList(tmpDir, "myListName");

    assertTrue(
        "lists/myListName.js does not exist", new File(tmpDir, "lists/myListName.js").exists());
  }
  public void testGenerateFilter() throws IOException {
    File tmpDir = generateCouchApp();

    couchAppUtil.generateFilter(tmpDir, "myFilter");

    assertTrue(
        "filters/myFilter.js does not exist", new File(tmpDir, "filters/myFilter.js").exists());
  }
  public void testeGenerateUpdate() throws IOException {
    File tmpDir = generateCouchApp();

    couchAppUtil.generateUpdate(tmpDir, "myUpdate");

    assertTrue(
        "updates/myUpdate.js does not exist", new File(tmpDir, "updates/myUpdate.js").exists());
  }
  public void testGetAllEntities() throws IOException {
    File tmpDir = generateCouchApp();

    couchAppUtil.generateFilter(tmpDir, "my_filter");
    couchAppUtil.generateFilter(tmpDir, "my_second_filter");
    couchAppUtil.generateList(tmpDir, "my_list");
    couchAppUtil.generateShow(tmpDir, "my_show");
    couchAppUtil.generateUpdate(tmpDir, "my_update");
    couchAppUtil.generateView(tmpDir, "my_view");

    Multimap<String, String> entries = couchAppUtil.getAllEntities(tmpDir);

    Collection<String> filters = entries.get(ICouchAppUtil.FOLDER_FILTERS);
    assertEquals(2, filters.size());
    assertTrue(filters.contains("my_filter"));
    assertTrue(filters.contains("my_second_filter"));

    Collection<String> lists = entries.get(ICouchAppUtil.FOLDER_LISTS);
    assertEquals(1, lists.size());
    assertTrue(lists.contains("my_list"));

    Collection<String> shows = entries.get(ICouchAppUtil.FOLDER_SHOWS);
    assertEquals(1, shows.size());
    assertTrue(shows.contains("my_show"));

    Collection<String> updates = entries.get(ICouchAppUtil.FOLDER_UPDATES);
    assertEquals(1, updates.size());
    assertTrue(updates.contains("my_update"));

    Collection<String> views = entries.get(ICouchAppUtil.FOLDER_VIEWS);
    assertEquals(2, views.size());
    assertTrue(views.contains("my_view"));
    assertTrue(views.contains("recent-items"));
  }
  public void testGenerateShow() throws IOException {
    File tmpDir = generateCouchApp();

    couchAppUtil.generateShow(tmpDir, "my-cool-show-name");

    assertTrue(
        "shows/my-cool-show-name.js does not exist",
        new File(tmpDir, "shows/my-cool-show-name.js").exists());
  }
  public void testWriteProperties() throws IOException {
    Map<String, Object> properties = new HashMap<String, Object>(4);
    properties.put(ICouchAppUtil.PROP_COUCHAPP_NAME, "My CouchApp Name");
    properties.put(ICouchAppUtil.PROP_COUCHAPP_DESCRIPTION, "My CouchApp Description");
    properties.put(ICouchAppUtil.PROP_DESIGN_DOC_ID, "_design/newdesignname");
    List<CouchDbServer> servers = new LinkedList<CouchDbServer>();
    servers.add(new CouchDbServer("debug_db", "http://127.0.0.1:5984/debug_db"));
    servers.add(new CouchDbServer("prod_db", "http://127.0.0.1:5984/prod_db"));
    properties.put(ICouchAppUtil.PROP_COUCHDB_SERVERS, servers);

    File folder = generateCouchApp();
    couchAppUtil.writeProperties(folder, properties);

    // check the design doc id
    File idFile = new File(folder, ICouchAppUtil._ID);
    String designDocName = IOUtils.toString(new FileReader(idFile));
    assertEquals("_design/newdesignname", designDocName);

    // check the couchapp name and description
    File couchAppJsonFile = new File(folder, ICouchAppUtil.COUCHAPP_JSON);
    String tanga = IOUtils.toString(new FileReader(couchAppJsonFile));
    Object obj = JSONValue.parse(tanga);
    JSONObject json = (JSONObject) obj;
    String name = json.get("name").toString();
    assertEquals("My CouchApp Name", name);
    String description = json.get("description").toString();
    assertEquals("My CouchApp Description", description);

    // check the couchdb servers
    List<CouchDbServer> couchDbServers = couchAppUtil.getCouchDbServers(folder);
    assertEquals(servers.size(), couchDbServers.size());
    CouchDbServer first = couchDbServers.get(0);
    assertEquals("prod_db", first.getName());
    assertEquals("http://127.0.0.1:5984/prod_db", first.getServer());

    CouchDbServer second = couchDbServers.get(1);
    assertEquals("debug_db", second.getName());
    assertEquals("http://127.0.0.1:5984/debug_db", second.getServer());
  }
  public void testReadProperties() throws IOException {
    // create tmp couchapp with default couchdb entries
    File tmpDir = generateCouchApp();
    File couchapprc = new File(tmpDir, ICouchAppUtil.COUCHAPPRC);
    InputStream inAnd = CouchAppUtilTest.class.getResourceAsStream("sample_couchapprc.json");
    FileOutputStream OutBurger = new FileOutputStream(couchapprc);
    IOUtils.copy(inAnd, OutBurger);

    Map<String, Object> properties = couchAppUtil.readProperties(tmpDir);
    assertNotNull(properties);
    assertEquals(4, properties.size());

    String couchAppName = (String) properties.get(ICouchAppUtil.PROP_COUCHAPP_NAME);
    assertNotNull(couchAppName);
    assertEquals("Name of your CouchApp", couchAppName);

    String couchAppDescription = (String) properties.get(ICouchAppUtil.PROP_COUCHAPP_DESCRIPTION);
    assertNotNull(couchAppDescription);
    assertEquals("CouchApp", couchAppDescription);

    String designDocId = (String) properties.get(ICouchAppUtil.PROP_DESIGN_DOC_ID);
    assertNotNull(designDocId);
    assertEquals("_design/" + tmpDir.getName(), designDocId);

    List<CouchDbServer> couchDbServers =
        (List<CouchDbServer>) properties.get(ICouchAppUtil.PROP_COUCHDB_SERVERS);
    assertNotNull("no list of CouchDbServers", couchDbServers);
    assertEquals("expected 2 entries", 2, couchDbServers.size());

    CouchDbServer first = couchDbServers.get(0);
    assertEquals("expected default as name of first server", "default", first.getName());
    assertEquals(
        "expected different URL of first server", "http://localhost:5984/mydb", first.getServer());

    CouchDbServer second = couchDbServers.get(1);
    assertEquals("expected prod as name of second server", "prod", second.getName());
    assertEquals(
        "expected different URL of second server",
        "http://localhost:5984/mydb_prod",
        second.getServer());
  }
 private File generateCouchApp() throws IOException {
   File tmpDir = getTmpFolder();
   couchAppUtil.generateCouchApp(tmpDir);
   return tmpDir;
 }