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