001/* 002 * Copyright 2020 Global Biodiversity Information Facility (GBIF) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.gbif.api.model.metrics.cube; 017 018import org.gbif.api.vocabulary.Country; 019import org.gbif.api.vocabulary.Kingdom; 020 021import java.util.UUID; 022 023import org.junit.jupiter.api.Test; 024 025import static org.junit.jupiter.api.Assertions.assertEquals; 026import static org.junit.jupiter.api.Assertions.assertNotNull; 027 028/** 029 * Tests the type safety of the builder, and illustrates usage. 030 */ 031public class ReadBuilderTest { 032 033 private static final class BooleanDimension extends Dimension<Boolean> { 034 035 public BooleanDimension() { 036 super("boolean", Boolean.class); 037 } 038 } 039 040 private static final class DoubleDimension extends Dimension<Double> { 041 042 public DoubleDimension() { 043 super("double", Double.class); 044 } 045 } 046 047 private static final class EnumDimension extends Dimension<Kingdom> { 048 049 public EnumDimension() { 050 super("enum", Kingdom.class); 051 } 052 } 053 054 private static final class FloatDimension extends Dimension<Float> { 055 056 public FloatDimension() { 057 super("float", Float.class); 058 } 059 } 060 061 private static final class IntDimension extends Dimension<Integer> { 062 063 public IntDimension() { 064 super("int", Integer.class); 065 } 066 } 067 068 private static final class StringDimension extends Dimension<String> { 069 070 public StringDimension() { 071 super("string", String.class); 072 } 073 } 074 075 private static final class UUIDDimension extends Dimension<UUID> { 076 077 public UUIDDimension() { 078 super("uuid", UUID.class); 079 } 080 } 081 082 private static final class CountryDimension extends Dimension<Country> { 083 084 public CountryDimension() { 085 super("country", Country.class); 086 } 087 } 088 089 @Test 090 public void testDimensions() { 091 ReadBuilder b = new ReadBuilder(); 092 UUID uuid = UUID.randomUUID(); 093 094 b.at(new StringDimension(), "1"); 095 assertNotNull(b.build()); 096 assertEquals(1, b.build().size()); 097 assertEquals("1", b.build().get(new StringDimension())); 098 099 b.at(new IntDimension(), 1); 100 assertNotNull(b.build()); 101 assertEquals(2, b.build().size()); 102 assertEquals("1", b.build().get(new IntDimension())); 103 104 b.at(new FloatDimension(), 1f); 105 assertNotNull(b.build()); 106 assertEquals(3, b.build().size()); 107 assertEquals("1.0", b.build().get(new FloatDimension())); 108 109 b.at(new DoubleDimension(), 1d); 110 assertNotNull(b.build()); 111 assertEquals(4, b.build().size()); 112 assertEquals("1.0", b.build().get(new DoubleDimension())); 113 114 b.at(new UUIDDimension(), uuid); 115 assertNotNull(b.build()); 116 assertEquals(5, b.build().size()); 117 assertEquals(uuid.toString(), b.build().get(new UUIDDimension())); 118 119 b.at(new EnumDimension(), Kingdom.ANIMALIA); 120 assertNotNull(b.build()); 121 assertEquals(6, b.build().size()); 122 assertEquals(Kingdom.ANIMALIA.toString(), b.build().get(new EnumDimension())); 123 124 b.at(new BooleanDimension(), true); 125 assertNotNull(b.build()); 126 assertEquals(7, b.build().size()); 127 assertEquals(String.valueOf(true), b.build().get(new BooleanDimension())); 128 129 b.at(new CountryDimension(), Country.GERMANY); 130 assertNotNull(b.build()); 131 assertEquals(8, b.build().size()); 132 assertEquals("DE", b.build().get(new CountryDimension())); 133 } 134}