@Test public void getFieldsForDescriptorTest() throws Exception { final String name = "name-test"; final Long typeId = 1L; final ConfigContextEnum configContextEnum = ConfigContextEnum.GLOBAL; final ConfigContextEnum invalidConfigContextEnum = ConfigContextEnum.DISTRIBUTION; final String definedFieldsKey = "defined-field-key-test"; Boolean isSensitive = Boolean.TRUE; RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(name, typeId); registeredDescriptorEntity.setId(1L); DescriptorKey descriptorKey = createDescriptorKey("descriptorKey-test"); ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name()); configContextEntity.setId(3L); DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(definedFieldsKey, isSensitive); RegisteredDescriptorRepository registeredDescriptorRepository = Mockito.mock(RegisteredDescriptorRepository.class); DefinedFieldRepository definedFieldRepository = Mockito.mock(DefinedFieldRepository.class); ConfigContextRepository configContextRepository = Mockito.mock(ConfigContextRepository.class); DescriptorTypeRepository descriptorTypeRepository = Mockito.mock(DescriptorTypeRepository.class); Mockito.when(registeredDescriptorRepository.findFirstByName(descriptorKey.getUniversalKey())).thenReturn(Optional.of(registeredDescriptorEntity)); Mockito.when(configContextRepository.findFirstByContext(configContextEnum.name())).thenReturn(Optional.of(configContextEntity)); Mockito.when(configContextRepository.findFirstByContext(invalidConfigContextEnum.name())).thenReturn(Optional.empty()); Mockito.when(configContextRepository.save(Mockito.any())).thenReturn(configContextEntity); Mockito.when(definedFieldRepository.findByDescriptorIdAndContext(Mockito.any(), Mockito.any())).thenReturn(List.of(definedFieldEntity)); DefaultDescriptorAccessor descriptorAccessor = new DefaultDescriptorAccessor(registeredDescriptorRepository, definedFieldRepository, configContextRepository, descriptorTypeRepository); List<DefinedFieldModel> definedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, configContextEnum); List<DefinedFieldModel> emptyConfigContextDefinedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, invalidConfigContextEnum); assertEquals(1, definedFieldModelList.size()); DefinedFieldModel definedFieldModel = definedFieldModelList.get(0); assertEquals(definedFieldsKey, definedFieldModel.getKey()); assertEquals(isSensitive, definedFieldModel.getSensitive()); List<ConfigContextEnum> configContextList = new ArrayList<>(definedFieldModel.getContexts()); assertEquals(configContextEnum, configContextList.get(0)); assertEquals(1, emptyConfigContextDefinedFieldModelList.size()); DefinedFieldModel emptyConfigContextDefinedFieldModel = emptyConfigContextDefinedFieldModelList.get(0); assertEquals(definedFieldsKey, emptyConfigContextDefinedFieldModel.getKey()); assertEquals(isSensitive, emptyConfigContextDefinedFieldModel.getSensitive()); List<ConfigContextEnum> configContextList2 = new ArrayList<>(emptyConfigContextDefinedFieldModel.getContexts()); assertEquals(invalidConfigContextEnum, configContextList2.get(0)); }
@Override public List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context) throws AlertDatabaseConstraintException { RegisteredDescriptorEntity descriptor = findDescriptorByKey(descriptorKey); Long contextId = saveContextAndReturnId(context); return getFieldsForDescriptorId(descriptor.getId(), contextId, context); }
DefaultDescriptorAccessor implements DescriptorAccessor { @Override public List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context) throws AlertDatabaseConstraintException { RegisteredDescriptorEntity descriptor = findDescriptorByKey(descriptorKey); Long contextId = saveContextAndReturnId(context); return getFieldsForDescriptorId(descriptor.getId(), contextId, context); } }
DefaultDescriptorAccessor implements DescriptorAccessor { @Override public List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context) throws AlertDatabaseConstraintException { RegisteredDescriptorEntity descriptor = findDescriptorByKey(descriptorKey); Long contextId = saveContextAndReturnId(context); return getFieldsForDescriptorId(descriptor.getId(), contextId, context); } @Autowired  DefaultDescriptorAccessor(RegisteredDescriptorRepository registeredDescriptorRepository, DefinedFieldRepository definedFieldRepository, ConfigContextRepository configContextRepository,
        DescriptorTypeRepository descriptorTypeRepository); }
DefaultDescriptorAccessor implements DescriptorAccessor { @Override public List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context) throws AlertDatabaseConstraintException { RegisteredDescriptorEntity descriptor = findDescriptorByKey(descriptorKey); Long contextId = saveContextAndReturnId(context); return getFieldsForDescriptorId(descriptor.getId(), contextId, context); } @Autowired  DefaultDescriptorAccessor(RegisteredDescriptorRepository registeredDescriptorRepository, DefinedFieldRepository definedFieldRepository, ConfigContextRepository configContextRepository,
        DescriptorTypeRepository descriptorTypeRepository); @Override List<RegisteredDescriptorModel> getRegisteredDescriptors(); @Override Optional<RegisteredDescriptorModel> getRegisteredDescriptorByKey(DescriptorKey descriptorKey); @Override List<RegisteredDescriptorModel> getRegisteredDescriptorsByType(DescriptorType descriptorType); @Override Optional<RegisteredDescriptorModel> getRegisteredDescriptorById(Long descriptorId); @Override List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context); @Override List<DefinedFieldModel> getFieldsForDescriptorById(Long descriptorId, ConfigContextEnum context); }
DefaultDescriptorAccessor implements DescriptorAccessor { @Override public List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context) throws AlertDatabaseConstraintException { RegisteredDescriptorEntity descriptor = findDescriptorByKey(descriptorKey); Long contextId = saveContextAndReturnId(context); return getFieldsForDescriptorId(descriptor.getId(), contextId, context); } @Autowired  DefaultDescriptorAccessor(RegisteredDescriptorRepository registeredDescriptorRepository, DefinedFieldRepository definedFieldRepository, ConfigContextRepository configContextRepository,
        DescriptorTypeRepository descriptorTypeRepository); @Override List<RegisteredDescriptorModel> getRegisteredDescriptors(); @Override Optional<RegisteredDescriptorModel> getRegisteredDescriptorByKey(DescriptorKey descriptorKey); @Override List<RegisteredDescriptorModel> getRegisteredDescriptorsByType(DescriptorType descriptorType); @Override Optional<RegisteredDescriptorModel> getRegisteredDescriptorById(Long descriptorId); @Override List<DefinedFieldModel> getFieldsForDescriptor(DescriptorKey descriptorKey, ConfigContextEnum context); @Override List<DefinedFieldModel> getFieldsForDescriptorById(Long descriptorId, ConfigContextEnum context);  }