Changeset 5550 for ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/SanboxTest.java
- Timestamp:
- 08/12/14 14:59:10 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/SanboxTest.java
r5549 r5550 12 12 import clarin.cmdi.componentregistry.model.ComponentDescription; 13 13 import clarin.cmdi.componentregistry.model.ProfileDescription; 14 import clarin.cmdi.componentregistry.model.RegisterResponse;15 14 import com.sun.jersey.api.client.ClientResponse; 16 15 import com.sun.jersey.multipart.FormDataMultiPart; 16 import java.util.List; 17 import javax.ws.rs.WebApplicationException; 17 18 import javax.ws.rs.core.MediaType; 18 19 import org.junit.Before; … … 21 22 import org.springframework.jdbc.core.JdbcTemplate; 22 23 23 import org.springframework.util.Assert;24 24 import static org.junit.Assert.*; 25 25 26 /** 26 27 * 27 28 * @author olhsha 28 29 */ 29 public class SanboxTest extends 30 ComponentRegistryRestServiceTestCase { 30 public class SanboxTest extends ComponentRegistryRestServiceTestCase { 31 31 32 32 @Autowired … … 40 40 @Before 41 41 public void init() { 42 43 44 42 ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); 43 createUserRecord(); 44 baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); 45 45 } 46 46 47 47 private String expectedUserId(String principal) { 48 48 return getUserDao().getByPrincipalName(principal).getId().toString(); 49 49 } 50 51 50 private ComponentDescription component1; 52 51 private ComponentDescription component2; 53 52 private ProfileDescription profile1; 54 53 private ProfileDescription profile2; 55 56 54 private ComponentDescription component3; 57 55 private ProfileDescription profile3; 58 59 56 private Comment profile1Comment1; 60 57 private Comment profile1Comment2; 61 58 private Comment component1Comment3; 62 59 private Comment component1Comment4; 63 64 60 private Comment profile3Comment5; 65 61 private Comment component3Comment7; 66 62 67 63 private void fillUpPublicItems() throws Exception { 68 69 profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);70 profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);71 component1 = RegistryTestHelper.addComponent(baseRegistry,72 "component2", true);73 component2 = RegistryTestHelper.addComponent(baseRegistry,74 "component1", true);75 profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",76 ProfileDescription.PROFILE_PREFIX + "profile1",77 "JUnit@test.com");78 profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",79 ProfileDescription.PROFILE_PREFIX + "profile1",80 "JUnit@test.com");81 component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",82 ComponentDescription.COMPONENT_PREFIX + "component1",83 "JUnit@test.com");84 component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",85 ComponentDescription.COMPONENT_PREFIX + "component1",86 "JUnit@test.com");87 }88 89 private void fillUpPrivateItems() throws Exception {90 profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);91 component3 = RegistryTestHelper.addComponent(baseRegistry,92 "component3", false);93 profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",94 ProfileDescription.PROFILE_PREFIX + "profile3",95 "JUnit@test.com");96 component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",97 ComponentDescription.COMPONENT_PREFIX + "component3",98 "JUnit@test.com");99 }100 101 @Test102 public void testCreatePrivateComponentWithRecursion() throws Exception {103 104 fillUpPrivateItems();105 // Create new componet106 FormDataMultiPart form = createFormData(RegistryTestHelper107 .getComponentTestContent());108 ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components")).type(109 MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);110 assertEquals(ClientResponse.Status.OK.getStatusCode(),111 cResponse.getStatus());112 RegisterResponse response = cResponse.getEntity(RegisterResponse.class);113 assertTrue(response.isRegistered());114 ComponentDescription desc = (ComponentDescription) response115 .getDescription();116 64 117 // Re-define with self-recursion 118 String compContent = ""; 119 compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 120 compContent += "\n"; 121 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 122 compContent += " xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n"; 123 compContent += " \n"; 124 compContent += " <Header/>\n"; 125 compContent += " \n"; 126 compContent += " <CMD_Component name=\"Nested\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; 127 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 128 compContent += " <CMD_Component ComponentId=\"" 129 + desc.getId() 130 + "\" name=\"Recursive\" CardinalityMin=\"1\" CardinalityMax=\"1\" />\n"; 131 compContent += " </CMD_Component>\n"; 132 compContent += "\n"; 133 compContent += "</CMD_ComponentSpec>\n"; 134 135 // Update component 136 form = createFormData( 137 RegistryTestHelper.getComponentContentAsStream(compContent), 138 "UPDATE DESCRIPTION!"); 139 cResponse = getAuthenticatedResource(getResource().path( 140 "/registry/components/" + desc.getId() + "/update")).type( 141 MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); 142 assertEquals(ClientResponse.Status.OK.getStatusCode(), 143 cResponse.getStatus()); 144 response = cResponse.getEntity(RegisterResponse.class); 145 assertFalse("Recursive definition should fail", response.isRegistered()); 146 assertEquals("There should be an error message for the recursion", 1, 147 response.getErrors().size()); 148 assertTrue( 149 "There error message should specify the point of recursion", 150 response.getErrors().get(0) 151 .contains("already contains " + desc.getId())); 152 65 profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true); 66 profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true); 67 component1 = RegistryTestHelper.addComponent(baseRegistry, 68 "component2", true); 69 component2 = RegistryTestHelper.addComponent(baseRegistry, 70 "component1", true); 71 profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2", 72 ProfileDescription.PROFILE_PREFIX + "profile1", 73 "JUnit@test.com"); 74 profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1", 75 ProfileDescription.PROFILE_PREFIX + "profile1", 76 "JUnit@test.com"); 77 component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3", 78 ComponentDescription.COMPONENT_PREFIX + "component1", 79 "JUnit@test.com"); 80 component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4", 81 ComponentDescription.COMPONENT_PREFIX + "component1", 82 "JUnit@test.com"); 153 83 } 154 84 155 private FormDataMultiPart createFormData(Object content) { 156 return createFormData(content, "My Test"); 85 private void fillUpPrivateItems() throws Exception { 86 profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false); 87 component3 = RegistryTestHelper.addComponent(baseRegistry, 88 "component3", false); 89 profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5", 90 ProfileDescription.PROFILE_PREFIX + "profile3", 91 "JUnit@test.com"); 92 component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7", 93 ComponentDescription.COMPONENT_PREFIX + "component3", 94 "JUnit@test.com"); 95 } 96 97 @Test 98 public void testDeleteCommentFromPublicProfile() throws Exception { 99 100 System.out.println("testDeleteCommentFromPublicProfile"); 101 102 fillUpPublicItems(); 103 104 List<Comment> comments = this.getAuthenticatedResource(getResource().path( 105 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 106 + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE); 107 assertEquals(2, comments.size()); 108 Comment aComment = this.getAuthenticatedResource(getResource().path( 109 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 110 + "profile1/comments/" + profile1Comment1.getId())).get(Comment.class); 111 assertNotNull(aComment); 112 113 // Try to delete from other profile 114 ClientResponse response = getAuthenticatedResource( 115 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 116 + "profile2/comments/9999").delete(ClientResponse.class); 117 assertEquals(500, response.getStatus()); 118 // Delete from correct profile 119 response = getAuthenticatedResource( 120 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 121 + "profile1/comments/" + profile1Comment1.getId()).delete(ClientResponse.class); 122 assertEquals(200, response.getStatus()); 123 124 comments = this.getAuthenticatedResource(getResource().path( 125 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 126 + "profile1/comments/")).get(COMMENT_LIST_GENERICTYPE); 127 assertEquals(1, comments.size()); 128 129 response = getAuthenticatedResource( 130 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 131 + "profile1/comments/" + profile1Comment2.getId()).delete(ClientResponse.class); 132 assertEquals(200, response.getStatus()); 133 134 comments = this.getAuthenticatedResource(getResource().path( 135 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 136 + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE); 137 assertEquals(0, comments.size()); 138 } 139 140 private FormDataMultiPart createFormData(Object content) { 141 return createFormData(content, "My Test"); 157 142 } 158 143 159 144 private FormDataMultiPart createFormData(Object content, String description) { 160 161 162 163 164 165 166 167 168 145 FormDataMultiPart form = new FormDataMultiPart(); 146 form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, 147 MediaType.APPLICATION_OCTET_STREAM_TYPE); 148 form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); 149 form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, 150 description); 151 form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); 152 form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); 153 return form; 169 154 } 170 155 }
Note: See TracChangeset
for help on using the changeset viewer.