1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import static org.junit.Assert.assertEquals; |
---|
4 | import static org.junit.Assert.assertTrue; |
---|
5 | import static org.junit.Assert.fail; |
---|
6 | |
---|
7 | import java.security.Principal; |
---|
8 | |
---|
9 | import org.junit.Before; |
---|
10 | import org.junit.Test; |
---|
11 | import org.springframework.beans.factory.annotation.Autowired; |
---|
12 | |
---|
13 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
14 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
15 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
16 | import clarin.cmdi.componentregistry.ComponentStatus; |
---|
17 | import clarin.cmdi.componentregistry.DeleteFailedException; |
---|
18 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
19 | import clarin.cmdi.componentregistry.frontend.CMDItemInfo; |
---|
20 | import clarin.cmdi.componentregistry.frontend.DisplayDataNode; |
---|
21 | import clarin.cmdi.componentregistry.frontend.SubmitFailedException; |
---|
22 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
23 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
24 | import clarin.cmdi.componentregistry.persistence.ComponentDao; |
---|
25 | import clarin.cmdi.componentregistry.rest.DummyPrincipal; |
---|
26 | import clarin.cmdi.componentregistry.rest.RegistryTestHelper; |
---|
27 | |
---|
28 | /** |
---|
29 | * |
---|
30 | * @author george.georgovassilis@mpi.nl |
---|
31 | * |
---|
32 | */ |
---|
33 | public class AdminRegistryTest extends BaseUnitTest{ |
---|
34 | |
---|
35 | @Autowired |
---|
36 | private ComponentDao componentDao; |
---|
37 | @Autowired |
---|
38 | private ComponentRegistryFactory componentRegistryFactory; |
---|
39 | private static final Principal PRINCIPAL_ADMIN = DummyPrincipal.DUMMY_ADMIN_PRINCIPAL; |
---|
40 | @Autowired |
---|
41 | private MDMarshaller marshaller; |
---|
42 | |
---|
43 | @Before |
---|
44 | public void init() { |
---|
45 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
46 | } |
---|
47 | |
---|
48 | @Test |
---|
49 | public void testForceUpdate() throws Exception { |
---|
50 | ComponentRegistry testRegistry = componentRegistryFactory.getPublicRegistry(); |
---|
51 | String content1 = ""; |
---|
52 | content1 += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
53 | content1 += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
54 | content1 += " <Header/>\n"; |
---|
55 | content1 += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
56 | content1 += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
57 | content1 += " </CMD_Component>\n"; |
---|
58 | content1 += "</CMD_ComponentSpec>\n"; |
---|
59 | ComponentDescription compDesc1 = RegistryTestHelper.addComponent(testRegistry, "XXX1", content1); |
---|
60 | |
---|
61 | String content2 = ""; |
---|
62 | content2 += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
63 | content2 += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
64 | content2 += " <Header/>\n"; |
---|
65 | content2 += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
66 | content2 += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
67 | content2 += " </CMD_Component>\n"; |
---|
68 | content2 += " </CMD_Component>\n"; |
---|
69 | content2 += "</CMD_ComponentSpec>\n"; |
---|
70 | ProfileDescription profileDesc = RegistryTestHelper.addProfile(testRegistry, "YYY1", content2); |
---|
71 | |
---|
72 | AdminRegistry adminReg = new AdminRegistry(); |
---|
73 | adminReg.setComponentRegistryFactory(componentRegistryFactory); |
---|
74 | adminReg.setComponentDao(componentDao); |
---|
75 | adminReg.setMarshaller(marshaller); |
---|
76 | CMDItemInfo fileInfo = new CMDItemInfo(marshaller); |
---|
77 | fileInfo.setForceUpdate(false); |
---|
78 | fileInfo.setDataNode(new DisplayDataNode(compDesc1.getName(), false, compDesc1, ComponentStatus.PUBLISHED)); |
---|
79 | fileInfo.setContent(content1); |
---|
80 | try { |
---|
81 | adminReg.submitFile(fileInfo, PRINCIPAL_ADMIN); |
---|
82 | fail(); |
---|
83 | } catch (SubmitFailedException e) { |
---|
84 | } |
---|
85 | fileInfo.setForceUpdate(true); |
---|
86 | adminReg.submitFile(fileInfo, PRINCIPAL_ADMIN); //Component needs to be forced because they can be used by other profiles/components |
---|
87 | |
---|
88 | assertEquals(1, testRegistry.getComponentDescriptions().size()); |
---|
89 | try { |
---|
90 | fileInfo.setForceUpdate(false); |
---|
91 | adminReg.delete(fileInfo, PRINCIPAL_ADMIN); |
---|
92 | fail(); |
---|
93 | } catch (SubmitFailedException e) { |
---|
94 | assertTrue(e.getCause() instanceof DeleteFailedException); |
---|
95 | } |
---|
96 | assertEquals(1, testRegistry.getComponentDescriptions().size()); |
---|
97 | fileInfo.setForceUpdate(true); |
---|
98 | adminReg.delete(fileInfo, PRINCIPAL_ADMIN); |
---|
99 | assertEquals(0, testRegistry.getComponentDescriptions().size()); |
---|
100 | |
---|
101 | assertEquals(1, testRegistry.getProfileDescriptions().size()); |
---|
102 | fileInfo.setForceUpdate(false); |
---|
103 | fileInfo.setDataNode(new DisplayDataNode(profileDesc.getName(), false, profileDesc, ComponentStatus.PUBLISHED)); |
---|
104 | adminReg.delete(fileInfo, PRINCIPAL_ADMIN); //Profile do not need to be forced they cannot be used by other profiles |
---|
105 | assertEquals(0, testRegistry.getProfileDescriptions().size()); |
---|
106 | } |
---|
107 | } |
---|