Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.3 kB
3
Indexable
Never
  @Override
    public List<FieldMetaInfo> getAllFeatureFieldMetaInfo(int featureId) {
        BooleanExpression predicate = featureRuleConfigField.isNotNull()
                .and(featureRuleConfigField.featureId.eq(featureId));

        JPQLQuery<Tuple> jpqlQuery = from(featureRuleConfigField).distinct()
                .select(
                        featureRuleConfigField.key,
                        featureRuleConfigField.valueType,
                        featureRuleConfigField.type)
                .where(predicate)
                .orderBy(featureRuleConfigField.key.asc());

        List<FieldMetaInfo> fieldMetaInfoList = new ArrayList<>();
        for (Tuple tuple : jpqlQuery.fetch()) {
            fieldMetaInfoList.add(new FieldMetaInfo(
                    tuple.get(featureRuleConfigField.key),
                    tuple.get(featureRuleConfigField.valueType),
                    tuple.get(featureRuleConfigField.type)));
        }

        return fieldMetaInfoList;
    }


/////////////////
@Transactional
    @Override
    public void deleteFeatureRuleConfigField(int featureId, String fieldKey) {
        if (fieldKey == null) {
            throw new FieldKeyNotFoundException();
        }

        BooleanExpression predicate = featureRuleConfigField.isNotNull()
                .and(featureRuleConfigField.featureId.eq(featureId))
                .and(featureRuleConfigField.key.eq(fieldKey));

        delete(featureRuleConfigField).where(predicate).execute();

        List<FeatureRuleConfig> featureRuleConfigs =
                featureRuleConfigService.getAllFeatureRuleConfigs(featureId);
        for (FeatureRuleConfig featureRuleConfig : featureRuleConfigs) {
            featureRuleConfigService.updateFeatureRuleConfigById(featureRuleConfig.getId(), null);
        }
    }
//////////////////////////
@ActiveProfiles(Profiles.TEST)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class FeatureRuleConfigFieldServiceUnitTest {

    @Autowired
    private FeatureRuleConfigFieldService featureRuleConfigFieldService;

    private FeatureRuleConfigField resultFeatureRuleConfigField,
            receivedFeatureRuleConfigFieldForSave;
    private Integer receivedFeatureRuleConfigId, receivedFeatureId, updatedFeatureRuleConfigId;
    private String receivedFieldKey;
    private List<FeatureRuleConfig> resultFeatureRuleConfigList;
    private FeatureRuleConfig resultFeatureRuleConfig;

    @MockBean
    private FeatureRuleConfigService featureRuleConfigService;

    @MockBean
    private FeatureRuleConfigFieldRepository featureRuleConfigFieldRepository;

    @BeforeEach
    public void setUp() throws Exception {
        MockitoAnnotations.openMocks(this);
        Mockito.when(featureRuleConfigFieldRepository.findOneByFeatureRuleConfigIdAndKey(
                Mockito.anyInt(), Mockito.anyString())).thenAnswer(
                        invocation -> {
                                receivedFeatureRuleConfigId =
                                        invocation.getArgument(0, Integer.class);
                                receivedFieldKey =
                                        invocation.getArgument(1, String.class);
                                return resultFeatureRuleConfigField;
                        });
        Mockito.when(featureRuleConfigFieldRepository.save(
                Mockito.any(FeatureRuleConfigField.class))).thenAnswer(invocation -> {
                    receivedFeatureRuleConfigFieldForSave =
                            invocation.getArgument(0, FeatureRuleConfigField.class);
                    return null;
                });
        Mockito.when(featureRuleConfigService.getAllFeatureRuleConfigs(
                Mockito.anyInt())).thenAnswer(invocation -> {
                    receivedFeatureId = invocation.getArgument(0, Integer.class);
                    resultFeatureRuleConfigList.add(resultFeatureRuleConfig);
                    return resultFeatureRuleConfigList;
                });
        Mockito.doAnswer(invocation -> {
            updatedFeatureRuleConfigId = invocation.getArgument(0, Integer.class);
            return null;
        }).when(featureRuleConfigService).updateFeatureRuleConfigById(
                Mockito.anyInt(), Mockito.nullable(FeatureRuleConfig.class));
    }

    @Test
    public void testUpdateFeatureRuleConfigField() {
        FeatureRuleConfigField featureRuleConfigField = new FeatureRuleConfigField();
        featureRuleConfigField.setKey("testKey");
        featureRuleConfigField.setValue("testValue");
        featureRuleConfigField.setValueType(FieldValueType.STRING);

        resultFeatureRuleConfigField = new FeatureRuleConfigField();
        resultFeatureRuleConfigField.setValueType(featureRuleConfigField.getValueType());
        featureRuleConfigFieldService.updateFeatureRuleConfigField(
                1, featureRuleConfigField);

        assertEquals(Integer.valueOf(1), receivedFeatureRuleConfigId);
        assertEquals("testKey", receivedFieldKey);
        assertEquals("testValue", receivedFeatureRuleConfigFieldForSave.getValue());
    }
Leave a Comment