/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2017 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.db;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import javax.inject.Inject;

import org.junit.Test;

import com.axelor.AbstractTest;
import com.axelor.db.mapper.Mapper;
import com.axelor.db.mapper.Property;
import com.axelor.db.mapper.PropertyType;
import com.axelor.test.db.EnumCheck;
import com.axelor.test.db.EnumStatus;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class EnumTest extends AbstractTest {

	@Inject
	private ObjectMapper objectMapper;
	
	private Mapper beanMapper = Mapper.of(EnumCheck.class);

	@Test
	public void testMapper() {
		Property status = beanMapper.getProperty("status");
		assertNotNull(status);
		assertEquals(PropertyType.ENUM, status.getType());
		assertEquals(EnumStatus.class, status.getEnumType());
		assertNotNull(status.getEnumItems());
		assertEquals(3, status.getEnumItems().size());
		
		// make sure CLOSED item has custom title
		assertEquals("Close", status.getEnumItems().get(2).get("title"));
		
		EnumCheck entity = new EnumCheck();
		assertNull(entity.getStatus());
		
		status.set(entity, "OPEN");
		assertEquals(EnumStatus.OPEN, entity.getStatus());
		
		status.set(entity, "CLOSED");
		assertEquals(EnumStatus.CLOSED, entity.getStatus());
		
		try {
			status.set(entity, "OPENED");
			fail();
		} catch (IllegalArgumentException e) {
		}
	}
	
	@Test
	public void testJson() throws JsonProcessingException {
		EnumCheck entity = new EnumCheck();
		String json = objectMapper.writeValueAsString(entity);
		assertTrue(json.contains("\"status\":null"));
		entity.setStatus(EnumStatus.OPEN);
		json = objectMapper.writeValueAsString(entity);
		assertTrue(json.contains("\"status\":\"OPEN\""));
	}
}

@@ -936,16 +936,40 @@
 .fail;%0A%0A
+import java.util.List;%0A%0A
 import j
@@ -1161,16 +1161,97 @@
 tyType;%0A
+import com.axelor.meta.MetaStore;%0Aimport com.axelor.meta.schema.views.Selection;%0A
 import c
@@ -1809,43 +1809,128 @@
 ;%0A%09%09
-assertNotNull(status.getEnumItems()
+%0A%09%09List%3CSelection.Option%3E selectionList = MetaStore.getSelectionList(EnumStatus.class);%0A%09%09%0A%09%09assertNotNull(selectionList
 );%0A%09
@@ -1947,36 +1947,28 @@
 als(3, s
-tatus.getEnumItems()
+electionList
 .size())
@@ -1969,18 +1969,16 @@
 ize());%0A
-%09%09
 %0A%09%09// ma
@@ -2043,28 +2043,20 @@
 %22, s
-tatus.getEnumItems()
+electionList
 .get
@@ -2066,16 +2066,14 @@
 .get
-(%22t
+T
 itle
-%22
+(
 ));%0A

Fix enum field unit tests