1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import static org.junit.Assert.assertTrue; |
---|
4 | import static org.junit.Assert.fail; |
---|
5 | |
---|
6 | import java.security.Principal; |
---|
7 | |
---|
8 | import org.junit.Before; |
---|
9 | import org.junit.Test; |
---|
10 | import org.springframework.beans.factory.annotation.Autowired; |
---|
11 | |
---|
12 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
13 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
14 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
15 | import clarin.cmdi.componentregistry.DeleteFailedException; |
---|
16 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
17 | import clarin.cmdi.componentregistry.RegistrySpace; |
---|
18 | import clarin.cmdi.componentregistry.frontend.CMDItemInfo; |
---|
19 | import clarin.cmdi.componentregistry.frontend.DisplayDataNode; |
---|
20 | import clarin.cmdi.componentregistry.frontend.SubmitFailedException; |
---|
21 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
22 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
23 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
24 | import clarin.cmdi.componentregistry.persistence.ComponentDao; |
---|
25 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
26 | import clarin.cmdi.componentregistry.rest.DummyPrincipal; |
---|
27 | import clarin.cmdi.componentregistry.rest.RegistryTestHelper; |
---|
28 | |
---|
29 | /** |
---|
30 | * |
---|
31 | * @author george.georgovassilis@mpi.nl |
---|
32 | * |
---|
33 | */ |
---|
34 | public class AdminRegistryTest extends BaseUnitTest{ |
---|
35 | |
---|
36 | @Autowired |
---|
37 | private ComponentDao componentDao; |
---|
38 | |
---|
39 | @Autowired |
---|
40 | private UserDao userDao; |
---|
41 | |
---|
42 | @Autowired |
---|
43 | private ComponentRegistryFactory componentRegistryFactory; |
---|
44 | private static final Principal PRINCIPAL_ADMIN = DummyPrincipal.DUMMY_ADMIN_PRINCIPAL; |
---|
45 | @Autowired |
---|
46 | private MDMarshaller marshaller; |
---|
47 | |
---|
48 | @Before |
---|
49 | public void init() { |
---|
50 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
51 | } |
---|
52 | |
---|
53 | // TODO: two questions |
---|
54 | @Test |
---|
55 | public void testForceUpdate() throws Exception { |
---|
56 | |
---|
57 | RegistryUser adminUser = new RegistryUser(); |
---|
58 | adminUser.setName(PRINCIPAL_ADMIN.getName()); |
---|
59 | adminUser.setPrincipalName(PRINCIPAL_ADMIN.getName()); |
---|
60 | userDao.save(adminUser); |
---|
61 | |
---|
62 | ComponentRegistry testRegistry = componentRegistryFactory.getPublicRegistry(); |
---|
63 | String content1 = ""; |
---|
64 | content1 += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
65 | content1 += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
66 | content1 += " <Header/>\n"; |
---|
67 | content1 += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
68 | content1 += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
69 | content1 += " </CMD_Component>\n"; |
---|
70 | content1 += "</CMD_ComponentSpec>\n"; |
---|
71 | ComponentDescription compDesc1 = RegistryTestHelper.addComponent(testRegistry, "XXX1", content1, true); |
---|
72 | |
---|
73 | assertEquals(1, testRegistry.getComponentDescriptions().size()); |
---|
74 | |
---|
75 | String content2 = ""; |
---|
76 | content2 += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
77 | content2 += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
78 | content2 += " <Header/>\n"; |
---|
79 | content2 += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
80 | content2 += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
81 | content2 += " </CMD_Component>\n"; |
---|
82 | content2 += " </CMD_Component>\n"; |
---|
83 | content2 += "</CMD_ComponentSpec>\n"; |
---|
84 | ProfileDescription profileDesc = RegistryTestHelper.addProfile(testRegistry, "YYY1", content2, true); |
---|
85 | |
---|
86 | |
---|
87 | // why two registries? |
---|
88 | // if you are logged in as an admin then at any registry you have the same all-mighty rights? |
---|
89 | // testRegistry with the owner admin should be ok? |
---|
90 | // how are these two registries connected, via componentDao? |
---|
91 | |
---|
92 | AdminRegistry adminReg = new AdminRegistry(); |
---|
93 | adminReg.setComponentRegistryFactory(componentRegistryFactory); |
---|
94 | adminReg.setComponentDao(componentDao); |
---|
95 | adminReg.setMarshaller(marshaller); |
---|
96 | CMDItemInfo fileInfo = new CMDItemInfo(marshaller); |
---|
97 | fileInfo.setForceUpdate(false); |
---|
98 | fileInfo.setDataNode(new DisplayDataNode(compDesc1.getName(), false, compDesc1, RegistrySpace.PUBLISHED)); |
---|
99 | fileInfo.setContent(content1); |
---|
100 | // TODO: how it should be? |
---|
101 | try { |
---|
102 | adminReg.submitFile(fileInfo, PRINCIPAL_ADMIN); |
---|
103 | |
---|
104 | // Twan ?? when submit should fail? |
---|
105 | //fail(); |
---|
106 | } catch (SubmitFailedException e) { |
---|
107 | } |
---|
108 | fileInfo.setForceUpdate(true); |
---|
109 | adminReg.submitFile(fileInfo, PRINCIPAL_ADMIN); //Component needs to be forced because they can be used by other profiles/components |
---|
110 | |
---|
111 | assertEquals(1, testRegistry.getComponentDescriptions().size()); |
---|
112 | |
---|
113 | try { |
---|
114 | fileInfo.setForceUpdate(false); |
---|
115 | adminReg.delete(fileInfo, PRINCIPAL_ADMIN); |
---|
116 | } catch (SubmitFailedException e) { |
---|
117 | assertTrue(e.getCause() instanceof DeleteFailedException); |
---|
118 | } |
---|
119 | |
---|
120 | assertEquals(1, testRegistry.getComponentDescriptions().size()); |
---|
121 | fileInfo.setForceUpdate(true); |
---|
122 | adminReg.delete(fileInfo, PRINCIPAL_ADMIN); |
---|
123 | assertEquals(0, testRegistry.getComponentDescriptions().size()); |
---|
124 | |
---|
125 | assertEquals(1, testRegistry.getProfileDescriptions().size()); |
---|
126 | fileInfo.setForceUpdate(false); |
---|
127 | fileInfo.setDataNode(new DisplayDataNode(profileDesc.getName(), false, profileDesc, RegistrySpace.PUBLISHED)); |
---|
128 | adminReg.delete(fileInfo, PRINCIPAL_ADMIN); //Profile do not need to be forced they cannot be used by other profiles |
---|
129 | assertEquals(0, testRegistry.getProfileDescriptions().size()); |
---|
130 | } |
---|
131 | } |
---|