### Question:
PrivUnsubscribe extends AbstractPrivateSubscriptionMethod { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { try { final PrivateUnsubscribeRequest unsubscribeRequest = getMapper().mapPrivateUnsubscribeRequest(requestContext); checkIfPrivacyGroupMatchesAuthenticatedEnclaveKey( requestContext, unsubscribeRequest.getPrivacyGroupId()); final boolean unsubscribed = subscriptionManager().unsubscribe(unsubscribeRequest); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), unsubscribed); } catch (final InvalidSubscriptionRequestException isEx) { return new JsonRpcErrorResponse( requestContext.getRequest().getId(), JsonRpcError.INVALID_REQUEST); } catch (final SubscriptionNotFoundException snfEx) { return new JsonRpcErrorResponse( requestContext.getRequest().getId(), JsonRpcError.SUBSCRIPTION_NOT_FOUND); } } PrivUnsubscribe(
      final SubscriptionManager subscriptionManager,
      final SubscriptionRequestMapper mapper,
      final PrivacyController privacyController,
      final EnclavePublicKeyProvider enclavePublicKeyProvider); @Override String getName(); @Override JsonRpcResponse response(final JsonRpcRequestContext requestContext);  }### Answer:
@Test public void responseContainsUnsubscribeStatus() { final JsonRpcRequestContext request = createPrivUnsubscribeRequest(); final PrivateUnsubscribeRequest unsubscribeRequest = new PrivateUnsubscribeRequest(1L, CONNECTION_ID, PRIVACY_GROUP_ID); when(mapperMock.mapPrivateUnsubscribeRequest(eq(request))).thenReturn(unsubscribeRequest); when(subscriptionManagerMock.unsubscribe(eq(unsubscribeRequest))).thenReturn(true); final JsonRpcSuccessResponse expectedResponse = new JsonRpcSuccessResponse(request.getRequest().getId(), true); assertThat(privUnsubscribe.response(request)).isEqualTo(expectedResponse); }
@Test public void invalidUnsubscribeRequestReturnsInvalidRequestResponse() { final JsonRpcRequestContext request = createPrivUnsubscribeRequest(); when(mapperMock.mapPrivateUnsubscribeRequest(any())) .thenThrow(new InvalidSubscriptionRequestException()); final JsonRpcErrorResponse expectedResponse = new JsonRpcErrorResponse(request.getRequest().getId(), JsonRpcError.INVALID_REQUEST); assertThat(privUnsubscribe.response(request)).isEqualTo(expectedResponse); }
@Test public void whenSubscriptionNotFoundReturnError() { final JsonRpcRequestContext request = createPrivUnsubscribeRequest(); when(mapperMock.mapPrivateUnsubscribeRequest(any())) .thenReturn(mock(PrivateUnsubscribeRequest.class)); when(subscriptionManagerMock.unsubscribe(any())) .thenThrow(new SubscriptionNotFoundException(1L)); final JsonRpcErrorResponse expectedResponse = new JsonRpcErrorResponse(request.getRequest().getId(), JsonRpcError.SUBSCRIPTION_NOT_FOUND); assertThat(privUnsubscribe.response(request)).isEqualTo(expectedResponse); }
@Test public void multiTenancyCheckFailure() { final User user = mock(User.class); final JsonRpcRequestContext jsonRpcrequestContext = createPrivUnsubscribeRequestWithUser(user); final PrivateUnsubscribeRequest unsubscribeRequest = new PrivateUnsubscribeRequest(0L, CONNECTION_ID, PRIVACY_GROUP_ID); when(mapperMock.mapPrivateUnsubscribeRequest(any())).thenReturn(unsubscribeRequest); when(enclavePublicKeyProvider.getEnclaveKey(any())).thenReturn(ENCLAVE_KEY); doThrow(new MultiTenancyValidationException("msg")) .when(privacyController) .verifyPrivacyGroupContainsEnclavePublicKey(eq(PRIVACY_GROUP_ID), eq(ENCLAVE_KEY)); assertThatThrownBy(() -> privUnsubscribe.response(jsonRpcrequestContext)) .isInstanceOf(MultiTenancyValidationException.class) .hasMessageContaining("msg"); }