Skip to content

Commit

Permalink
Add units tests for spring graphql annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
IshikaDawda committed Oct 4, 2024
1 parent 9f927c2 commit 16b3da4
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.nr.agent.security.instrumentation.springweb.springweb.app;

import com.newrelic.api.agent.Trace;
Expand All @@ -16,7 +10,7 @@ public static String requestMapping() {
TestMappings path = new TestMappings();
return path.testRequest();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -27,7 +21,7 @@ public static String getMapping() {
TestMappings path = new TestMappings();
return path.testGet();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -38,7 +32,7 @@ public static String postMapping() {
TestMappings path = new TestMappings();
return path.testPost();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -49,7 +43,7 @@ public static String patchMapping() {
TestMappings path = new TestMappings();
return path.testPatch();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -60,7 +54,7 @@ public static String putMapping() {
TestMappings path = new TestMappings();
return path.testPut();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -71,7 +65,7 @@ public static String deleteMapping() {
TestMappings path = new TestMappings();
return path.testDelete();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -82,7 +76,7 @@ public static String requestMappingWithRest() {
TestMappingsWithRest path = new TestMappingsWithRest();
return path.testRequest();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -93,7 +87,7 @@ public static String getMappingWithRest() {
TestMappingsWithRest path = new TestMappingsWithRest();
return path.testGet();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -104,7 +98,7 @@ public static String postMappingWithRest() {
TestMappingsWithRest path = new TestMappingsWithRest();
return path.testPost();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -115,7 +109,7 @@ public static String patchMappingWithRest() {
TestMappingsWithRest path = new TestMappingsWithRest();
return path.testPatch();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -126,7 +120,7 @@ public static String putMappingWithRest() {
TestMappingsWithRest path = new TestMappingsWithRest();
return path.testPut();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}
Expand All @@ -137,8 +131,98 @@ public static String deleteMappingWithRest() {
TestMappingsWithRest path = new TestMappingsWithRest();
return path.testDelete();
} catch (RuntimeException caught) {
System.out.printf("Caught exception");
System.out.print("Caught exception");
}
return null;
}

@Trace(dispatcher = true)
public static void batchMappingWithRest() {
try {
new TestMappingsWithRest().testBatchMapping();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void mutationWithRest() {
try {
new TestMappingsWithRest().testMutation();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void queryWithRest() {
try {
new TestMappingsWithRest().testQuery();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void schemaMappingWithRest() {
try {
new TestMappingsWithRest().testSchemaMapping();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void subscriptionMappingWithRest() {
try {
new TestMappingsWithRest().testSubscriptionMapping();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void batchMapping() {
try {
new TestMappings().testBatchMapping();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void mutation() {
try {
new TestMappings().testMutation();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void query() {
try {
new TestMappings().testQuery();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void schemaMapping() {
try {
new TestMappings().testSchemaMapping();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}

@Trace(dispatcher = true)
public static void subscriptionMapping() {
try {
new TestMappings().testSubscriptionMapping();
} catch (RuntimeException caught) {
System.out.print("Caught exception");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nr.agent.security.instrumentation.springweb.springweb.app;/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.nr.agent.security.instrumentation.springweb.springweb.app;

import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -46,4 +46,29 @@ public String testPut() {
public String testDelete() {
return "From Delete Mapping";
}

@MutationMapping
public void testMutation() {
// mutation mapping
}

@QueryMapping
public void testQuery() {
// Query mapping
}

@SchemaMapping
public void testSchemaMapping() {
// SchemaMapping
}

@SubscriptionMapping
public void testSubscriptionMapping() {
// SubscriptionMapping
}

@BatchMapping
public void testBatchMapping() {
// BatchMapping
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nr.agent.security.instrumentation.springweb.springweb.app;/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.nr.agent.security.instrumentation.springweb.springweb.app;

import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -46,4 +46,30 @@ public String testPut() {
public String testDelete() {
return "From Delete RestMapping";
}

@MutationMapping
public void testMutation() {
// mutation mapping
}

@QueryMapping
public void testQuery() {
// Query mapping
}

@SchemaMapping
public void testSchemaMapping() {
// SchemaMapping
}

@SubscriptionMapping
public void testSubscriptionMapping() {
// SubscriptionMapping
}

@BatchMapping
public void testBatchMapping() {
// BatchMapping
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.nr.agent.security.instrumentation.springweb.springweb.test;

import com.newrelic.agent.security.introspec.InstrumentationTestConfig;
Expand All @@ -27,7 +21,6 @@ public void testRequestMapping() {
Assert.assertEquals("From Request Mapping", App.requestMapping());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
// String expectedTransactionName = "OtherTransaction/SpringController/errorPath (GET)";
AgentMetaData meta = introspector.getSecurityMetaData().getMetaData();
Assert.assertNotNull("Service trace can not be empty/null.", meta.getServiceTrace());
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Expand Down Expand Up @@ -88,4 +81,60 @@ public void testDeleteMapping() {
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Assert.assertTrue("Annotated userLevelService Method was not encountered.", meta.isFoundAnnotedUserLevelServiceMethod());
}


@Test
public void testBatchMapping() {
App.batchMapping();

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
AgentMetaData meta = introspector.getSecurityMetaData().getMetaData();
Assert.assertNotNull("Service trace can not be empty/null.", meta.getServiceTrace());
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Assert.assertTrue("Annotated userLevelService Method was not encountered.", meta.isFoundAnnotedUserLevelServiceMethod());
}

@Test
public void testMutation() {
App.mutation();

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
AgentMetaData meta = introspector.getSecurityMetaData().getMetaData();
Assert.assertNotNull("Service trace can not be empty/null.", meta.getServiceTrace());
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Assert.assertTrue("Annotated userLevelService Method was not encountered.", meta.isFoundAnnotedUserLevelServiceMethod());
}

@Test
public void testQuery() {
App.query();

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
AgentMetaData meta = introspector.getSecurityMetaData().getMetaData();
Assert.assertNotNull("Service trace can not be empty/null.", meta.getServiceTrace());
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Assert.assertTrue("Annotated userLevelService Method was not encountered.", meta.isFoundAnnotedUserLevelServiceMethod());
}

@Test
public void testSchemaMapping() {
App.schemaMapping();

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
AgentMetaData meta = introspector.getSecurityMetaData().getMetaData();
Assert.assertNotNull("Service trace can not be empty/null.", meta.getServiceTrace());
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Assert.assertTrue("Annotated userLevelService Method was not encountered.", meta.isFoundAnnotedUserLevelServiceMethod());
}

@Test
public void testSubscriptionMapping() {
App.subscriptionMapping();

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
AgentMetaData meta = introspector.getSecurityMetaData().getMetaData();
Assert.assertNotNull("Service trace can not be empty/null.", meta.getServiceTrace());
Assert.assertTrue("user level service method was not encountered.", meta.isUserLevelServiceMethodEncountered());
Assert.assertTrue("Annotated userLevelService Method was not encountered.", meta.isFoundAnnotedUserLevelServiceMethod());
}
}
Loading

0 comments on commit 16b3da4

Please sign in to comment.