1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package clarin.cmdi.componentregistry.rest; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
8 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
9 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory; |
---|
10 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; |
---|
11 | import clarin.cmdi.componentregistry.model.Comment; |
---|
12 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
13 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
14 | import clarin.cmdi.componentregistry.model.RegisterResponse; |
---|
15 | import com.sun.jersey.api.client.ClientResponse; |
---|
16 | import com.sun.jersey.multipart.FormDataMultiPart; |
---|
17 | import javax.ws.rs.core.MediaType; |
---|
18 | import org.junit.Before; |
---|
19 | import org.junit.Test; |
---|
20 | import org.springframework.beans.factory.annotation.Autowired; |
---|
21 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
22 | |
---|
23 | import org.springframework.util.Assert; |
---|
24 | import static org.junit.Assert.*; |
---|
25 | /** |
---|
26 | * |
---|
27 | * @author olhsha |
---|
28 | */ |
---|
29 | public class SanboxTest extends |
---|
30 | ComponentRegistryRestServiceTestCase { |
---|
31 | |
---|
32 | @Autowired |
---|
33 | private ComponentRegistryFactory componentRegistryFactory; |
---|
34 | @Autowired |
---|
35 | private ComponentRegistryBeanFactory componentRegistryBeanFactory; |
---|
36 | @Autowired |
---|
37 | private JdbcTemplate jdbcTemplate; |
---|
38 | private ComponentRegistry baseRegistry; |
---|
39 | |
---|
40 | @Before |
---|
41 | public void init() { |
---|
42 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
43 | createUserRecord(); |
---|
44 | baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
45 | } |
---|
46 | |
---|
47 | private String expectedUserId(String principal) { |
---|
48 | return getUserDao().getByPrincipalName(principal).getId().toString(); |
---|
49 | } |
---|
50 | |
---|
51 | private ComponentDescription component1; |
---|
52 | private ComponentDescription component2; |
---|
53 | private ProfileDescription profile1; |
---|
54 | private ProfileDescription profile2; |
---|
55 | |
---|
56 | private ComponentDescription component3; |
---|
57 | private ProfileDescription profile3; |
---|
58 | |
---|
59 | private Comment profile1Comment1; |
---|
60 | private Comment profile1Comment2; |
---|
61 | private Comment component1Comment3; |
---|
62 | private Comment component1Comment4; |
---|
63 | |
---|
64 | private Comment profile3Comment5; |
---|
65 | private Comment component3Comment7; |
---|
66 | |
---|
67 | 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 | @Test |
---|
102 | public void testCreatePrivateComponentWithRecursion() throws Exception { |
---|
103 | |
---|
104 | fillUpPrivateItems(); |
---|
105 | // Create new componet |
---|
106 | FormDataMultiPart form = createFormData(RegistryTestHelper |
---|
107 | .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) response |
---|
115 | .getDescription(); |
---|
116 | |
---|
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 | |
---|
153 | } |
---|
154 | |
---|
155 | private FormDataMultiPart createFormData(Object content) { |
---|
156 | return createFormData(content, "My Test"); |
---|
157 | } |
---|
158 | |
---|
159 | private FormDataMultiPart createFormData(Object content, String description) { |
---|
160 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
161 | form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, |
---|
162 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
163 | form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); |
---|
164 | form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, |
---|
165 | description); |
---|
166 | form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); |
---|
167 | form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); |
---|
168 | return form; |
---|
169 | } |
---|
170 | } |
---|