Segue as classes.
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import br.com.alura.loja.modelo.Projeto;
public class ProjetoDao {
private static Map<Long, Projeto> banco = new HashMap<Long, Projeto>();
private static AtomicLong contador = new AtomicLong(1);
static {
banco.put(1L, new Projeto(1L, "Minha loja", 2014));
banco.put(2L, new Projeto(2L, "Alura", 2012));
}
public void adiciona(Projeto projeto) {
long codigo = contador.incrementAndGet();
projeto.setCodigo(codigo);
banco.put(codigo, projeto);
}
public Projeto remover(Long codigo) {
return banco.remove(codigo);
}
public Projeto busca(Long codigo) {
return banco.get(codigo);
}
}
package br.com.alura.loja.modelo;
import com.thoughtworks.xstream.XStream;
public class Projeto {
private Long codigo;
private String nome;
private Integer anoDeInicio;
public Projeto() {
}
public Projeto(Long codigo, String nome, Integer ano) {
this.codigo = codigo;
this.nome = nome;
this.anoDeInicio = ano;
}
public void setCodigo(Long codigo) {
this.codigo = codigo;
}
public Long getCodigo() {
return codigo;
}
public String getNome() {
return nome;
}
public Integer getAnoDeInicio() {
return anoDeInicio;
}
public String toXML() {
XStream xs = new XStream();
return xs.toXML(this);
}
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import br.com.alura.loja.dao.ProjetoDao;
import br.com.alura.loja.modelo.Projeto;
@Path("projeto")
public class ProjetoResource {
@GET
@Produces(MediaType.APPLICATION_XML)
public String buscar() {
Projeto projeto = new ProjetoDao().busca(1L);
return projeto.toXML();
}
}
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.junit.Assert;
import org.junit.Test;
public class ProjetoTest {
@Test
public void testarConexaoComProjeto() {
Client client = ClientBuilder.newClient();
WebTarget alvo = client.target("http://localhost:8080");
String conteudo = alvo.path("/projeto").request().get(String.class);
Assert.assertTrue(conteudo.contains("<nome>Minha loja"));
}
}
javax.ws.rs.NotFoundException: HTTP 404 Not Found
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:917)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at br.com.alura.loja.ProjetoTest.testarConexaoComProjeto(ProjetoTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
erro quando rodo o teste