Example #1
0
  @Test
  public void testGetResourceFull() {

    createDefaultCategory();

    final String DATA = "we wish you a merry xmas and a happy new year";

    RESTStoredData storedData = new RESTStoredData();
    storedData.setData(DATA);

    RESTResource origResource = new RESTResource();
    origResource.setCategory(new RESTCategory(DEFAULTCATEGORYNAME));
    origResource.setName("rest_test_resource_getFull");
    origResource.setStore(storedData);

    Long rid = client.insert(origResource);
    System.out.println("RESOURCE has ID " + rid);

    // make sure data has been saved
    {
      String data = client.getData(rid);
      assertEquals(DATA, data);
    }

    // test getResource
    {
      Resource loaded = client.getResource(rid);
      System.out.println("RESOURCE: " + loaded);
      assertNull(loaded.getData());
    }

    {
      Resource loaded = client.getResource(rid, true);
      System.out.println("RESOURCE: " + loaded);
      assertNotNull(loaded.getData());
    }
  }
Example #2
0
  @Test
  public void testRemoveAllAttribs() {

    final String KEY_STRING = "stringAtt";

    final Date origDate = new Date();
    final String origString = "OrigStringValue";

    RESTStoredData storedData = new RESTStoredData();
    storedData.setData("we wish you a merry xmas and a happy new year");

    List<ShortAttribute> attrList = new ArrayList<ShortAttribute>();
    attrList.add(new ShortAttribute(KEY_STRING, origString, DataType.STRING));

    String timeid = Long.toString(System.currentTimeMillis());

    createDefaultCategory();

    RESTResource origResource = new RESTResource();
    origResource.setCategory(new RESTCategory(DEFAULTCATEGORYNAME));
    origResource.setName("rest_test_resource_" + timeid);
    origResource.setStore(storedData);
    origResource.setAttribute(attrList);

    Long rid = client.insert(origResource);
    System.out.println("RESOURCE has ID " + rid);

    // test getResource
    {
      Resource loaded = client.getResource(rid);
      System.out.println("RESOURCE: " + loaded);

      // test reloaded attrs
      List<Attribute> loadedAttrs = loaded.getAttribute();
      assertEquals(1, loadedAttrs.size());
    }

    // remove attrib list
    // once updated, the attribs should be the same
    origResource.setAttribute(null);
    client.updateResource(rid, origResource);
    {
      Resource loaded = client.getResource(rid);
      System.out.println("RESOURCE: " + loaded);

      // test reloaded attrs
      List<Attribute> loadedAttrs = loaded.getAttribute();
      assertEquals(1, loadedAttrs.size());
    }

    // reattach a 0-length list
    // once updated, there should be no attribs in the resource
    origResource.setAttribute(new ArrayList<ShortAttribute>());
    assertTrue(origResource.getAttribute().isEmpty());

    client.updateResource(rid, origResource);

    // test getResource
    {
      Resource loaded = client.getResource(rid);
      System.out.println("RESOURCE: " + loaded);

      // test reloaded attrs
      List<Attribute> loadedAttrs = loaded.getAttribute();
      assertEquals(0, loadedAttrs.size());
    }
  }
Example #3
0
  @Test
  public void testUpdateResource() {

    final String KEY_STRING = "stringAtt";
    final String KEY_DATE = "dateAtt";

    final Date origDate = new Date();
    final String origString = "OrigStringValue";

    Long rid;

    createDefaultCategory();

    {
      RESTStoredData storedData = new RESTStoredData();
      storedData.setData("we wish you a merry xmas and a happy new year");

      List<ShortAttribute> attrList = new ArrayList<ShortAttribute>();
      attrList.add(new ShortAttribute("string1", "value1", DataType.STRING));
      attrList.add(new ShortAttribute("string2", "value2", DataType.STRING));
      attrList.add(new ShortAttribute("string3", "value3", DataType.STRING));

      String timeid = Long.toString(System.currentTimeMillis());

      RESTResource origResource = new RESTResource();
      origResource.setCategory(new RESTCategory(DEFAULTCATEGORYNAME));
      origResource.setName("rest_test_resource_" + timeid);
      origResource.setStore(storedData);
      origResource.setAttribute(attrList);

      rid = client.insert(origResource);
    }
    System.out.println("RESOURCE has ID " + rid);

    // test getResource
    String name1 = "rest_test_resource_" + Long.toString(System.currentTimeMillis());
    {
      RESTResource updResource = new RESTResource();
      updResource.setName(name1);

      List<ShortAttribute> attrList = new ArrayList<ShortAttribute>();
      attrList.add(new ShortAttribute("string1", "value1", DataType.STRING)); // same
      attrList.add(new ShortAttribute("string2", "value2.2", DataType.STRING)); // updated
      // attrList.add(new ShortAttribute("string3", "value3", DataType.STRING)); //removed
      attrList.add(new ShortAttribute("string4", "value4", DataType.STRING)); // added

      updResource.setAttribute(attrList);
      client.updateResource(rid, updResource);
    }

    {
      Resource loaded = client.getResource(rid);
      System.out.println("RESOURCE: " + loaded);

      // test reloaded attrs
      List<Attribute> loadedAttrs = loaded.getAttribute();
      assertEquals(3, loadedAttrs.size());

      Map<String, String> attMap = new HashMap<String, String>();
      for (Attribute attribute : loadedAttrs) {
        attMap.put(attribute.getName(), attribute.getTextValue());
      }

      assertEquals("value1", attMap.get("string1"));
      assertEquals("value2.2", attMap.get("string2"));
      assertEquals("value4", attMap.get("string4"));
    }

    // try bad update
    {
      RESTResource res = new RESTResource();
      res.setCategory(new RESTCategory("TestCategory2"));
      try {
        client.updateResource(rid, res);
        fail("Undetected error");
      } catch (UniformInterfaceException e) {
        String response = "COULD NOT READ RESPONSE";
        try {
          response = IOUtils.toString(e.getResponse().getEntityInputStream());
        } catch (Exception e2) {
          LOGGER.warn("Error reading response: " + e2.getMessage());
        }
        LOGGER.info("Error condition successfully detected: " + response);
      } catch (Exception e) {
        LOGGER.info("Error condition successfully detected:" + e.getMessage(), e);
      }
    }

    client.deleteResource(rid);
  }
Example #4
0
  @Test
  // @Ignore
  public void testInsertResource() {

    final String KEY_STRING = "stringAtt";
    final String KEY_DATE = "dateAtt";

    final Date origDate = new Date();
    final String origString = "OrigStringValue";

    createDefaultCategory();

    RESTStoredData storedData = new RESTStoredData();
    storedData.setData("we wish you a merry xmas and a happy new year");

    List<ShortAttribute> attrList = new ArrayList<ShortAttribute>();
    attrList.add(new ShortAttribute(KEY_STRING, origString, DataType.STRING));
    attrList.add(ShortAttribute.createDateAttribute(KEY_DATE, origDate));

    String timeid = Long.toString(System.currentTimeMillis());

    RESTResource origResource = new RESTResource();
    origResource.setCategory(new RESTCategory(DEFAULTCATEGORYNAME));
    origResource.setName("rest_test_resource_" + timeid);
    origResource.setStore(storedData);
    origResource.setAttribute(attrList);

    Long rid = client.insert(origResource);
    System.out.println("RESOURCE has ID " + rid);

    // test getResource
    {
      Resource loaded = client.getResource(rid);
      System.out.println("RESOURCE: " + loaded);

      // test reloaded attrs
      List<Attribute> loadedAttrs = loaded.getAttribute();
      assertEquals(2, loadedAttrs.size());

      Attribute satt, datt;

      if (loadedAttrs.get(0).getType() == DataType.STRING) {
        satt = loadedAttrs.get(0);
        datt = loadedAttrs.get(1);
      } else {
        datt = loadedAttrs.get(0);
        satt = loadedAttrs.get(1);
      }

      assertEquals(DataType.STRING, satt.getType());
      assertEquals(KEY_STRING, satt.getName());
      assertEquals(origString, satt.getTextValue());

      assertEquals(DataType.DATE, datt.getType());
      assertEquals(KEY_DATE, datt.getName());
      assertEquals(origDate, datt.getDateValue());
    }
    // test Search
    SearchFilter searchFilter = new FieldFilter(BaseField.NAME, "%" + timeid, SearchOperator.LIKE);
    ShortResourceList rlist = client.searchResources(searchFilter);
    assertNotNull(rlist);
    assertEquals(1, rlist.getList().size());
    assertEquals(rid, (Long) rlist.getList().get(0).getId());
  }