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 com.sun.jersey.api.client.ClientResponse; |
---|
15 | import com.sun.jersey.multipart.FormDataMultiPart; |
---|
16 | import java.util.List; |
---|
17 | import javax.ws.rs.WebApplicationException; |
---|
18 | import javax.ws.rs.core.MediaType; |
---|
19 | import org.junit.Before; |
---|
20 | import org.junit.Test; |
---|
21 | import org.springframework.beans.factory.annotation.Autowired; |
---|
22 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
23 | |
---|
24 | import static org.junit.Assert.*; |
---|
25 | |
---|
26 | /** |
---|
27 | * |
---|
28 | * @author olhsha |
---|
29 | */ |
---|
30 | public class SanboxTest extends 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 | private ComponentDescription component1; |
---|
51 | private ComponentDescription component2; |
---|
52 | private ProfileDescription profile1; |
---|
53 | private ProfileDescription profile2; |
---|
54 | private ComponentDescription component3; |
---|
55 | private ProfileDescription profile3; |
---|
56 | private Comment profile1Comment1; |
---|
57 | private Comment profile1Comment2; |
---|
58 | private Comment component1Comment3; |
---|
59 | private Comment component1Comment4; |
---|
60 | private Comment profile3Comment5; |
---|
61 | private Comment component3Comment7; |
---|
62 | |
---|
63 | private void fillUpPublicItems() throws Exception { |
---|
64 | |
---|
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"); |
---|
83 | } |
---|
84 | |
---|
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"); |
---|
142 | } |
---|
143 | |
---|
144 | private FormDataMultiPart createFormData(Object content, String description) { |
---|
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; |
---|
154 | } |
---|
155 | } |
---|