Search in sources :

Example 76 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class ModelRepositoryAttachmentTest method testAttachFileWithCustomTag.

@Test
public void testAttachFileWithCustomTag() {
    IUserContext erle = createUserContext("erle", "playground");
    importModel("Color.type", erle);
    try {
        Tag myCustomTag = new Tag("myCustom");
        repositoryFactory.getRepository(erle).attachFile(new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0"), new FileContent("backup1.xml", IOUtils.toByteArray(new ClassPathResource("sample_models/backup1.xml").getInputStream())), erle, myCustomTag);
        List<Attachment> attachments = repositoryFactory.getRepository(erle).getAttachments(new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0"));
        assertEquals(1, attachments.get(0).getTags().size());
        assertEquals(myCustomTag, attachments.get(0).getTags().get(0));
        attachments.forEach(System.out::println);
    } catch (IOException | FatalModelRepositoryException e) {
        e.printStackTrace();
        fail("Cannot load sample file");
    }
}
Also used : IOException(java.io.IOException) ModelId(org.eclipse.vorto.model.ModelId) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 77 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class ModelRepositoryAttachmentTest method testGetAttachmentsWithTagNotExisting.

@Test
public void testGetAttachmentsWithTagNotExisting() {
    IUserContext erle = createUserContext("erle", "playground");
    importModel("Color.type", erle);
    try {
        repositoryFactory.getRepository(erle).attachFile(new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0"), new FileContent("backup1.xml", IOUtils.toByteArray(new ClassPathResource("sample_models/backup1.xml").getInputStream())), erle, Attachment.TAG_DOCUMENTATION);
        List<Attachment> attachments = repositoryFactory.getRepository(erle).getAttachmentsByTag(new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0"), Attachment.TAG_IMAGE);
        assertEquals(0, attachments.size());
    } catch (IOException | FatalModelRepositoryException e) {
        e.printStackTrace();
        fail("Cannot load sample file");
    }
}
Also used : IOException(java.io.IOException) ModelId(org.eclipse.vorto.model.ModelId) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 78 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class ModelRepositoryAttachmentTest method testDeleteImportedAttachmentOtherAttachmentsNotExist.

@Test
public void testDeleteImportedAttachmentOtherAttachmentsNotExist() throws Exception {
    IUserContext erle = createUserContext("erle", "playground");
    importModel("Color.type", erle);
    ModelId modelId = new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0");
    repositoryFactory.getRepository(erle).attachFile(modelId, new FileContent("backup1.xml", IOUtils.toByteArray(new ClassPathResource("sample_models/backup1.xml").getInputStream())), erle, Attachment.TAG_IMPORTED);
    boolean deleteResult = repositoryFactory.getRepository(erle).deleteAttachment(modelId, "backup1.xml");
    assertFalse(deleteResult);
}
Also used : ModelId(org.eclipse.vorto.model.ModelId) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 79 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class ModelRepositoryAttachmentTest method testDeleteImportedAttachmentOtherAttachmentsExist.

@Test
public void testDeleteImportedAttachmentOtherAttachmentsExist() throws Exception {
    IUserContext erle = createUserContext("erle", "playground");
    importModel("Color.type", erle);
    ModelId modelId = new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0");
    repositoryFactory.getRepository(erle).attachFile(modelId, new FileContent("backup1.xml", IOUtils.toByteArray(new ClassPathResource("sample_models/backup1.xml").getInputStream())), erle, Attachment.TAG_IMPORTED);
    repositoryFactory.getRepository(erle).attachFile(modelId, new FileContent("backup-withImages.xml", IOUtils.toByteArray(new ClassPathResource("sample_models/backup-withImages.xml").getInputStream())), erle);
    boolean deleteResultTrue = repositoryFactory.getRepository(erle).deleteAttachment(modelId, "backup-withImages.xml");
    assertTrue(deleteResultTrue);
    boolean deleteResultFalse = repositoryFactory.getRepository(erle).deleteAttachment(modelId, "backup1.xml");
    assertFalse(deleteResultFalse);
}
Also used : ModelId(org.eclipse.vorto.model.ModelId) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 80 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class ModelRepositoryTest method testGetModelWithImage.

/**
 * Originally, this only tested that a given persisted model had an image once an image was
 * attached. <br/>
 * In light of changes to tags (with the introduction of {@link Attachment#TAG_DISPLAY_IMAGE}
 * which is unique among a model's images and signifies this is the image to display, the test
 * verifies a few more things:
 * <ol>
 *  <li>
 *    That the both the {@link Attachment#TAG_DISPLAY_IMAGE} and {@link Attachment#TAG_IMAGE} tags
 *    are present once attached (unfortunately this is rather trivial since the operation is
 *    directed at controller level).
 *  </li>
 *  <li>
 *    More importantly, that once another image is attached, only one
 *    {@link Attachment#TAG_DISPLAY_IMAGE} is found, and that it belongs to the latest resource.
 *  </li>
 * </ol>
 * @throws Exception
 */
@Test
public void testGetModelWithImage() throws Exception {
    IUserContext alex = UserContext.user("alex", "playground");
    final ModelId modelId = new ModelId("HueLightStrips", "com.mycompany", "1.0.0");
    importModel("Color.type");
    importModel("Colorlight.fbmodel");
    importModel("Switcher.fbmodel");
    importModel("HueLightStrips.infomodel");
    // this adds the two tags "image" and "display image" as the controller would do when
    // uploading an image attachment
    this.repositoryFactory.getRepository(createUserContext("admin")).attachFile(modelId, new FileContent("sample.png", IOUtils.toByteArray(new ClassPathResource("sample_models/sample.png").getInputStream())), alex, Attachment.TAG_IMAGE, Attachment.TAG_DISPLAY_IMAGE);
    assertEquals(true, this.repositoryFactory.getRepository(createUserContext("admin")).getById(modelId).isHasImage());
    assertEquals(1, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_DISPLAY_IMAGE).size());
    assertEquals(1, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_IMAGE).size());
    // adding "another" image - will test if only one has tag display image
    this.repositoryFactory.getRepository(createUserContext("admin")).attachFile(modelId, new FileContent("sample2.png", IOUtils.toByteArray(new ClassPathResource("sample_models/sample2.png").getInputStream())), alex, Attachment.TAG_IMAGE, Attachment.TAG_DISPLAY_IMAGE);
    // only one display image tag present among attachments
    assertEquals(1, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_DISPLAY_IMAGE).size());
    // two image tags among attachments
    assertEquals(2, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_IMAGE).size());
    // display image tag pertains to latest attachment added
    assertEquals(true, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_DISPLAY_IMAGE).get(0).getFilename().contains("sample2.png"));
    // adding a third non-image resource with custom tags
    this.repositoryFactory.getRepository(createUserContext("admin")).attachFile(modelId, new FileContent("backup1.xml", IOUtils.toByteArray(new ClassPathResource("sample_models/backup1.xml").getInputStream())), alex, new Tag("custom"));
    // only one display image tag present among attachments
    assertEquals(1, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_DISPLAY_IMAGE).size());
    // two image tags among attachments
    assertEquals(2, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_IMAGE).size());
    // display image tag pertains to latest attachment added
    assertEquals(true, this.repositoryFactory.getRepository(createUserContext("admin")).getAttachmentsByTag(modelId, Attachment.TAG_DISPLAY_IMAGE).get(0).getFilename().contains("sample2.png"));
}
Also used : ModelId(org.eclipse.vorto.model.ModelId) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

ModelId (org.eclipse.vorto.model.ModelId)124 Test (org.junit.Test)48 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)29 IOException (java.io.IOException)28 ClassPathResource (org.springframework.core.io.ClassPathResource)25 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)19 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ResponseEntity (org.springframework.http.ResponseEntity)14 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)13 ModelType (org.eclipse.vorto.model.ModelType)12 IUserContext (org.eclipse.vorto.repository.core.IUserContext)12 Autowired (org.springframework.beans.factory.annotation.Autowired)12 GetMapping (org.springframework.web.bind.annotation.GetMapping)11 Optional (java.util.Optional)10 List (java.util.List)9 IOUtils (org.apache.commons.io.IOUtils)9 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)9 ModelLink (org.eclipse.vorto.repository.web.api.v1.dto.ModelLink)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 ApiOperation (io.swagger.annotations.ApiOperation)8