source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rss/PrintXmlFromRssRunner.java @ 4098

Last change on this file since 4098 was 4098, checked in by George.Georgovassilis@mpi.nl, 10 years ago

#360, #431, #432: JPA and unified component entities

File size: 5.8 KB
Line 
1package clarin.cmdi.componentregistry.rss;
2
3import clarin.cmdi.componentregistry.ComponentRegistry;
4import clarin.cmdi.componentregistry.ComponentRegistryException;
5import clarin.cmdi.componentregistry.ComponentRegistryFactory;
6import clarin.cmdi.componentregistry.DatesHelper;
7import clarin.cmdi.componentregistry.MDMarshaller;
8import clarin.cmdi.componentregistry.impl.ComponentUtils;
9import clarin.cmdi.componentregistry.model.BaseDescription;
10import clarin.cmdi.componentregistry.model.Comment;
11import clarin.cmdi.componentregistry.rest.RegistryTestHelper;
12
13import java.io.BufferedReader;
14import java.io.IOException;
15import java.io.InputStreamReader;
16import java.text.ParseException;
17import java.util.Collections;
18import java.util.Date;
19import java.util.List;
20
21import javax.xml.bind.JAXBException;
22import javax.xml.transform.TransformerException;
23
24import org.junit.BeforeClass;
25import org.springframework.beans.factory.annotation.Autowired;
26import org.springframework.context.ApplicationContext;
27import org.springframework.context.support.ClassPathXmlApplicationContext;
28
29/**
30 *
31 * non-automatised developer's test class Main method generates an Rss xml-file
32 * to validate it later using one of the on-line rss-source validators
33 */
34public class PrintXmlFromRssRunner {
35
36    private static MDMarshaller marshaller;
37
38    @Autowired
39    public void setMarshaller(MDMarshaller marshaller) {
40        this.marshaller = marshaller;
41    }
42
43    public static <T extends BaseDescription> void printIds(List<T> desc) {
44        for (T current : desc) {
45            String currentId = current.getId();
46            System.out.println(currentId);
47        }
48    }
49
50    private static void printXmlRssToFile(Rss rssObject) throws IOException, JAXBException {
51
52        String path = RegistryTestHelper.openTestDir("testRss");
53        String os = marshaller.marshalToString(rssObject);
54        RegistryTestHelper.writeStringToFile(os, path + "testRssResl.xml");
55
56    }
57
58    private static Rss makeRssForDescriptions(List<? extends BaseDescription> descriptions, int kind, String baseUri, int limit) throws ParseException {
59        System.out.println(descriptions.size());
60        Collections.sort(descriptions, ComponentUtils.COMPARE_ON_DATE);
61        System.out.println(descriptions.size());
62
63        System.out.println("check if the descriptions are sorted in a proper way, by the dates ");
64        for (BaseDescription desc : descriptions) {
65            Date date = desc.getRegistrationDate();
66            System.out.println(date + ", formatted: " + date
67                    + ", Rss=formatted: " + DatesHelper.getRFCDateTime(date));
68        }
69
70        RssCreatorDescriptions instance = new RssCreatorDescriptions(false, baseUri, (kind == 1) ? "profiles" : "components", limit, descriptions, ComponentUtils.COMPARE_ON_DATE);
71        Rss result = instance.getRss();
72
73        return result;
74
75    }
76
77    private static Rss makeRssForComments(List<Comment> comments, int kind, String baseUri, int limit, String profileId, String profileName) throws ParseException {
78        System.out.println(comments.size());
79        Collections.sort(comments, Comment.COMPARE_ON_DATE);
80        System.out.println(comments.size());
81
82        System.out.println("check if the comments are sorted in a proper way, by the dates ");
83        for (Comment commentje : comments) {
84            System.out.println(commentje.getCommentDate());
85        }
86
87        RssCreatorComments instance = new RssCreatorComments(false, baseUri, limit, profileId, profileName, (kind == 3) ? "profile" : "component", comments, Comment.COMPARE_ON_DATE);
88        Rss result = instance.getRss();
89        return result;
90    }
91
92    /*
93     * input: sort of rss -- profiles, or comopnents, or comments (see below the prompt string)
94     */
95    public static void main(String args[]) throws ComponentRegistryException, ParseException, IOException, JAXBException {
96
97        System.out.println("Type 1 or 2, or 3, or 4, \n "
98                + "to check Rss generaion for profiles, components  or comments for a profile or a component respectively: >> ");
99
100        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
101        String buffer = null;
102
103        //  read the username from the command-line; need to use try/catch with the
104        //  readLine() method
105        try {
106            buffer = br.readLine();
107        } catch (IOException ioe) {
108            System.out.println("IO error trying get the number");
109            System.exit(1);
110        }
111
112        int kind = Integer.parseInt(buffer);
113
114        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContextJDBC.xml");
115        ComponentRegistry registry = ((ComponentRegistryFactory) applicationContext.getBean("componentRegistryFactory")).getPublicRegistry();
116
117        Rss rss = null;
118        String baseUri = "http://localhost:8080/ComponentRegistry"; /* used only as a nice URi-example content for the "link"-field
119         in this test we do not click on link, we just generate an Rss xml-file to validate it
120         using one of the on-line rss-source vaidators */
121
122        if (kind == 1 || kind == 2) { // testing Rss for profiles/components
123            List<? extends BaseDescription> descriptions =
124                    (kind == 1) ? registry.getProfileDescriptions() : registry.getComponentDescriptions();
125            rss = makeRssForDescriptions(descriptions, kind, baseUri, 10);
126        };
127
128        if (kind == 3 || kind == 4) { // testing Rss comments
129            List<? extends BaseDescription> descriptions =
130                    (kind == 3) ? registry.getProfileDescriptions() : registry.getComponentDescriptions();
131            printIds(descriptions);
132            System.out.println("Pick up and input one of the description id above");// "clarin.eu:cr1:p_1284723009187" "clarin.eu:cr1:c_1288172614011"
133            try {
134                buffer = br.readLine();
135            } catch (IOException ioe) {
136                System.out.println("IO error trying to get the id");
137                System.exit(1);
138            }
139            final List<Comment> comments = (kind == 3) ? registry.getCommentsInProfile(buffer, null) : registry.getCommentsInComponent(buffer, null);
140            final String name = (kind == 3) ? registry.getProfileDescription(buffer).getName() : registry.getComponentDescription(buffer).getName();
141            rss = makeRssForComments(comments, kind, baseUri, 10, buffer, name);
142
143        }
144        printXmlRssToFile(rss);
145    }
146}
Note: See TracBrowser for help on using the repository browser.