Commit aad34ae0 by Shwetha GS

ATLAS-888 NPE in NotificationHookConsumer (sumasai via shwethags)

parent 8fefd165
...@@ -23,6 +23,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset ...@@ -23,6 +23,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset
ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags) ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags)
ALL CHANGES: ALL CHANGES:
ATLAS-888 NPE in NotificationHookConsumer (sumasai via shwethags)
ATLAS-884 Process registration should call Entity update instead of create (sumasai) ATLAS-884 Process registration should call Entity update instead of create (sumasai)
ATLAS-515 Ability to initialize Kafka topics with more than 1 replica (yhemanth) ATLAS-515 Ability to initialize Kafka topics with more than 1 replica (yhemanth)
ATLAS-891 UI changes to implement Update term (Kalyanikashikar via yhemanth) ATLAS-891 UI changes to implement Update term (Kalyanikashikar via yhemanth)
......
...@@ -242,6 +242,11 @@ ...@@ -242,6 +242,11 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
......
...@@ -18,8 +18,10 @@ ...@@ -18,8 +18,10 @@
package org.apache.atlas.web.resources; package org.apache.atlas.web.resources;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.apache.atlas.AtlasClient; import org.apache.atlas.AtlasClient;
import org.apache.atlas.AtlasConstants;
import org.apache.atlas.AtlasException; import org.apache.atlas.AtlasException;
import org.apache.atlas.EntityAuditEvent; import org.apache.atlas.EntityAuditEvent;
import org.apache.atlas.services.MetadataService; import org.apache.atlas.services.MetadataService;
...@@ -33,6 +35,7 @@ import org.apache.atlas.typesystem.types.ValueConversionException; ...@@ -33,6 +35,7 @@ import org.apache.atlas.typesystem.types.ValueConversionException;
import org.apache.atlas.utils.ParamChecker; import org.apache.atlas.utils.ParamChecker;
import org.apache.atlas.web.util.Servlets; import org.apache.atlas.web.util.Servlets;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.http.protocol.HTTP;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
...@@ -59,6 +62,7 @@ import javax.ws.rs.core.Response; ...@@ -59,6 +62,7 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import java.net.URI; import java.net.URI;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
...@@ -119,8 +123,7 @@ public class EntityResource { ...@@ -119,8 +123,7 @@ public class EntityResource {
final List<String> guids = metadataService.createEntities(entities); final List<String> guids = metadataService.createEntities(entities);
JSONObject response = getResponse(new AtlasClient.EntityResult(guids, null, null)); JSONObject response = getResponse(new AtlasClient.EntityResult(guids, null, null));
UriBuilder ub = uriInfo.getAbsolutePathBuilder(); URI locationURI = getLocationURI(guids);
URI locationURI = guids.isEmpty() ? null : ub.path(guids.get(0)).build();
return Response.created(locationURI).entity(response).build(); return Response.created(locationURI).entity(response).build();
...@@ -128,7 +131,7 @@ public class EntityResource { ...@@ -128,7 +131,7 @@ public class EntityResource {
LOG.error("Unique constraint violation", e); LOG.error("Unique constraint violation", e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT)); throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
} catch (ValueConversionException ve) { } catch (ValueConversionException ve) {
LOG.error("Unable to persist entity instance due to a desrialization error ", ve); LOG.error("Unable to persist entity instance due to a deserialization error ", ve);
throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST)); throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
} catch (AtlasException | IllegalArgumentException e) { } catch (AtlasException | IllegalArgumentException e) {
LOG.error("Unable to persist entity instance", e); LOG.error("Unable to persist entity instance", e);
...@@ -139,6 +142,23 @@ public class EntityResource { ...@@ -139,6 +142,23 @@ public class EntityResource {
} }
} }
@VisibleForTesting
public URI getLocationURI(List<String> guids) {
URI locationURI = null;
if (uriInfo != null) {
UriBuilder ub = uriInfo.getAbsolutePathBuilder();
locationURI = guids.isEmpty() ? null : ub.path(guids.get(0)).build();
} else {
String uriPath = AtlasClient.API.GET_ENTITY.getPath();
locationURI = guids.isEmpty() ? null : UriBuilder
.fromPath(AtlasConstants.DEFAULT_ATLAS_REST_ADDRESS)
.path(uriPath).path(guids.get(0)).build();
}
return locationURI;
}
private JSONObject getResponse(AtlasClient.EntityResult entityResult) throws AtlasException, JSONException { private JSONObject getResponse(AtlasClient.EntityResult entityResult) throws AtlasException, JSONException {
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId()); response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
...@@ -171,7 +191,7 @@ public class EntityResource { ...@@ -171,7 +191,7 @@ public class EntityResource {
LOG.error("Unique constraint violation", e); LOG.error("Unique constraint violation", e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT)); throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
} catch (ValueConversionException ve) { } catch (ValueConversionException ve) {
LOG.error("Unable to persist entity instance due to a desrialization error ", ve); LOG.error("Unable to persist entity instance due to a deserialization error ", ve);
throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST)); throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
} catch (AtlasException | IllegalArgumentException e) { } catch (AtlasException | IllegalArgumentException e) {
LOG.error("Unable to persist entity instance", e); LOG.error("Unable to persist entity instance", e);
...@@ -234,7 +254,7 @@ public class EntityResource { ...@@ -234,7 +254,7 @@ public class EntityResource {
JSONObject response = getResponse(entityResult); JSONObject response = getResponse(entityResult);
return Response.ok(response).build(); return Response.ok(response).build();
} catch (ValueConversionException ve) { } catch (ValueConversionException ve) {
LOG.error("Unable to persist entity instance due to a desrialization error ", ve); LOG.error("Unable to persist entity instance due to a deserialization error ", ve);
throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST)); throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
} catch(EntityExistsException e) { } catch(EntityExistsException e) {
LOG.error("Unique constraint violation", e); LOG.error("Unique constraint violation", e);
...@@ -549,8 +569,9 @@ public class EntityResource { ...@@ -549,8 +569,9 @@ public class EntityResource {
LOG.debug("Adding trait={} for entity={} ", traitDefinition, guid); LOG.debug("Adding trait={} for entity={} ", traitDefinition, guid);
metadataService.addTrait(guid, traitDefinition); metadataService.addTrait(guid, traitDefinition);
UriBuilder ub = uriInfo.getAbsolutePathBuilder(); URI locationURI = getLocationURI(new ArrayList<String>() {{
URI locationURI = ub.path(guid).build(); add(guid);
}});
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId()); response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.apache.atlas; package org.apache.atlas;
import com.google.inject.Inject;
import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse;
import org.apache.atlas.typesystem.Referenceable; import org.apache.atlas.typesystem.Referenceable;
import org.apache.atlas.web.resources.EntityResource; import org.apache.atlas.web.resources.EntityResource;
...@@ -27,11 +28,14 @@ import org.codehaus.jettison.json.JSONObject; ...@@ -27,11 +28,14 @@ import org.codehaus.jettison.json.JSONObject;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -46,8 +50,12 @@ import static org.testng.Assert.assertFalse; ...@@ -46,8 +50,12 @@ import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
@Guice(modules= RepositoryMetadataModule.class)
public class LocalAtlasClientTest { public class LocalAtlasClientTest {
@Mock @Mock
private EntityResource mockEntityResource;
@Inject
private EntityResource entityResource; private EntityResource entityResource;
@Mock @Mock
...@@ -61,14 +69,14 @@ public class LocalAtlasClientTest { ...@@ -61,14 +69,14 @@ public class LocalAtlasClientTest {
@Test @Test
public void testCreateEntity() throws Exception { public void testCreateEntity() throws Exception {
Response response = mock(Response.class); Response response = mock(Response.class);
when(entityResource.submit(any(HttpServletRequest.class))).thenReturn(response); when(mockEntityResource.submit(any(HttpServletRequest.class))).thenReturn(response);
final String guid = random(); final String guid = random();
when(response.getEntity()).thenReturn(new JSONObject() {{ when(response.getEntity()).thenReturn(new JSONObject() {{
put(ENTITIES, new JSONObject( put(ENTITIES, new JSONObject(
new AtlasClient.EntityResult(Arrays.asList(guid), null, null).toString()).get(ENTITIES)); new AtlasClient.EntityResult(Arrays.asList(guid), null, null).toString()).get(ENTITIES));
}}); }});
LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, entityResource); LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, mockEntityResource);
List<String> results = atlasClient.createEntity(new Referenceable(random())); List<String> results = atlasClient.createEntity(new Referenceable(random()));
assertEquals(results.size(), 1); assertEquals(results.size(), 1);
assertEquals(results.get(0), guid); assertEquals(results.get(0), guid);
...@@ -76,10 +84,10 @@ public class LocalAtlasClientTest { ...@@ -76,10 +84,10 @@ public class LocalAtlasClientTest {
@Test @Test
public void testException() throws Exception { public void testException() throws Exception {
LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, entityResource); LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, mockEntityResource);
Response response = mock(Response.class); Response response = mock(Response.class);
when(entityResource.submit(any(HttpServletRequest.class))).thenThrow(new WebApplicationException(response)); when(mockEntityResource.submit(any(HttpServletRequest.class))).thenThrow(new WebApplicationException(response));
when(response.getEntity()).thenReturn(new JSONObject() {{ when(response.getEntity()).thenReturn(new JSONObject() {{
put("stackTrace", "stackTrace"); put("stackTrace", "stackTrace");
}}); }});
...@@ -91,7 +99,7 @@ public class LocalAtlasClientTest { ...@@ -91,7 +99,7 @@ public class LocalAtlasClientTest {
assertEquals(e.getStatus(), ClientResponse.Status.BAD_REQUEST); assertEquals(e.getStatus(), ClientResponse.Status.BAD_REQUEST);
} }
when(entityResource.updateByUniqueAttribute(anyString(), anyString(), anyString(), when(mockEntityResource.updateByUniqueAttribute(anyString(), anyString(), anyString(),
any(HttpServletRequest.class))).thenThrow(new WebApplicationException(response)); any(HttpServletRequest.class))).thenThrow(new WebApplicationException(response));
when(response.getStatus()).thenReturn(Response.Status.NOT_FOUND.getStatusCode()); when(response.getStatus()).thenReturn(Response.Status.NOT_FOUND.getStatusCode());
try { try {
...@@ -106,7 +114,7 @@ public class LocalAtlasClientTest { ...@@ -106,7 +114,7 @@ public class LocalAtlasClientTest {
@Test @Test
public void testIsServerReady() throws Exception { public void testIsServerReady() throws Exception {
when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.ACTIVE); when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.ACTIVE);
LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, entityResource); LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, mockEntityResource);
assertTrue(atlasClient.isServerReady()); assertTrue(atlasClient.isServerReady());
when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.BECOMING_ACTIVE); when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.BECOMING_ACTIVE);
...@@ -117,14 +125,14 @@ public class LocalAtlasClientTest { ...@@ -117,14 +125,14 @@ public class LocalAtlasClientTest {
public void testUpdateEntity() throws Exception { public void testUpdateEntity() throws Exception {
final String guid = random(); final String guid = random();
Response response = mock(Response.class); Response response = mock(Response.class);
when(entityResource.updateByUniqueAttribute(anyString(), anyString(), anyString(), when(mockEntityResource.updateByUniqueAttribute(anyString(), anyString(), anyString(),
any(HttpServletRequest.class))).thenReturn(response); any(HttpServletRequest.class))).thenReturn(response);
when(response.getEntity()).thenReturn(new JSONObject() {{ when(response.getEntity()).thenReturn(new JSONObject() {{
put(ENTITIES, new JSONObject( put(ENTITIES, new JSONObject(
new AtlasClient.EntityResult(null, Arrays.asList(guid), null).toString()).get(ENTITIES)); new AtlasClient.EntityResult(null, Arrays.asList(guid), null).toString()).get(ENTITIES));
}}); }});
LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, entityResource); LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, mockEntityResource);
AtlasClient.EntityResult AtlasClient.EntityResult
entityResult = atlasClient.updateEntity(random(), random(), random(), new Referenceable(random())); entityResult = atlasClient.updateEntity(random(), random(), random(), new Referenceable(random()));
assertEquals(entityResult.getUpdateEntities(), Arrays.asList(guid)); assertEquals(entityResult.getUpdateEntities(), Arrays.asList(guid));
...@@ -139,8 +147,8 @@ public class LocalAtlasClientTest { ...@@ -139,8 +147,8 @@ public class LocalAtlasClientTest {
new AtlasClient.EntityResult(null, null, Arrays.asList(guid)).toString()).get(ENTITIES)); new AtlasClient.EntityResult(null, null, Arrays.asList(guid)).toString()).get(ENTITIES));
}}); }});
when(entityResource.deleteEntities(anyListOf(String.class), anyString(), anyString(), anyString())).thenReturn(response); when(mockEntityResource.deleteEntities(anyListOf(String.class), anyString(), anyString(), anyString())).thenReturn(response);
LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, entityResource); LocalAtlasClient atlasClient = new LocalAtlasClient(serviceState, mockEntityResource);
AtlasClient.EntityResult entityResult = atlasClient.deleteEntity(random(), random(), random()); AtlasClient.EntityResult entityResult = atlasClient.deleteEntity(random(), random(), random());
assertEquals(entityResult.getDeletedEntities(), Arrays.asList(guid)); assertEquals(entityResult.getDeletedEntities(), Arrays.asList(guid));
} }
...@@ -148,4 +156,12 @@ public class LocalAtlasClientTest { ...@@ -148,4 +156,12 @@ public class LocalAtlasClientTest {
private String random() { private String random() {
return RandomStringUtils.randomAlphanumeric(10); return RandomStringUtils.randomAlphanumeric(10);
} }
@Test
@Inject
public void testGetLocationURI() {
final String guid = "123";
URI uri = entityResource.getLocationURI(new ArrayList<String>() {{ add(guid); }});
uri.getRawPath().equals(AtlasConstants.DEFAULT_ATLAS_REST_ADDRESS + "/" + AtlasClient.API.GET_ENTITY.getPath() + "/" + guid);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment