package mil.dds.anet.test.resources;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import mil.dds.anet.beans.Tag;
import mil.dds.anet.beans.lists.AnetBeanList;

public class TagResourceTest extends AbstractResourceTest {

	@Rule
	public ExpectedException thrown = ExpectedException.none();

	@Test
	public void tagCreateTest() throws UnsupportedEncodingException {
		final Tag t = new Tag();
		t.setName("name");
		t.setDescription("desc");

		// Create
		final Tag created = httpQuery("/api/tags/new", admin).post(Entity.json(t), Tag.class);
		assertThat(created.getName()).isEqualTo(t.getName());
		assertThat(created.getDescription()).isEqualTo(t.getDescription());
		assertThat(created.getCreatedAt()).isNotNull();
		assertThat(created).isNotEqualTo(t);

		// Update
		created.setName("eman");
		final Response resp = httpQuery("/api/tags/update", admin).post(Entity.json(created));
		assertThat(resp.getStatus()).isEqualTo(200);

		// Get
		final Tag returned = httpQuery(String.format("/api/tags/%d", created.getId()), admin).get(Tag.class);
		assertThat(returned).isEqualTo(created);
	}

	@Test
	public void tagExceptionTest() throws UnsupportedEncodingException {
		// Get with unknown id
		thrown.expect(NotFoundException.class);
		httpQuery(String.format("/api/tags/-1"), admin).get(Tag.class);

		// Create with empty name
		thrown.expect(BadRequestException.class);
		httpQuery("/api/tags/new", admin).post(Entity.json(new Tag()), Tag.class);
	}

	@Test
	public void tagListTest() throws UnsupportedEncodingException {
		// All
		final AnetBeanList<Tag> tagList = httpQuery("/api/tags/", admin).get(new GenericType<AnetBeanList<Tag>>(){});
		assertThat(tagList).isNotNull();
	}

	@Test
	public void tagSearchTest() throws UnsupportedEncodingException {
		// Search for a tag from the initial data
		final AnetBeanList<Tag> tagList  = httpQuery(String.format("/api/tags/search?text=%s",
				URLEncoder.encode("bribery", "UTF-8")), admin)
				.get(new GenericType<AnetBeanList<Tag>>(){});
		assertThat(tagList).isNotNull();
		final List<Tag> tags = tagList.getList();
		assertThat(tags).isNotNull();
		assertThat(tags.size()).isGreaterThan(0);
	}

}

package mil.dds.anet.test.resources;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.UnsupportedEncodingException;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.GenericType;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import mil.dds.anet.beans.Tag;
import mil.dds.anet.beans.lists.AnetBeanList;
import mil.dds.anet.beans.search.TagSearchQuery;

public class TagResourceTest extends AbstractResourceTest {

	@Rule
	public ExpectedException thrown = ExpectedException.none();

	private final GraphQLHelper<Tag, TagSearchQuery> graphQLHelper;

	public TagResourceTest() {
		super();
		graphQLHelper = new GraphQLHelper<>(client, RULE.getLocalPort());
	}

	@Test
	public void tagCreateTest() throws UnsupportedEncodingException {
		final Tag t = new Tag();
		t.setName("name");
		t.setDescription("desc");

		// Create
		final Integer tId = graphQLHelper.createObject(admin, "createTag", "tag", "TagInput", t, new GenericType<GraphQLResponse<Tag>>() {});
		assertThat(tId).isNotNull();
		final Tag created = graphQLHelper.getObjectById(admin, "tag", "id name description createdAt", tId, new GenericType<GraphQLResponse<Tag>>() {});
		assertThat(created.getName()).isEqualTo(t.getName());
		assertThat(created.getDescription()).isEqualTo(t.getDescription());
		assertThat(created.getCreatedAt()).isNotNull();
		assertThat(created).isNotEqualTo(t);

		// Update
		created.setName("eman");
		final Integer nrUpdated = graphQLHelper.updateObject(admin, "updateTag", "tag", "TagInput", created);
		assertThat(nrUpdated).isEqualTo(1);

		// Get
		final Tag updated = graphQLHelper.getObjectById(admin, "tag", "id name description createdAt", tId, new GenericType<GraphQLResponse<Tag>>() {});
		assertThat(updated).isEqualTo(created);
	}

	@Test
	public void tagExceptionTest() throws UnsupportedEncodingException {
		// Get with unknown id
		thrown.expect(NotFoundException.class);
		graphQLHelper.getObjectById(admin, "tag", "id name description createdAt", -1, new GenericType<GraphQLResponse<Tag>>() {});

		// Create with empty name
		thrown.expect(BadRequestException.class);
		graphQLHelper.createObject(admin, "createTag", "tag", "TagInput", new Tag(), new GenericType<GraphQLResponse<Tag>>() {});
	}

	@Test
	public void tagListTest() throws UnsupportedEncodingException {
		// All
		final AnetBeanList<Tag> tagList = graphQLHelper.getAllObjects(admin, "tags",
				"id name description createdAt", new GenericType<GraphQLResponse<AnetBeanList<Tag>>>() {});
		assertThat(tagList).isNotNull();
	}

	@Test
	public void tagSearchTest() throws UnsupportedEncodingException {
		// Search for a tag from the initial data
		final TagSearchQuery query = new TagSearchQuery();
		query.setText("bribery");
		final AnetBeanList<Tag> searchObjects = graphQLHelper.searchObjects(admin, "tagList", "query", "TagSearchQueryInput",
				"id name description createdAt", query, new GenericType<GraphQLResponse<AnetBeanList<Tag>>>() {});
		assertThat(searchObjects).isNotNull();
		assertThat(searchObjects.getList()).isNotNull();
		assertThat(searchObjects.getList().size()).isGreaterThan(0);
	}

}

Refactor Tag tests to call /graphql instead of the REST API