Changeset 3449 for ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ConcurrentRestServiceTest.java
- Timestamp:
- 08/20/13 15:32:37 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ConcurrentRestServiceTest.java
r1861 r3449 17 17 import org.junit.Before; 18 18 import org.junit.Test; 19 import org.junit.runner.RunWith;20 19 import org.slf4j.Logger; 21 20 import org.slf4j.LoggerFactory; 22 21 import org.springframework.beans.factory.annotation.Autowired; 23 22 import org.springframework.jdbc.core.JdbcTemplate; 24 import org.springframework.test.context.ContextConfiguration;25 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;26 23 27 24 import static clarin.cmdi.componentregistry.rest.ComponentRegistryRestService.USERSPACE_PARAM; 28 25 import static org.junit.Assert.*; 29 26 30 @RunWith(SpringJUnit4ClassRunner.class) 31 @ContextConfiguration(locations = {"/applicationContext.xml"}) 32 public class ConcurrentRestServiceTest extends ComponentRegistryRestServiceTestCase { 33 34 private final static Logger LOG = LoggerFactory.getLogger(ConcurrentRestServiceTest.class); 27 /** 28 * 29 * @author george.georgovassilis@mpi.nl 30 * 31 */ 32 public class ConcurrentRestServiceTest extends 33 ComponentRegistryRestServiceTestCase { 34 35 private final static Logger LOG = LoggerFactory 36 .getLogger(ConcurrentRestServiceTest.class); 35 37 private int NR_OF_PROFILES = 20; 36 38 private int NR_OF_COMPONENTS = 20; … … 44 46 } 45 47 46 @Override47 protected String getApplicationContextFile() {48 return "classpath:applicationContext.xml";49 }50 51 48 @Test 52 49 public void testConcurrentRegisterProfile() throws Exception { 53 List<String> errors = new ArrayList ();50 List<String> errors = new ArrayList<String>(); 54 51 List<Thread> ts = new ArrayList<Thread>(); 55 52 … … 73 70 private void assertProfiles(int nrOfProfiles, String userSpace) { 74 71 List<ProfileDescription> response = getAuthenticatedResource( 75 getResource().path("/registry/profiles").queryParam( USERSPACE_PARAM, userSpace)).76 accept(MediaType.APPLICATION_XML).get(77 PROFILE_LIST_GENERICTYPE);72 getResource().path("/registry/profiles").queryParam( 73 USERSPACE_PARAM, userSpace)).accept( 74 MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE); 78 75 Collections.sort(response, descriptionComparator); 79 assertEquals("half should be deleted", nrOfProfiles / 2, response.size()); 76 assertEquals("half should be deleted", nrOfProfiles / 2, 77 response.size()); 80 78 for (int i = 0; i < nrOfProfiles / 2; i++) { 81 79 ProfileDescription desc = response.get(i); 82 80 assertEquals("Test Profile" + (i * 2 + 1000), desc.getName()); 83 assertEquals("Test Profile" + (i * 2 + 1000) + " Description", desc.getDescription()); 84 } 85 } 81 assertEquals("Test Profile" + (i * 2 + 1000) + " Description", 82 desc.getDescription()); 83 } 84 } 85 86 86 private Comparator<AbstractDescription> descriptionComparator = new Comparator<AbstractDescription>() { 87 87 … … 94 94 private void assertComponents(int nrOfComponents, String userSpace) { 95 95 List<ComponentDescription> cResponse = getAuthenticatedResource( 96 getResource().path("/registry/components").queryParam( USERSPACE_PARAM, userSpace)).97 accept(MediaType.APPLICATION_XML).get(98 COMPONENT_LIST_GENERICTYPE);96 getResource().path("/registry/components").queryParam( 97 USERSPACE_PARAM, userSpace)).accept( 98 MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE); 99 99 Collections.sort(cResponse, descriptionComparator); 100 assertEquals("half should be deleted", nrOfComponents / 2, cResponse.size()); 100 assertEquals("half should be deleted", nrOfComponents / 2, 101 cResponse.size()); 101 102 for (int i = 0; i < nrOfComponents / 2; i++) { 102 103 ComponentDescription desc = cResponse.get(i); 103 104 assertEquals("Test Component" + (i * 2 + 1000), desc.getName()); 104 assertEquals("Test Component" + (i * 2 + 1000) + " Description", desc.getDescription()); 105 assertEquals("Test Component" + (i * 2 + 1000) + " Description", 106 desc.getDescription()); 105 107 } 106 108 } … … 112 114 } 113 115 for (Thread thread : ts) { 114 thread.join(); //Wait till all are finished 115 } 116 } 117 118 private void registerProfiles(List<Thread> ts, int size, final List<String> errors, String userSpace) throws InterruptedException { 116 thread.join(); // Wait till all are finished 117 } 118 } 119 120 private void registerProfiles(List<Thread> ts, int size, 121 final List<String> errors, String userSpace) 122 throws InterruptedException { 119 123 for (int i = 0; i < size; i++) { 120 124 final boolean shouldDelete = (i % 2) == 1; 121 LOG.debug("Profile {} should be registered in {} and {}", new Object[]{i + 1000, Boolean.parseBoolean(userSpace) ? "user space" : "public space", shouldDelete ? "ALSO DELETED" : "not deleted"}); 122 Thread thread = createThread("/registry/profiles/", userSpace, "Test Profile" + (i + 1000), shouldDelete, RegistryTestHelper.getTestProfileContent(), errors); 125 LOG.debug("Profile {} should be registered in {} and {}", 126 new Object[] { 127 i + 1000, 128 Boolean.parseBoolean(userSpace) ? "user space" 129 : "public space", 130 shouldDelete ? "ALSO DELETED" : "not deleted" }); 131 Thread thread = createThread("/registry/profiles/", userSpace, 132 "Test Profile" + (i + 1000), shouldDelete, 133 RegistryTestHelper.getTestProfileContent(), errors); 123 134 ts.add(thread); 124 135 } 125 136 } 126 137 127 private void registerComponents(List<Thread> ts, int size, final List<String> errors, String userSpace) throws InterruptedException { 138 private void registerComponents(List<Thread> ts, int size, 139 final List<String> errors, String userSpace) 140 throws InterruptedException { 128 141 for (int i = 0; i < size; i++) { 129 142 final boolean shouldDelete = (i % 2) == 1; 130 LOG.debug("Component {} should be registered in {} and {}", new Object[]{i + 1000, Boolean.parseBoolean(userSpace) ? "user space" : "public space", shouldDelete ? "ALSO DELETED" : "not deleted"}); 131 Thread thread = createThread("/registry/components/", userSpace, "Test Component" + (i + 1000), shouldDelete, 143 LOG.debug("Component {} should be registered in {} and {}", 144 new Object[] { 145 i + 1000, 146 Boolean.parseBoolean(userSpace) ? "user space" 147 : "public space", 148 shouldDelete ? "ALSO DELETED" : "not deleted" }); 149 Thread thread = createThread("/registry/components/", userSpace, 150 "Test Component" + (i + 1000), shouldDelete, 132 151 RegistryTestHelper.getComponentTestContent(), errors); 133 152 ts.add(thread); … … 135 154 } 136 155 137 private Thread createThread(final String path, final String userSpace, final String name, final boolean alsoDelete, 138 InputStream content, final List<String> errors) throws InterruptedException { 156 private Thread createThread(final String path, final String userSpace, 157 final String name, final boolean alsoDelete, InputStream content, 158 final List<String> errors) throws InterruptedException { 139 159 final FormDataMultiPart form = new FormDataMultiPart(); 140 form.field(ComponentRegistryRestService.DATA_FORM_FIELD, content, MediaType.APPLICATION_OCTET_STREAM_TYPE); 141 form.field(ComponentRegistryRestService.NAME_FORM_FIELD, name); 142 form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, name + " Description"); 160 form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, 161 MediaType.APPLICATION_OCTET_STREAM_TYPE); 162 form.field(IComponentRegistryRestService.NAME_FORM_FIELD, name); 163 form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, name 164 + " Description"); 143 165 Thread t = new Thread(new Runnable() { 144 166 145 167 @Override 146 168 public void run() { 147 // 169 // System.out.println("THREAD STARTED"+Thread.currentThread().getName()); 148 170 RegisterResponse registerResponse = getAuthenticatedResource( 149 getResource().path(path).queryParam(USERSPACE_PARAM, userSpace)).150 type(MediaType.MULTIPART_FORM_DATA).post(151 RegisterResponse.class, form);171 getResource().path(path).queryParam(USERSPACE_PARAM, 172 userSpace)).type(MediaType.MULTIPART_FORM_DATA) 173 .post(RegisterResponse.class, form); 152 174 if (!registerResponse.isRegistered()) { 153 errors.add("Failed to register " + Arrays.toString(registerResponse.getErrors().toArray())); 175 errors.add("Failed to register " 176 + Arrays.toString(registerResponse.getErrors() 177 .toArray())); 154 178 } 155 LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode() + "] REGISTERING DESCRIPTION " + name + " " + registerResponse.getDescription().getId() + (Boolean.valueOf(userSpace) ? " userspace" : "") + (alsoDelete ? " alsoDelete" : "")); 179 LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode() 180 + "] REGISTERING DESCRIPTION " + name + " " 181 + registerResponse.getDescription().getId() 182 + (Boolean.valueOf(userSpace) ? " userspace" : "") 183 + (alsoDelete ? " alsoDelete" : "")); 156 184 if (alsoDelete) { 157 LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode() + "] DELETING DESCRIPTION " + name + " " + registerResponse.getDescription().getId() + (Boolean.valueOf(userSpace) ? " userspace " : "") + (alsoDelete ? " alsoDelete" : "")); 185 LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode() 186 + "] DELETING DESCRIPTION " + name + " " 187 + registerResponse.getDescription().getId() 188 + (Boolean.valueOf(userSpace) ? " userspace " : "") 189 + (alsoDelete ? " alsoDelete" : "")); 158 190 ClientResponse response = getAuthenticatedResource( 159 getResource().path(path + registerResponse.getDescription().getId()).queryParam(USERSPACE_PARAM, userSpace)). 160 delete(ClientResponse.class); 191 getResource().path( 192 path 193 + registerResponse.getDescription() 194 .getId()).queryParam( 195 USERSPACE_PARAM, userSpace)).delete( 196 ClientResponse.class); 161 197 if (response.getStatus() != 200) { 162 errors.add("Failed to delete " + registerResponse.getDescription()); 198 errors.add("Failed to delete " 199 + registerResponse.getDescription()); 163 200 } 164 201 } 165 // 202 // System.out.println("THREAD FINISHED"+Thread.currentThread().getName()); 166 203 } 167 204 });
Note: See TracChangeset
for help on using the changeset viewer.