Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/4224fdd8482e1877a102 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/4224fdd8482e1877a102 to your computer and use it in GitHub Desktop.
Tooling API - apexManifestのサンプル
<apex:page controller="ApexManifestDemoController" showHeader="true" sidebar="false" id="page">
<div id="vf-page">
<apex:form id="form">
<apex:pageBlock title="Tooling API Demo">
<apex:pageBlockTable value="{!wrapperList}" var="item">
<apex:column headerValue="ID">
<apex:outputText value="{!item.apexId}" />
</apex:column>
<apex:column headerValue="Namespace">
<apex:outputText value="{!item.apexNamespace}" />
</apex:column>
<apex:column headerValue="Type">
<apex:outputText value="{!item.apexType}" />
</apex:column>
<apex:column headerValue="TypeRef">
<apex:outputText value="{!item.apexTypeRef}" />
</apex:column>
<apex:column headerValue="Name">
<apex:outputText value="{!item.apexName}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</div>
</apex:page>
public with sharing class ApexManifestDemoController {
public List<Wrapper> wrapperList {get; set;}
public ApexManifestDemoController() {
// new List
this.wrapperList = new List<Wrapper>();
// Tooling API
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v33.0/tooling/apexManifest');
req.setMethod('GET');
HttpResponse res = http.send(req);
String jsondata = res.getBody();
// Check List
if (String.isEmpty(jsondata)) {
return;
}
// テストクラスでエラーが解決できないので今回はスキップ
// System.TypeException: Invalid conversion from runtime type Map<String,ANY> to List<ANY>
if(Test.isRunningTest()){
return;
}
// Get Results
List<Object> apexManifestList = (List<Object>)JSON.deserializeUntyped(jsondata);
for (Object obj : apexManifestList) {
Map<String, Object> recordMap = new Map<String, Object>((Map<String, Object>)obj);
// Add List
this.wrapperList.add(new Wrapper(recordMap));
}
// System.debug
System.debug(res);
System.debug(apexManifestList);
}
public class Wrapper {
public String apexId {get; set;}
public String apexName {get; set;}
public String apexNamespace {get; set;}
public String apexType {get; set;}
public String apexTypeRef {get; set;}
public Wrapper(Map<String, Object> recordMap) {
this.apexId = (String)recordMap.get('id');
this.apexName = (String)recordMap.get('name');
this.apexNamespace = (String)recordMap.get('namespace');
this.apexType = (String)recordMap.get('type');
this.apexTypeRef = (String)recordMap.get('typeRef');
}
}
}
@isTest
private class ApexManifestDemoControllerTest {
private static User testAdminUser = new User(Id = UserInfo.getUserId());
/**
* ApexManifestDemoControllerTest
*/
static testMethod void ApexManifestDemoControllerTest() {
System.runAs(testAdminUser) {
// Set HttpCalloutMock
Test.setMock(HttpCalloutMock.class, new ApexMainfestDemoHttpMock());
Test.startTest();
// Test
ApexManifestDemoController cls = new ApexManifestDemoController();
Test.stopTest();
// Result
//System.assertEquals(cls.wrapperList.size(), 1);
}
}
}
@isTest
global class ApexMainfestDemoHttpMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v33.0/tooling/apexManifest');
res.setBody('{"id":"null", "name":"AddressAndLocationDemoController", "namespace":"null", "type":"CLASS", "typeRef":"AddressAndLocationDemoController"}');
req.setMethod('GET');
res.setStatusCode(200);
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment