use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.
the class AgentDao method findAgentCookieByUuid.
private AgentCookie findAgentCookieByUuid(final AgentIdentifier agentIdentifier) {
return (AgentCookie) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("from AgentCookie where uuid = :uuid");
query.setString("uuid", agentIdentifier.getUuid());
return query.uniqueResult();
}
});
}
use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.
the class AgentDao method cookieFor.
public String cookieFor(final AgentIdentifier agentIdentifier) {
String key = agentCacheKey(agentIdentifier);
AgentCookie cookie = (AgentCookie) cache.get(key);
if (cookie == null) {
synchronized (key) {
cookie = findAgentCookieByUuid(agentIdentifier);
cache.put(key, cookie);
}
}
if (cookie != null) {
return cookie.getCookie();
}
return null;
}
use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.
the class AgentDaoTest method shouldCacheCookieForAgent.
@Test
public void shouldCacheCookieForAgent() throws Exception {
AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
agentDao.associateCookie(agentIdentifier, "cookie");
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
AgentCookie agentCookie = (AgentCookie) session.createQuery("from AgentCookie where uuid = 'uuid'").uniqueResult();
agentCookie.updateCookie("updated_cookie");
session.update(agentCookie);
return null;
}
});
assertThat(cookieForUuid(), is("updated_cookie"));
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
goCache.clear();
assertThat(agentDao.cookieFor(agentIdentifier), is("updated_cookie"));
}
use of com.thoughtworks.go.server.domain.AgentCookie in project gocd by gocd.
the class AgentDaoTest method shouldNotClearCacheIfTransactionFails.
@Test
public void shouldNotClearCacheIfTransactionFails() throws Exception {
AgentIdentifier agentIdentifier = new AgentIdentifier("host", "127.0.0.1", "uuid");
agentDao.associateCookie(agentIdentifier, "cookie");
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
AgentCookie agentCookie = (AgentCookie) session.createQuery("from AgentCookie where uuid = 'uuid'").uniqueResult();
agentCookie.updateCookie("updated_cookie");
session.update(agentCookie);
return null;
}
});
assertThat(cookieForUuid(), is("updated_cookie"));
agentDao.setHibernateTemplate(mockHibernateTemplate);
doThrow(new RuntimeException("holy smoke")).when(mockHibernateTemplate).saveOrUpdate(any(AgentCookie.class));
try {
agentDao.associateCookie(agentIdentifier, "cookie");
fail("should have propagated saveOrUpdate exception");
} catch (Exception e) {
assertThat(e.getMessage(), is("holy smoke"));
}
assertThat(agentDao.cookieFor(agentIdentifier), is("cookie"));
}
Aggregations