You need to sign in to do that
Don't have an account?
Rajbharath
Need Apex Test Class
Hello all, I am new to Apex development. I need a sample test class for the below code:
public with sharing class CalloutAssistantController { public Integer maxRounds { get; set; } public String teamToAdd { get; set; } public RoundWrapper roundToAdd { get; set; } public Integer fixturePositionToAdd { get; set; } public String playedRoundsBody { get; set; } public String rawResponseText { get; set; } public List<TeamWrapper> teamList { get; set; } public List<RoundWrapper> roundList { get; set; } public FixtureResponse fixtureResponse { get; set; } public String debugString { get; set; } public CalloutAssistantController() { maxRounds = 0; teamToAdd = ''; fixturePositionToAdd = null; playedRoundsBody = ''; teamList = new List<TeamWrapper>(); roundList = new List<RoundWrapper>(); rawResponseText = 'Please submit request'; debugString = ''; } public void addTeam() { teamList.add(new teamWrapper(teamToAdd, fixturePositionToAdd)); debugString = JSON.serializePretty(new FixtureRequest(maxRounds, teamList, roundList)); teamToAdd = ''; fixturePositionToAdd = null; } public void cleanList() { teamList = new List<TeamWrapper>(); } public void submitRequest() { FixtureRequest request = new FixtureRequest(maxRounds, teamList, roundList); fixtureResponse = new FixtureResponse(request); fixtureResponse.submitRequest(); } public class FixtureRequest { public Integer maxRounds { get; set; } public Map<String, Integer> teams { get; set; } public List<RoundWrapper> playedRounds { get; set; } public FixtureRequest() { this.maxRounds = null; this.teams = new Map<String, Integer>(); this.playedRounds = new List<RoundWrapper>(); } public FixtureRequest(Integer maxRounds, List<TeamWrapper> submittedTeams, List<RoundWrapper> playedRounds) { this.maxRounds = maxRounds; this.teams = new Map<String, Integer>(); for (TeamWrapper team : submittedTeams) { this.teams.put(team.teamName, team.fixturePosition); } this.playedRounds = playedRounds; } } public class FixtureResponse { public Boolean success { get; set; } public String errorString { get; set; } public String requestString { get; set; } public String rawResponseText { get; set; } public List<FixtureWrapper> fixtureList { get; set; } public FixtureResponse(FixtureRequest newRequest) { this.requestString = JSON.serialize(newRequest); fixtureList = new List<FixtureWrapper>(); } public void submitRequest() { try { HttpResponse response = HTTPCallout.sendRequest('http://perl-fixture-equalisation.herokuapp.com/fixture', 'POST', requestString, 'fixtureGeneration'); rawResponseText = response.getBody(); parseResponse(); } catch (Exception e) { HTTPCallout.apiCallLogEntry.ResponseBody__c = e.getMessage(); HTTPCallout.insertLogs(); } HTTPCallout.insertLogs(); } public void parseResponse() { Map<String, Object> root = (Map<String, Object>) JSON.deserializeUntyped(rawResponseText); success = (String) root.get('status') == 'success'; if (!success) { errorString = (String) root.get('error'); } else { List<Object> fixtures = (List<Object>) root.get('rounds'); for (Object fixtureObject : fixtures) { Map<String, Object> fixtureMap = (Map<String, Object>) fixtureObject; FixtureWrapper fixture = new FixtureWrapper(); fixture.name = (String) fixtureMap.get('name'); List <Object> matches = (List<Object>) fixtureMap.get('matches'); for (Object matchObject : matches) { Map<String, Object> matchMap = (Map<String, Object>) matchObject; fixture.matches.add(new MatchWrapper((String) matchMap.get('home'), (String) matchMap.get('away'))); } fixtureList.add(fixture); } } } } public class FixtureWrapper { public String name { get; set; } public List<MatchWrapper> matches { get; set; } public FixtureWrapper() { this.name = ''; this.matches = new List<MatchWrapper>(); } } public class MatchWrapper { public String home { get; set; } public String away { get; set; } public MatchWrapper(String home, String away) { this.home = home; this.away = away; } } public class TeamWrapper { public String teamName { get; set; } public Integer fixturePosition { get; set; } public TeamWrapper(String teamName, Integer fixturePosition) { this.teamName = teamName; this.fixturePosition = fixturePosition; } } public class RoundWrapper { public List<MatchWrapper> matches { get; set; } public List<String> name { get; set; } public RoundWrapper(List<MatchWrapper> matches) { this.matches = matches; this.name = null; } } }
If you want the basic skeleton of how to initiate a test class then I suggest you to go through this module.
https://trailhead.salesforce.com/en/modules/apex_testing