CounterDelegator extends BaseDelegator implements ICounterDelegator { @Override public void setCounter(final String counterName, final String value, final boolean perforceCounter) throws ConnectionException, RequestException, AccessException { Validate.notBlank(counterName, "Counter name shouldn't be null or empty"); Validate.notBlank(value, "Counter value shouldn't be null or empty"); P4JavaExceptions.asRequestException(() -> { CounterOptions counterOptions = new CounterOptions() .setPerforceCounter(perforceCounter); setCounter(counterName, value, counterOptions); return null; }); } CounterDelegator(final IOptionsServer server); @Override String getCounter(final String counterName); @Override String getCounter(final String counterName, final CounterOptions opts); @Override void setCounter(final String counterName, final String value,
            final boolean perforceCounter); @Override String setCounter(final String counterName, final String value,
            final CounterOptions opts); @Override void deleteCounter(final String counterName, final boolean perforceCounter);  }
@Test public void setCounterBlankNameShouldFail() throws Exception { try { counterDelegator.setCounter("", mockCounterValue, true); } catch (IllegalArgumentException e) { return; } fail("Did not throw exception if counter name was blank"); }
@Test public void setCounterBlankValueShouldFail() throws Exception { try { counterDelegator.setCounter(mockCounterName, "", true); } catch (IllegalArgumentException e) { return; } fail("Did not throw exception if counter value was blank"); }
@Test public void setCounterThrowConnectionException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.COUNTER.toString()), eq(new String[] { mockCounterName, mockCounterValue }), any(null))) .thenThrow(ConnectionException.class); try { counterDelegator.setCounter(mockCounterName, mockCounterValue, new CounterOptions()); } catch (ConnectionException e) { return; } fail("Did not return ConnectionException"); }
@Test public void setCounterThrowAccessException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.COUNTER.toString()), eq(new String[] { mockCounterName, mockCounterValue }), any(null))) .thenThrow(AccessException.AccessExceptionForTests.class); try { counterDelegator.setCounter(mockCounterName, mockCounterValue, new CounterOptions()); } catch (AccessException e) { return; } fail("Did not return AccessException"); }
@Test public void setCounterThrowRequestException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.COUNTER.toString()), eq(new String[] { mockCounterName, mockCounterValue }), any(null))) .thenThrow(RequestException.class); try { counterDelegator.setCounter(mockCounterName, mockCounterValue, new CounterOptions()); } catch (RequestException e) { return; } fail("Did not return RequestException"); }
@Test public void setCounterP4ExceptionThrowsRequestException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.COUNTER.toString()), eq(new String[] { "-f", mockCounterName, mockCounterValue }), any(null))) .thenThrow(P4JavaException.class); try { counterDelegator.setCounter(mockCounterName, mockCounterValue, true); } catch (RequestException e) { return; } catch (Exception e) { fail("Threw " + e.getClass().getName() + " instead of RequestException"); } fail("Did not return RequestException"); }