PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/report/generator/PivotTableGeneratorTest.java

http://sigma-h.googlecode.com/
Java | 156 lines | 95 code | 44 blank | 17 comment | 1 complexity | 973f94fea442c60936ae85b973fe5796 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.report.generator;
  7
  8import org.easymock.EasyMock;
  9import org.junit.Assert;
 10import org.junit.Test;
 11import org.sigmah.server.dao.PivotDAO;
 12import org.sigmah.shared.dao.Filter;
 13import org.sigmah.shared.domain.User;
 14import org.sigmah.shared.report.content.DimensionCategory;
 15import org.sigmah.shared.report.content.EntityCategory;
 16import org.sigmah.shared.report.content.PivotTableData;
 17import org.sigmah.shared.report.model.AdminDimension;
 18import org.sigmah.shared.report.model.Dimension;
 19import org.sigmah.shared.report.model.DimensionType;
 20import org.sigmah.shared.report.model.PivotTableElement;
 21
 22import java.util.ArrayList;
 23import java.util.List;
 24import java.util.Set;
 25
 26import static org.easymock.EasyMock.*;
 27
 28/**
 29 * @author Alex Bertram (akbertram@gmail.com)
 30 */
 31public class PivotTableGeneratorTest {
 32
 33
 34    @Test
 35    public void test2x2() {
 36
 37        // test input data: user
 38
 39        User user = new User();
 40        user.setLocale("fr");
 41
 42
 43        // test input data: PivotTableElement
 44
 45        PivotTableElement element = new PivotTableElement();
 46        AdminDimension provinceDim = new AdminDimension(1);
 47        element.addRowDimension(provinceDim);
 48        Dimension partnerDim = new Dimension(DimensionType.Partner);
 49        element.addColDimension(partnerDim);
 50
 51        // test input data: aggregated results
 52        List<PivotDAO.Bucket> buckets = new ArrayList<PivotDAO.Bucket>();
 53
 54        buckets.add(newBucket(433, category(provinceDim, 2, "Sud Kivu"), category(partnerDim, 1, "IRC")));
 55        buckets.add(newBucket(1032, category(provinceDim, 1, "Nord Kivu"), category(partnerDim, 2, "Solidarites")));
 56        buckets.add(newBucket(310, category(provinceDim, 1, "Nord Kivu"), category(partnerDim, 1, "IRC")));
 57        buckets.add(newBucket(926, category(provinceDim, 1, "Nord Kivu"), category(partnerDim, 3, "AVSI")));
 58
 59        // collaborator : PivotDAO
 60
 61        PivotDAO dao = createMock(PivotDAO.class);
 62        expect(dao.aggregate(anyInt(), isA(Filter.class), EasyMock.<Set<Dimension>>anyObject())).andReturn(buckets);
 63        replay(dao);
 64
 65        // CLASS UNDER TEST!!
 66
 67        PivotTableGenerator generator = new PivotTableGenerator(dao);
 68
 69        generator.generate(user, element, null, null);
 70
 71        Assert.assertNotNull("element content", element.getContent());
 72
 73        PivotTableData data = element.getContent().getData();
 74        Assert.assertEquals("rows", 2, data.getRootRow().getChildCount());
 75        Assert.assertEquals("rows sorted", "Nord Kivu", data.getRootRow().getChildren().get(0).getLabel());
 76        Assert.assertEquals("cols", 3, data.getRootColumn().getChildCount());
 77    }
 78
 79    @Test
 80    public void testIndicatorSortOrder() {
 81
 82        // test input data: user
 83        User user = new User();
 84        user.setLocale("fr");
 85
 86        // test input data: PivotTableElement
 87        PivotTableElement element = new PivotTableElement();
 88        Dimension indicatorDim = new Dimension(DimensionType.Indicator);
 89        element.addRowDimension(indicatorDim);
 90
 91        // test input data: aggregated results
 92        List<PivotDAO.Bucket> buckets = new ArrayList<PivotDAO.Bucket>();
 93
 94        buckets.add(newBucket(300, category(indicatorDim, 1, "Nb. menages", 3)));
 95        buckets.add(newBucket(400, category(indicatorDim, 2, "Nb. personnes", 1)));
 96        buckets.add(newBucket(600, category(indicatorDim, 3, "Nb. deplaces", 2)));
 97
 98        // collaborator : PivotDAO
 99        PivotDAO dao = createMock(PivotDAO.class);
100        expect(dao.aggregate(anyInt(), isA(Filter.class), EasyMock.<Set<Dimension>>anyObject())).andReturn(buckets);
101        replay(dao);
102
103        // CLASS UNDER TEST!!
104
105        PivotTableGenerator generator = new PivotTableGenerator(dao);
106
107        generator.generate(user, element, null, null);
108
109        Assert.assertNotNull("element content", element.getContent());
110
111        List<PivotTableData.Axis> rows = element.getContent().getData().getRootRow().getChildren();
112        Assert.assertEquals(2, ((EntityCategory) rows.get(0).getCategory()).getId());
113        Assert.assertEquals(3, ((EntityCategory) rows.get(1).getCategory()).getId());
114        Assert.assertEquals(1, ((EntityCategory) rows.get(2).getCategory()).getId());
115    }
116
117
118
119    public class AndCategory {
120        private Dimension dimension;
121        private DimensionCategory category;
122
123        public AndCategory(Dimension dimension, DimensionCategory category) {
124            this.dimension = dimension;
125            this.category = category;
126        }
127
128        public DimensionCategory getCategory() {
129            return category;
130        }
131
132        public Dimension getDimension() {
133            return dimension;
134        }
135    }
136
137
138
139    public AndCategory category(Dimension dim, int id, String label) {
140        return new AndCategory(dim, new EntityCategory(id, label));
141    }
142
143    public AndCategory category(Dimension dim, int id, String label, int sortOrder) {
144        return new AndCategory(dim, new EntityCategory(id, label, sortOrder));
145    }
146
147    public PivotDAO.Bucket newBucket(double value, AndCategory... pairs) {
148        PivotDAO.Bucket bucket = new PivotDAO.Bucket();
149        bucket.setDoubleValue(value);
150        for (AndCategory pair : pairs) {
151            bucket.setCategory(pair.getDimension(), pair.getCategory());
152        }
153        return bucket;
154    }
155
156}